I have a function to display a notification, which I call from various activities.
public static void CrearNotificacion(Context pContexto, String pTituloBarra, String pTitulo, String pTexto){
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) pContexto.getSystemService(ns);
Notification notification = new Notification(R.drawable.icono, pTituloBarra, System.currentTimeMillis());
notification.defaults |= Notification.DEFAULT_SOUND;
Intent notificationIntent = new Intent(pContexto, pContexto.getClass());
PendingIntent contentIntent = PendingIntent.getActivity(pContexto, 0, notificationIntent, 0);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.setLatestEventInfo(pContexto, pTitulo, pTexto, contentIntent);
mNotificationManager.notify(1, notification);
}
works perfect, the problem is that pressing on the notification opens the activity that created the notification and that's wrong, I think the notiifcacion activity should not open when I select the notification.
Why? there any way to fix this?
I do not want to open any activity when I select the notification.
thanks from now.
In order to have no action taken when clicking the notification, you may set an empty Intent as follows:
Intent notificationIntent = new Intent() ;
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.contentIntent = contentIntent;
Intent notificationIntent = new Intent(pContexto, pContexto.getClass());
i guess this is the line you have to change with
Intent notificationIntent = new Intent(pContexto, yourClass.class);
Related
I'm trying to create a simple notification for a music player that has 3 buttons, Pause, Previous and Next. My code only shows one of them which is Next. Any suggestions where I did a mistake?
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
Intent playNextIntent = new Intent(this, BroadcastReceiver.class);
playNextIntent.setAction("NEXT_ACTION");
PendingIntent nextPendingIntent = PendingIntent.getBroadcast(this, 1, playNextIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Intent pauseIntent = new Intent(this, BroadcastReceiver.class);
pauseIntent.setAction("PAUSE_ACTION");
PendingIntent pausePendingIntent = PendingIntent.getBroadcast(this, 2, pauseIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Intent playPreviousIntent = new Intent(this, BroadcastReceiver.class);
playPreviousIntent.setAction("PREVIOUS_ACTION");
PendingIntent prevPendingIntent = PendingIntent.getBroadcast(this, 3, playPreviousIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, "D")
.setSmallIcon(R.drawable.ic_launcher_background) //notification icon
.setContentTitle("Simple Music Player")
.setContentText("Currently playing: " + mediaPlayerHolder.getSongsList().get(songIterator).getSongName())
.setPriority(NotificationCompat.PRIORITY_MAX)
.setWhen(0)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setOngoing(true) //user can't remove notification
.addAction(R.drawable.ic_previous, "Previous", prevPendingIntent) // #0
.addAction(R.drawable.ic_pause, "Pause", pausePendingIntent) // #1
.addAction(R.drawable.ic_next, "Next", nextPendingIntent) // #2
.setStyle(new NotificationCompat.BigTextStyle())
.setContentIntent(pendingIntent); //on click go to app intent
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
// notificationId is a unique int for each notification that you must define
notificationManager.notify(1, mBuilder.build()); //show notification
There's nothing wrong with your code. If you run your code in an emulator on stock android the actions show up as expected.
On Android displaying notifications is done by the launcher. Like any other app launchers can contain bugs. In this case there is a bug in the launcher you're using to display multiple actions in a notification.
I recommend you to try a google sample Universal Music player Here is a GitHub link . It includes all of you want in notifications and it has a latest notification Android Oreo features just try out it .
I have the following code in an android app. What it currently does is, at the specified time passed with the Calendar when variable, it opens up the RunningActivity (which is blank), vibrates, and sends a notification. Even if I've pressed the home button and it's running in the background, it starts a new blank RunningActivity and vibrates and sends a notification. I'm trying to figure out how to do all the stuff in the RunningActivity (Vibrate and send a notification) without opening up the blank RunningActivity, allowing the application to stay in the background.
I do NOT need help with actually calling the notification or vibration. I just need to know how to run the actions in the RunningActivity onCreate at a specific time witout opening/showing the RunningActivity. As seen below, I setup a PendingIntent with an AlarmManagager, the issue is that it is launching an Activity and showing it when all I want it to do is vibrate/send a notification.
public void startAlarm(Activity activity, Calendar when){
currentTimerHour = when.get(Calendar.HOUR);
currentTimerMin = when.get(Calendar.MINUTE);
Intent intent = new Intent(activity, RunningActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(activity, 12345, intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager am = (AlarmManager)activity.getSystemService(Activity.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, when.getTimeInMillis(), pendingIntent);
}
RunningActivity Class:
public class RunningActivity extends Activity {
#Override
public void onCreate(Bundle bundle){
super.onCreate(bundle);
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(500);
NotificationCompat.Builder mBuilder;
mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("My notification")
.setContentText("Hello World!");
Intent resultIntent = new Intent(this, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, mBuilder.build());
}
}
To send Notification From Background:
public void createNotification(Context context) {
// Prepare intent which is triggered if the
// notification is selected
Intent intent = new Intent(context,Myexample.class);
PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, 0);
Notification noti = new Notification.Builder(context)
.setContentTitle("My Title")
.setContentText("My message.")
.setSmallIcon(R.drawable.app_icon)
.setContentIntent(pIntent).build();
#SuppressWarnings("static-access")
NotificationManager notificationManager =
(NotificationManager)context.getSystemService(context.NOTIFICATION_SERVICE);
// Hide the notification after its selected
noti.flags |= Notification.FLAG_AUTO_CANCEL;
noti.flags |= Notification.FLAG_SHOW_LIGHTS;
notificationManager.notify(0, noti);
}
You can use service. Add your vibrator class on Service and call it when you need need it. You can also create a method on your class and call it when you need it.
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(500);
I tried to create a notification that starts a activity by clicking on it and which you can't swipe away.
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("DroidSchool")
.setContentText("DroidSchool l\u00E4uft im Hintergrund...");
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, Intent.FLAG_FROM_BACKGROUND);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int mId = 1234567890;
mNotificationManager.notify(mId, mBuilder.build());
with the code above the notification gets displayed, but nothing happens when I click on it and you can swipe it away.
To keep the Notification, in that way the user can't click it away in any way, add this flag
Notification mNotification = mBuilder.build();
notification.flags = Notification.FLAG_NO_CLEAR;
mNotificationManager.notify(mId, notification);
To start an Activity you have to use this flag for your Intent
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
Do like this:
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("DroidSchool")
.setContentText("DroidSchool l\u00E4uft im Hintergrund...");
Intent intent = new Intent(YourActivity.this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
Notification noti = mBuilder.build();
noti.flags |= mBuilder.build().FLAG_NO_CLEAR | mBuilder.build().FLAG_ONGOING_EVENT;
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(0, noti);
You want a foreground service as detailed here
It's worth noting that only android 4.3 added the persistent notification you seem to be after and it can be overriden by the users settings.
try using like this
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, Intent.FLAG_UPDATE_CURRENT);
in MainActivity.java
Dont not consume your notification; ie instead of calling below 2 line code in oncreate() call under onDestro()
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nm.cancel(mId);
You can use NotificationCompat.Builder's .setOngoing(true), use it like this:
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setAutoCancel(true)
.setContentTitle("Downloading Something")
.setContentText("Downloading")
.setSound(null)
.setDefaults(0)
.setOngoing(true);
// Makes it so the notification is clickable but not destroyable by swiping-away or hitting "clear all"
I have a clickable notification, that opens my application when clicked. Howewer, old activity isn't deleted, so, when I close app, there's the same activity under previous...hope you understand.
So, how can I delete old activity?
The code below is situated in service. And I need to destroy Main activity.
void sendNotif() {
Intent in = new Intent(this, MainActivity.class);
PendingIntent pin = PendingIntent.getActivity(this, 0, in, 0);
Notification notif = new Notification.Builder(this)
.setContentTitle("App launched")
.setContentText("Press to open app")
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pin)
.build();
notif.flags |= Notification.FLAG_NO_CLEAR;
nm.notify(1, notif);
}
Kotlin:
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK)
val pendingIntent = PendingIntent.getActivity(applicationContext, 0,
intent, PendingIntent.FLAG_CANCEL_CURRENT)
Why don't you just add a flag to your Intent when you create your PendingIntent like this:
Intent in = new Intent(this, getClass()).addFlags(Intent.FLAG_CANCEL_CURRENT));
PendingIntent pin = PendingIntent.getActivity(this, 0, in, 0);
Try adding these flags to your intent.
in.setFlags(FLAG_ACTIVITY_CLEAR_TASK | FLAG_ACTIVITY_NEW_TASK);
See this for explanation.
onPause()
{
MainActivity.this.finish();
}
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
OnMessage i want to open Google Play android
When I use the below code in Android 4.0 (ICS) it doesn't work:
NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher, "Testomg", System.currentTimeMillis() + 5000);
Intent notificationIntent = new Intent(Intent.ACTION_VIEW);
notificationIntent.setData(Uri.parse("market://details?id=com.karya.kot"));
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent intent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, 0);
notification.setLatestEventInfo(getApplicationContext(), "Google Play", "Download app", intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
But it's works for lower versions of Android.
For Android ICS, I want to use Notification.Builder, but then what should I use for lower versions?
Below is full working code for me on any version of the android 2.2 and above
private static void generateNotification(Context context, String message) {
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(context, <nameofactivity>.class);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent =
PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Play default notification sound
notification.defaults |= Notification.DEFAULT_SOUND;
// Vibrate if vibrate is enabled
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);
}