leenolasco
leenolasco

Reputation: 75

Pending Intent opens wrong activity

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?

Upvotes: 0

Views: 1323

Answers (2)

leenolasco
leenolasco

Reputation: 75

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.

Upvotes: 0

Dnyaneshwar Panchal
Dnyaneshwar Panchal

Reputation: 444

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

Upvotes: 1

Related Questions