Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使用活动和片段处理推送通知导航_Android_Android Activity_Push Notification_Fragment - Fatal编程技术网

Android 使用活动和片段处理推送通知导航

Android 使用活动和片段处理推送通知导航,android,android-activity,push-notification,fragment,Android,Android Activity,Push Notification,Fragment,我开发了一个具有Firebase推送通知功能的应用程序。 我创建了一个主活动和五个片段A、B、C、D、E。 我想显示片段C,当我点击推送通知消息时,当我的应用程序在屏幕上打开时,它可以正常工作 但当我杀死应用程序,然后推送通知到来时,我点击推送通知,MainActivity加载片段A,这是我与MainActivity的初始片段 我的应用程序流: Splash Screen -> Login Screen (Silent Login) -> Main Activity -> Lo

我开发了一个具有Firebase推送通知功能的应用程序。 我创建了一个主活动和五个片段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());
}
主要活动如下:

if(getIntent() != null)
{
  if (getIntent().hasExtra(ARG_NOTIFICATION_FOR))
  {
    //Load Fragment C
  }
}
当我点击推送通知图标时,应用程序从初始屏幕打开,无提示登录->主要活动->加载片段A


主活动没有hasExtra(ARG_NOTIFICATION_FOR)

推送通知的挂起意图需要向活动发送一个参数,以确定应用程序是否从图标或通知打开

 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);
您应该为此目的创建自定义通知。签出此链接
在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
       }
}

data
json添加到通知的
payload
。在
数据中
,您可以传递附加信息

可以在启动的活动中使用
getIntent().getExtras()
检索此信息。接下来,您可以根据通知中包含的信息决定显示哪个片段

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
       }
}
带有
数据的
有效载荷
示例如下:-

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

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

MainActivity
oncreate
中获取
intent
,并检查它是否为null,如果它不为null,则设置
viewPager.setCurrentItem(C)这太棒了