【发布时间】:2017-07-03 17:26:56
【问题描述】:
我开发了一个具有 Firebase 推送通知功能的应用程序。 我创建了一个 MainActivity 和五个片段 A、B、C、D、E。 我想显示片段 C,当我点击推送通知消息时,当我的应用程序在屏幕上打开时它工作正常。
但是当我杀死应用程序然后推送通知时,我点击了推送通知,MainActivity 加载 Fragment A,这是我使用 MainActivity 的初始 Fragment。
我的应用流程:
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
主Activity没有hasExtra(ARG_NOTIFICATION_FOR)
【问题讨论】:
-
get
intentinoncreateofMainActivity并检查它是否为空,如果不为空则设置viewPager.setCurrentItem(C);
标签: android android-activity push-notification fragment