Ad

2nd Push Notification Doesn't Open My App

I'm using FCM Cloud Messaging service and I'm able to generate a few notification at once from the server. But the only problem is that, I'm able to open up the first notification, but starting from the 2nd until the remaining notifications just won't do anything when I clicked on them.

Messaging Service Code :

public class MyFirebaseMessagingService extends FirebaseMessagingService {

private final String NOTIFICATION_TYPE = "type";
private final String NOTIFICATION_ID = "id";
private final String NOTIFICATION_TYPE_PRODUCT_DETAIL = "productdetail";

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);

    String title = remoteMessage.getNotification().getTitle();
    String body = remoteMessage.getNotification().getBody();

    String token = remoteMessage.getFrom();
    Log.d("FireBase TAG: ", token);

    // Check if message contains a data payload.
    if (remoteMessage.getData().size() > 0) {
        Log.d("FireBaseMessageService","FireBase Data payload : " + remoteMessage.getData());
    }

    // Check if message contains a notification payload.
    if (remoteMessage.getNotification() != null) {
        Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
    }



}


@Override
public void handleIntent(Intent intent) {

    String type = intent.getExtras().getString(NOTIFICATION_TYPE, "");

    int id = 0;
    try {
        id = Integer.valueOf(intent.getExtras().getString(NOTIFICATION_ID, ""));
    } catch (NumberFormatException e) {
        e.printStackTrace();
    }
    //Intents
    Intent mainIntent = MainActivity.newIntent(this);
    Intent editProfileIntent = EditProfileActivity.newIntent(this);
    Intent settingsIntent = SettingsActivity.newIntent(this);
    Intent productIntent = ProductActivity.newNotificationIntent(this, id, false, true);


    if (UserManager.getSingleton().isUserLoggedIn(getApplicationContext())) {

        PendingIntent pendingIntent;

        if (type.equalsIgnoreCase(NOTIFICATION_TYPE_PRODUCT_DETAIL)) {
            TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
            stackBuilder.addParentStack(MainActivity.class);
            stackBuilder.addNextIntent(mainIntent);
            stackBuilder.addNextIntent(productIntent);

            editProfileIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_ONE_SHOT);

        } else if (type.isEmpty() || type.equals("")){
            pendingIntent = PendingIntent.getActivity(this, 0, mainIntent,
                    PendingIntent.FLAG_ONE_SHOT);
        } else {

            TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
            stackBuilder.addParentStack(MainActivity.class);
            stackBuilder.addNextIntent(mainIntent);
            stackBuilder.addNextIntent(productIntent);

            pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_ONE_SHOT);


        }

        Bitmap icon = BitmapFactory.decodeResource(getResources(),
                R.drawable.onerm_logo_rounded);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.oneringgit_transparent)
                .setLargeIcon(icon)
                .setContentTitle(intent.getExtras().getString("gcm.notification.title"))
                .setContentText(intent.getExtras().getString("gcm.notification.body"))
                .setDefaults(NotificationCompat.DEFAULT_VIBRATE)
                .setPriority(NotificationCompat.PRIORITY_MAX)
                .setContentIntent(pendingIntent)
                .setAutoCancel(true)
                .setColor(getBaseContext().getResources().getColor(R.color.colorPrimary));

        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        manager.notify(id, builder.build());
    }
}

I suspect it has something to do with the "id" variable I put into the manager.notify () method.

Is there a way through this?

Ad

Answer

yes change the id, since it is overriding the previous notification, so nothing will open, change it to this:

 int num = (int) System.currentTimeMillis();
  manager.notify(num, builder.build())

change the id in the pending intent also:

 pendingIntent = PendingIntent.getActivity(this, num, mainIntent, PendingIntent.FLAG_ONE_SHOT);
Ad
source: stackoverflow.com
Ad