0

I am using the FirebaseMessagingService for getting notifications and opening the app upon clickng the notification. But everytime i click the notification the app opens the MainActivity instead of the intended ResultActivity. I also followed docs from the PendingIntent docs and still does the same.

    private void createNotification( String messageBody) {
        Intent intent = new Intent( this , ResultActivity.class );

        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
        PendingIntent resultIntent = PendingIntent.getActivity( this , 0, intent,
                PendingIntent.FLAG_CANCEL_CURRENT);
//        PendingIntent resultPending = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

        Uri notificationSoundURI = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder mNotificationBuilder = new NotificationCompat.Builder( this)
                .setContentTitle("VERA")
                .setContentText(messageBody)
                .setAutoCancel( true )
                .setSound(notificationSoundURI)
                .setContentIntent(resultIntent);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);


        notificationManager.notify(0, mNotificationBuilder.build());
    }

Here is my Manifest.

<activity
    android:name=".MainActivity"
    android:label="@string/app_name">
    <intent-filter>
        <action android:name="android.intent.action.MAIN"/>
        <category android:name="android.intent.category.LAUNCHER"/>
    </intent-filter>
</activity>
    <activity android:name=".ResultActivity"
        android:launchMode="singleTask"
        android:excludeFromRecents="true"
        android:taskAffinity=""></activity>

EDIT: I tried to pass some extra strings, but the main activity is not even receiving anything. Is it possible that the notif is only triggering its default method to launch the app?

leenolasco
  • 75
  • 10
  • where is your `startActivity(intent)`? – John Joe May 18 '18 at 01:56
  • isnt the pending intent the trigger? i am trying to open it from notification. – leenolasco May 18 '18 at 02:02
  • `intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);` – John Joe May 18 '18 at 02:14
  • https://stackoverflow.com/a/13716784/5156075 – John Joe May 18 '18 at 02:14
  • still showing the mainactivity not the resultactivity – leenolasco May 18 '18 at 02:21
  • 1
    Make sure you don't use the "notification" body when generating the notification via fcm api, see here: https://stackoverflow.com/a/39570727/5315490 – Alvin Rusli May 18 '18 at 03:06
  • i am not using any payload or datapayload. my problem is with the notification trigerrng the main activity. it should not trigger the main activity – leenolasco May 18 '18 at 03:30
  • How do you generate the firebase notification? If you use the FCM console, it will always be a notification message, which defaults to opening the application's main activity – Alvin Rusli May 18 '18 at 04:06
  • I've read some posts and apparently i cannot open non launcher activity when my app is in background. Thanks for the inputs. BTW what do you mean "dont use notification body"? Is it in the fcm console? – leenolasco May 18 '18 at 05:06
  • 1
    @leenolasco See my previous link (https://stackoverflow.com/a/39570727/5315490). Try checking this too https://firebase.google.com/docs/cloud-messaging/http-server-ref – Alvin Rusli May 18 '18 at 05:13
  • Thanks for the docs. Apparently i am only using the console to send notifications, so i am unable to use the data message to trigger the onMessageReceived function. – leenolasco May 18 '18 at 05:43

2 Answers2

1

Create an Intent that starts the Activity.

Set the Activity to start in a new, empty task by calling setFlags() with the flags FLAG_ACTIVITY_NEW_TASK and FLAG_ACTIVITY_CLEAR_TASK.

Create a PendingIntent by calling getActivity().

Like,

Intent notifyIntent = new Intent(this, ResultActivity.class);
    // Set the Activity to start in a new, empty task
    notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | 
Intent.FLAG_ACTIVITY_CLEAR_TASK);
    // Create the PendingIntent
    PendingIntent notifyPendingIntent = PendingIntent.getActivity(this, 0, 
notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);

Then you can pass the PendingIntent to the notification as usual:

NotificationCompat.Builder mNotificationBuilder= new NotificationCompat.Builder(this, "CHANNEL_ID");
builder.setContentIntent(notifyPendingIntent);
...
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(0, mNotificationBuilder.build());
0

Apparently there is no way to trigger the onMessageReceived by using the FCM console only. It will only be triggered if i use other ways to send data messages.

leenolasco
  • 75
  • 10