How to get different extra from different Notification - android

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?

Related

show multiple messages in single notification

how to get multiple messages in a single notification .This is my following snippet
NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(context);//
Intent nIntent = new Intent(context, ConvActivity.class);
PendingIntent nPendingInten = PendingIntent.getActivity(context, 0, nIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
nBuilder.setSmallIcon(R.drawable.app_icon_indietext_new);
nBuilder.setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.app_icon_indietext_new));
nBuilder.setContentTitle(senderNum);
nBuilder.setContentText(message);
nBuilder.setStyle(new NotificationCompat.InboxStyle()
.addLine(message)
.addLine(message))
.setGroup(GROUP_KEY_EMAILS)
.setGroupSummary(true)
.build();
TaskStackBuilder nStackBuilder = TaskStackBuilder.create(context);
nStackBuilder.addParentStack(ConvActivity.class);
nStackBuilder.addNextIntent(nIntent);
nBuilder.setContentIntent(nPendingInten);
NotificationManager nNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
nBuilder.setAutoCancel(true);
//nNotificationManager.notify(0, nBuilder.build());
nNotificationManager.notify(9999,nBuilder.build());
what is the procedure to get multiple notifications
You just have to use different IDs
mNotificationManager.notify(ID, notifyDetails);
and set multiple messages in those ID's

How can I catch the click on notification?

I need to call some methods after clicking the notification. My notification has a pendingIntent that opens a browser. But I also have to do some other things AFTER user clicks it. Oh, and I must admit that I get my notifications from GCM(push).
Intent intent;
intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(urlString));
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(mBitmap)
.setContentTitle(title)
.setDefaults(Notification.DEFAULT_ALL)
.setContentText(body)
.setAutoCancel(true);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
Notification myNotification = notificationBuilder.build();
myNotification.setLatestEventInfo(getBaseContext(), title, body, pendingIntent);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(TAG, myNotification);
To handle the click on the notification itself (not an action) you need to specify a ContentIntent as following:
notificationBuilder.setContentIntent(PendingIntent.getActivity(
context,
0,
new Intent(context, YourActivity.class)
.putExtra("extra_something", something)
.setData(Uri.parse("pass an uri if needed")),
PendingIntent.FLAG_UPDATE_CURRENT
))
Hope it helps.

Click event not working on Heads-up notification

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();

Notification is not cancelled after calling notification.cancel

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().

android notification click not working

I am using this code inside a service in order to get a notification if I have any new alerts but when I click on them I'm not getting to the view that I want:
if(newAlertCounter > 0) // alert user about new alerts
{
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_action_warning)
.setContentTitle(newAlertCounter + " new flood alerts!")
.setContentText("Tap to see where in your vecinity.");
// Sets an ID for the notification
int mNotificationId = 001;
// Gets an instance of the NotificationManager service
NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Builds the notification and issues it.
mNotifyMgr.notify(mNotificationId, mBuilder.build());
// notification click action
Intent notificationIntent = new Intent(this, ViewAlertsActivity.class);
PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
}
It shows up but its not clickable, so whats wrong with this?
put
mNotifyMgr.notify(mNotificationId, mBuilder.build());
after
mBuilder.setContentIntent(resultPendingIntent);
Move your
mNotifyMgr.notify(mNotificationId, mBuilder.build());
after
mBuilder.setContentIntent(resultPendingIntent);
When you call .build() you create the notification without the content intent. (and no, it will not be added after because the object which will be sent to notification system will be the Notification not the Builder)
if(newAlertCounter > 0) // alert user about new alerts
{
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_action_warning)
.setContentTitle(newAlertCounter + " new flood alerts!")
.setContentText("Tap to see where in your vecinity.");
// Sets an ID for the notification
int mNotificationId = 001;
// Gets an instance of the NotificationManager service
NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Builds the notification and issues it.
// notification click action
Intent notificationIntent = new Intent(this, ViewAlertsActivity.class);
PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
mNotifyMgr.notify(mNotificationId, mBuilder.build());
}

Categories

Resources