Pending intent in notification not working

Viewed 56061

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

}
6 Answers

Try checking your AndroidManifest.xml, double check if you are navigating to an activity with an intent-filter main action, launcher category

For example,

enter image description here

I couldn't make my pending intent to navigate to HomeActivity but it works when navigating to SplashScreen

Hope this helps.

If the app is already running then you need to handle it in onNewIntent

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);

    //Handle intent here...
}

Added the requestid, but the intent was still not opening.

This is the solution that worked for me:

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

Setting those flags to clear the activities below the intent activity.

Related