简体   繁体   中英

Handle Push Notification Navigation with Activity and Fragment

I develop one application with Firebase Push Notification feature. I created one MainActivity and five fragment A, B, C, D, E. I want to show Fragment C, when i clicked on Push Notification message, Its works fine when my application is open on screen.

But when I kill app and then push notification comes, I clicked on Push Notification, MainActivity load Fragment A which my initial Fragment with MainActivity.

My App Flow:

Splash Screen -> Login Screen (Silent Login) -> Main Activity -> Load a Specific Fragment

My code:

private void sendNotification(String title, String messageBody, int senderId) {
        Intent intent = new Intent(this, MainActivity.class);
        intent.putExtra(ARG_NOTIFICATION_FOR, ConstantValues.NOTIFICATION_FOR_CHAT_SCREEN);
        intent.putExtra(ARG_SENDER_ID, senderId);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0 /* Request code */, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle(title)
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}

For MainActivity as below:

if(getIntent() != null)
{
  if (getIntent().hasExtra(ARG_NOTIFICATION_FOR))
  {
    //Load Fragment C
  }
}

When i clicked on Push Notification icon, App opens from Splash Screen with Silent Login -> Main Activity -> Load Fragment A

Main Activity not have hasExtra(ARG_NOTIFICATION_FOR)

Your pending intent for push notification needs to send a parameter to activity to determine application is opened from icon or notification.

 Intent notificationIntent = new Intent(getApplicationContext(), YourActivity.class);
    notificationIntent.putExtra("FromNotification", true);//or fragment Id
    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingNotificationIntent = PendingIntent.getActivity(getApplicationContext(),notificationIndex,notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notification.setLatestEventInfo(getApplicationContext(), notificationTitle, notificationMessage, pendingNotificationIntent);

You should create your custom notification for this purpose. Checkout this link https://developer.android.com/training/notify-user/build-notification.html
Also inside the onCreate method you should receive data from notification.

protected void onCreate(Bundle bundle){
       super.onCreate(bundle);
       //bla bla bla
       boolean fromNotification = getIntent().getBooleanExtra("FromNotification",false);
       if(fromNotification){
            //open fragment c
       }else{
            //open fragment a
       }
}

Add a data json to payload of your notification. Inside the data , you can pass additional information.

This information can be retrieved using getIntent().getExtras() inside the activity that launches. Following this, you can decide which fragment to display according to the information contained in the notification.

An example of a payload with data is as follows :-

payload = {
  notification: {
    title: `You have a new like on your post!`,
    click_action : 'HANDLE_NOTIFICATION'

  },
  data : {
        liker_uid : xyz,
        post_id : postId,
    }}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM