Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pending intent in notification not working

Below is my block of code which should open NotificationActivity when the notification is tapped on. But its not working.

private void setNotification(String notificationMessage) {     Uri alarmSound = getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);     mNotificationManager  = getApplication().getSystemService(Context.NOTIFICATION_SERVICE);      Intent notificationIntent = new Intent(getApplicationContext(), NotificationActivity2.class);      PendingIntent contentIntent = PendingIntent.getActivity(this, 0,notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);      NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext())     .setSmallIcon(R.drawable.logo)     .setContentTitle("My Notification")     .setStyle(new NotificationCompat.BigTextStyle()     .bigText(notificationMessage))     .setContentText(notificationMessage).setAutoCancel(true);     mBuilder.setSound(alarmSound);     mBuilder.setContentIntent(contentIntent);     mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());  } 
like image 438
prashantwosti Avatar asked Jan 02 '14 10:01

prashantwosti


People also ask

How does Pending Intent work?

A PendingIntent itself is simply a reference to a token maintained by the system describing the original data used to retrieve it. This means that, even if its owning application's process is killed, the PendingIntent itself will remain usable from other processes that have been given it.

Which intent is employed for generating notification in Android?

MainActivity. The NotificationManager. notify() method is used to display the notification. The Intent class is used to call another activity (NotificationView. java) on taping the notification.

How do I make my Android notifications clickable?

1 Answer. Show activity on this post. setContentIntent(PROVIDE_YOUR_PENDING_INTENT); You will need to provide a pending intent to where you want to redirect the user when he click on the notification.


1 Answers

try this:

private void setNotification(String notificationMessage) {  //**add this line** int requestID = (int) System.currentTimeMillis();  Uri alarmSound = getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); mNotificationManager  = getApplication().getSystemService(Context.NOTIFICATION_SERVICE);  Intent notificationIntent = new Intent(getApplicationContext(), NotificationActivity2.class);  //**add this line** notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);   //**edit this line to put requestID as requestCode** PendingIntent contentIntent = PendingIntent.getActivity(this, requestID,notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);  NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext()) .setSmallIcon(R.drawable.logo) .setContentTitle("My Notification") .setStyle(new NotificationCompat.BigTextStyle() .bigText(notificationMessage)) .setContentText(notificationMessage).setAutoCancel(true); mBuilder.setSound(alarmSound); mBuilder.setContentIntent(contentIntent); mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());  } 
like image 72
droidx Avatar answered Sep 20 '22 12:09

droidx