【问题标题】:Handle Push Notification Navigation with Activity and Fragment使用 Activity 和 Fragment 处理推送通知导航
【发布时间】: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 intent in oncreate of MainActivity 并检查它是否为空,如果不为空则设置viewPager.setCurrentItem(C);

标签: android android-activity push-notification fragment


【解决方案1】:

您的推送通知的待处理意图需要向活动发送一个参数,以确定应用程序是从图标还是通知打开的。

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

【讨论】:

  • 这太棒了
【解决方案2】:

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

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

payloaddata 的示例如下:-

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-25
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多