1

I'm trying to create an notification to launch an Activity with extra information. However, currently it is not working.

Here is the code for creating the notification

private void showNotification(RemoteMessage remoteMessage){
        try{
            // Create an explicit intent for an Activity in your app
            Intent i = new Intent(getBaseContext(), MainActivity.class);
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
            i.putExtra(EXTRA_MESSAGE,remoteMessage);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, i, 0);

            NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
                    .setSmallIcon(R.drawable.ic_action_name)
                    .setContentTitle("Title")
                    .setContentText("Content")
                    .setPriority(NotificationCompat.PRIORITY_HIGH)
                    .setCategory(NotificationCompat.CATEGORY_CALL)
                    .setAutoCancel(true)
                    // Set the intent that will fire when the user taps the notification
                    .setFullScreenIntent(pendingIntent, true);



            // notificationId is a unique int for each notification that you must define
            notificationId++;
            //https://developer.android.com/training/notify-user/time-sensitive
            // Provide a unique integer for the "notificationId" of each notification.
            startForeground(notificationId, builder.build());
        }catch (Exception e){
            Log.d(TAG, e.getMessage());
        }


    }

The Activity is launching after clicking the notification. However, inside the onCreate of the Activity when checking for the extra in the bundle it doesn't find it:

EDIT: Actually what I want is for the activity to show without the user having to click anything hence why I am using setFullScreenIntent.

    if(bundle!=null && bundle.containsKey(CustomFirebaseMessagingService.EXTRA_MESSAGE)){
      Log.d(TAG, "MainActivity has extra message");
      
    }else{
      Log.d(TAG, "MainActivity doesn't have extra message");
    }

My logs say MainActivity doesn't have extra message

Joshua Augustinus
  • 1,468
  • 3
  • 15
  • 28

2 Answers2

1

Try

setContentIntent(pendingIntent)

Since you want an intent that will fire on the notification click, the docs for this method says:

Supply a PendingIntent to send when the notification is clicked.

  • Actually I want the Activity to launch as soon as that code is run ideally. If the user doesn't have to click a notification that is even better. – Joshua Augustinus Sep 01 '20 at 20:53
  • Hey your solution kinda worked. Now if the app is open and I get the notification and press it the Activity opens with the extra message. However, I don't get any notification or anything when the app is closed. – Joshua Augustinus Sep 01 '20 at 21:06
  • @Josh Then maybe try adding the flag PendingIntent.FLAG_UPDATE_CURRENT as the last parameter of your pending intent. Make sure to build the notification with setFullScreenIntent set. Also, if you're creating the notification from a push notification take a look [here](https://stackoverflow.com/questions/42273699/how-to-stack-firebase-cloud-messaging-notifications-when-the-application-is-not/43914028#43914028) – João Pucci Sep 02 '20 at 01:20
1

You need to set the flag FLAG_UPDATE_CURRENT when creating the PendingIntent:

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, i,
        PendingIntent.FLAG_UPDATE_CURRENT);

This will ensure that your "extras" are actually added to the PendingIntent.

David Wasser
  • 93,459
  • 16
  • 209
  • 274