我使用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)
得到'intent'在'oncreate' MainActivity'的'並檢查它是否是空或不是,如果它不爲空,然後設置' viewPager.setCurrentItem(C);' –