Question

I am showing a notification on arrival of new messages and also when a user clicks on the notification an activity should open. Notification part is working properly. When I click on the notification the notification gets cancelled, but activity is not opening not sure why I have this problem?

Here is what I have tried:

Intent notificationIntent = new Intent(context, Sms.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

NotificationManager nm = (NotificationManager)   
context.getSystemService(Context.NOTIFICATION_SERVICE);
Resources res = context.getResources();
Notification.Builder builder = new Notification.Builder(context);
builder.setContentIntent(contentIntent)
.setSmallIcon(R.drawable.newyellowsms)
.setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.smsnotification))
.setTicker(res.getString(R.string.ticker))
.setWhen(System.currentTimeMillis())
.setAutoCancel(true)
.setContentTitle(address)
.setContentText(body);
Notification n = builder.build();
nm.notify(007, n);
Was it helpful?

Solution

Use Intent.FLAG_ACTIVITY_NEW_TASK instead of 0 for flag.

Replace

PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

with

PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top