2017-07-03 30 views
1

我使用Firebase推送通知功能開發了一個應用程序。 我創建了一個MainActivity和五個片段A,B,C,D,E。 我想顯示片段C,當我單擊推送通知消息時,它的工作正常,當我的應用程序在屏幕上打開時。使用活動和片段處理推送通知導航

但是,當我殺了應用程序,然後推送通知來了,我點擊推送通知,MainActivity加載片段A,我的初始片段與MainActivity。

我的應用流量:

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

我的代碼:

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

對於MainActivity如下:

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

當我點擊推送通知圖標,應用程序從啓動畫面打開帶無聲登錄 - >主要活動 - >加載片段A

主要活動沒有hasExtra(ARG_NOTIFICATION_FOR)

+0

得到'intent'在'oncreate' MainActivity'的'並檢查它是否是空或不是,如果它不爲空,然後設置' viewPager.setCurrentItem(C);' –

回答

3

您的未決意圖推送通知需要發送一個參數到活動,以確定從圖標或通知打開應用程序。

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

您應該爲此創建自定義通知。結賬鏈接https://developer.android.com/training/notify-user/build-notification.html
同樣在onCreate方法中,您應該接收來自通知的數據。

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 
     } 
} 
+0

這太棒了 – ken

0

添加data JSON你通知​​。在data內部,您可以傳遞更多信息。

此信息可以在啓動的活動中使用getIntent().getExtras()檢索。在此之後,您可以根據通知中包含的信息決定顯示哪個片段。

data一個​​的例子如下: -

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

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