I create notification with action button. When clicking on action button, broadcast receiver is called. I am passing the notification ID in the intent
In the broadcast receiver I do the following
int notifId = intent.getIntExtra(Constants.NOTIF_ID, 0);
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.cancel(notifId);
This is how I generate the notification
int notifId = Util.random.nextInt(9000);
Intent mIntent = new Intent(con, NotificationBroadcastReceiver.class);
mIntent.putExtra(Constants.NOTIF_CODE, codeReason);
mIntent.putExtra(Constants.NOTIF_ID, notifId);
PendingIntent mPendingIntent = PendingIntent.getBroadcast(con, 0, mIntent , 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(con)
.setSmallIcon(R.drawable.icon)
.setContentTitle("test")
.setPriority(Notification.PRIORITY_MAX)
.setAutoCancel(true)
.addAction(R.drawable.ic_launcher, "action", mPendingIntent);
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(con, MainActivity.class);
PendingIntent resultPendingIntent = PendingIntent.getActivity(
con, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) con.getSystemService(Context.NOTIFICATION_SERVICE);
mBuilder.setDefaults(
Notification.DEFAULT_SOUND |
Notification.DEFAULT_VIBRATE |
Notification.DEFAULT_LIGHTS
);
// mId allows you to update the notification later on.
mNotificationManager.notify(notifId, mBuilder.build());
However the notification does not get hidden/dismissed although I know I am hitting the code ( using log statements).
Why is that?
I found the answer. I "think" that due to the 2 pending intents having the same con and req code, they are ending up modifying their intent values. I fixed it by using 2 different request codes to ensure unique Pending Intents . Therefore , I got the same notifId
Check your id if it's zero.
Check the notification is not binded with other Service by startForeground().
Related
Nothing happens on clicking Accept or Reject button of the head-ups notification.
But when the head-up notification disappear and from clicking Accept and Reject from the notification panel is working.
Testing on Android 5.1.0.
Intent acceptIntent = new Intent(this, NotificationReceiver.class);
acceptIntent.setAction("com.android.test.Accept");
PendingIntent acceptPendingIntent = PendingIntent.getBroadcast(TestApplication.getAppContext(), 12345, acceptIntent, PendingIntent.FLAG_CANCEL_CURRENT);
Intent rejectIntent = new Intent(this, NotificationReceiver.class);
rejectIntent.setAction("com.android.test.Reject");
PendingIntent rejectPendingIntent = PendingIntent.getBroadcast(TestApplication.getAppContext(), 12345, rejectIntent, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(R.drawable.fundu);
builder.setContentTitle("Test Notification");
builder.setContentText("Hello");
builder.setAutoCancel(true);
builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
builder.setCategory(NotificationCompat.CATEGORY_SERVICE);
builder.setDefaults(NotificationCompat.DEFAULT_ALL);
builder.setPriority(NotificationCompat.PRIORITY_MAX);
builder.addAction(R.drawable.ic_check_icon, "Accept", acceptPendingIntent);
builder.addAction(R.drawable.ic_action_close, "Reject", rejectPendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, builder.build());
Just setting the vibration, makes it working fine.
builder.setVibrate(new long[0]);
The key is to call setContentIntent on the Notification Builder and passing it a PendingIntent. The Full code with comments explaining each step is included below. See the part named "THIS IS THE PERTINENT PART" (The Full code is included for completeness sake.)
// Use Notification Builder to start things off
// There are other ways of acquiring a Notification Builder; this is just an example
String channelId = "channelId";
String title = "title";
String body = "body";
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, channelId);
notificationBuilder
.setSmallIcon(R.drawable.ic_alarm)
.setContentTitle(title)
.setContentText(body);
//--------------- THIS IS THE PERTINENT PART ---------------
// Prepare Intent for creating PendingIntent
Intent intent = new Intent(context, ActivityToStart.class);
// Create Pending Intent
int requestCode= 1234; // requestCode has to be a unique ID for EACH PendingIntent
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addNextIntent(intent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(
requestCode,
PendingIntent.FLAG_UPDATE_CURRENT // use to prevent re-using current Activity Intent
);
notificationBuilder.setContentIntent(pendingIntent);
// Finally, create the Notification
Notification notification = notificationBuilder.build();
I sent some notification:
NotificationCompat.Builder mBuilder = new
Intent resultIntent = new Intent(this, AutoEdviserActivity.class);
resultIntent.putExtra("id",i);
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
this,
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
int mNotificationId = i+900;
NotificationManager mNotifyMgr =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotifyMgr.notify(mNotificationId, mBuilder.build());
With flag PendingIntent.FLAG_UPDATE_CURRENT, when I see more one notification and push those which I get early, in AutoEdviserActivity I getExtra from last notification.
Another flags as I understand, unsuitable to: FLAG_ONE_SHOT, FLAG_NO_CREATE, FLAG_CANCEL_CURRENT
How can I get different Extra from different Notifications?
I receive push notifications with data(intent). If I received two or more notifications with different ids, but open one Activity and ids are same. For example, I receive three notifications with different id=1,2,3. But when Activity is started use one id = 3. When I click first or second notification with ids 1 and 2, open Activity with id 3.Can you help understand my mistake in the code?
NOTIFICATION_ID ++;
mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(msg.getString("title"))
.setContentText(msg.getString("message"))
.setDefaults(Notification.DEFAULT_SOUND)
.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(Picasso.with(getApplicationContext()).load(msg.getString("icon")).get()).setSummaryText(msg.getString("message")))
.setAutoCancel(true);
Log.e("msg---",msg.toString());
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setComponent(new ComponentName(this, ActivityDetail.class));
// intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
// intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.putExtra(Keys._PostId,msg.getString("id"));
intent.putExtra(Keys._Image, msg.getString("icon"));
intent.putExtra(Keys._PostType, msg.getString("post_type"));
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
Create your PendingIntent like this and do a trick
PendingIntent contentIntent = PendingIntent.getActivity(this, (int) (Math.random() * 100), intent, PendingIntent.FLAG_UPDATE_CURRENT);
My app is getting called by an intent that is passing information(pendingintent in statusbar) but when I hit the home button and reopen my app by holding the home button it calls the intent again and the same extras are still there...
is there any way to clear the intent? or check whether it has been used before?
Intent intent = new Intent(context, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("reportID", reportID);
intent.putExtra("message", message);
intent.putExtra("toast_show", true);
intent.putExtra("detail_open", true);
intent.putExtra("SplashActivity", true);
PendingIntent pendingIntent = PendingIntent
.getActivity(context.getApplicationContext(), 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT
| PendingIntent.FLAG_ONE_SHOT);
// Intent.FLAG_ACTIVITY_NEW_TASK
/* get the system service that manage notification NotificationManager */
NotificationManager notificationManager = (NotificationManager) context
.getApplicationContext().getSystemService(
Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(
context.getApplicationContext())
.setWhen(when)
.setContentText(message)
.setContentTitle(title)
.setSmallIcon(smalIcon)
.setAutoCancel(true)
.setTicker(message)
.setLargeIcon(largeIcon)
.setDefaults(
Notification.DEFAULT_LIGHTS
| Notification.DEFAULT_VIBRATE
| Notification.DEFAULT_SOUND)
.setContentIntent(pendingIntent);
/* Create notification with builder */
Notification notification = notificationBuilder.build();
notificationManager.notify(0, notification);
The problem starts when I receive a notification. If I start the app from the notification, it starts the correct screen and I can use the app => ok. But when I quit the app (with home or back button) it do not appears anymore in the "recent app" list. More precisely:
Does anyone knows how to keep the app in the "recent app" list after being started form a notification?
Update this line and try
PendingIntent pendingIntent = PendingIntent.getActivity(context, (int)(Math.random() * 100),intent,PendingIntent.FLAG_UPDATE_CURRENT);
What you need is the PendingIntent.FLAG_CANCEL_CURRENT :
If the described PendingIntent already exists, the current one is canceled before generating a new one. You can use this to retrieve a new PendingIntent when you are only changing the extra data in the Intent; by canceling the previous pending intent, this ensures that only entities given the new data will be able to launch it.
PendingIntent pendingIntent = PendingIntent.getActivity(
context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
This will ensure that your latest data will pe present on your Intent.
I am trying to create notifications correctly, but as I can not perform an action when you click on the notification. I hope you can help me or give some clue.
It is recommended to use Notification.Builder and NotificationCompat.Builder from the support library for building notifications.
Also why do use reuse the Intent which was caught by your BroadcastReceiver? It should refer to the actual activity which you want to launch by click.
For example:
Intent intent = new Intent(context, MainActivity.class);
Notification notification = new NotificationCompat.Builder(context)
.setContentTitle(contentTitle)
.setContentText(contentText)
.setContentIntent(PendingIntent.getActivity(context, 0, intent, 0))
.setAutoCancel(true)
.build();
If you doesn't really want to launch some activity, you may start a Service or send a broadcast message instead. See PendingIntent.
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notif = new Notification(R.drawable.ic_launcher, "Text",
System.currentTimeMillis());
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("somekey", "someextra");
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
notif.setLatestEventInfo(this, "Title", "Message", pIntent);
notif.flags |= Notification.FLAG_AUTO_CANCEL;
nm.notify(1, notif);
Use Notification.Builder for new version api