2017-07-03 19 views
1

Firebaseプッシュ通知機能を使用して1つのアプリケーションを開発します。 MainActivityと5つのフラグメントA、B、C、D、Eを作成しました。 フラグメントCを表示したいとき、プッシュ通知メッセージをクリックすると、アプリケーションが画面上で開いているときに正常に動作します。アクティビティとフラグメントを含むハンドルプッシュ通知ナビゲーション

しかし、私はアプリを殺してから通知を押すと、MainActivityのMainActivityをクリックしてMainActivityの最初の断片であるMainActivityの読み込み断片Aをクリックしました。

マイアプリの流れ:

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

マイコード:私はプッシュ通知アイコンをクリックすると、アプリケーションは、スプラッシュ画面から開きます

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

:MainActivityについては

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

次のようにサイレントログイン - >メインアクティビティ - >ロードフラグメントA

主な活動はhasExtra(ARG_NOTIFICATION_FOR)

+0

はMainActivity' 'の' oncreate'で 'intent'を取得し、それがnullだかいないかどうかを確認、それがnullでない場合は、'設定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, 
    }}