【问题标题】:Intent extras is null alwaysIntent extras 始终为空
【发布时间】:2019-11-27 08:16:24
【问题描述】:

我正在使用 FCM 进行推送通知,并且我正在将数据从通知发送到我正在打开的活动。

构建通知的代码:

private void handleNotification(RemoteMessage remoteMessage) {
    String notTitle = remoteMessage.getNotification().getTitle();
    String notBody = remoteMessage.getNotification().getBody();

    Intent resultIntent = new Intent(this, HomeActivity.class);
    resultIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);

    resultIntent.putExtra("pushNotClick", "yes");
    resultIntent.putExtra("pushNotHead", ""+notTitle);

    PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
    mBuilder.setSmallIcon(R.drawable.fb_icon);
    mBuilder.setColor(getResources().getColor(R.color.colorPrimary));
    mBuilder.setContentTitle(notBody)
            .setContentText(notTitle)
            .setAutoCancel(true)
            .setContentIntent(resultPendingIntent);
    NotificationManager mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", importance);
        notificationChannel.enableLights(true);
        assert mNotificationManager != null;
        mBuilder.setSmallIcon(R.mipmap.icon_not);
        mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
        mNotificationManager.createNotificationChannel(notificationChannel);
    }
    assert mNotificationManager != null;
    mNotificationManager.notify((int) System.currentTimeMillis() /* Request Code */, mBuilder.build());
}

我在我的活动中得到了额外的意图,如下所示:

String notState = getIntent().getStringExtra("pushNotClick");
String notHead = getIntent().getStringExtra("pushNotHead");

但问题是,每次 Intent Extras 在 Activity 中为空时,我都检查了我在社区中找到的所有可能原因,但每次响应都是一样的。

我已经尝试过下面提到的链接

Intent's extra is always null

Android Notification PendingIntent Extras null

Always getting data null from notification intent android

我不确定我在哪里做错了。

【问题讨论】:

    标签: android push-notification firebase-cloud-messaging


    【解决方案1】:

    即使经过大量调试,我也找不到问题的原因。最后,我改变了向活动发送 Intent 附加内容的方式。现在我正在使用 bundle 发送数据,它的工作就像一个魅力。这是一种方法,而不是问题的答案,所以我不会接受它作为已接受的答案。

    【讨论】:

      【解决方案2】:

      您是否尝试过检查 RemoteMessage 数据字段? 像这样的:

      // Check if message contains a data payload.
          if (remoteMessage.getData().size() > 0) {
              Log.d(TAG, "Message data payload: " + remoteMessage.getData());
      
              if (/* Check if data needs to be processed by long running job */ true) {
                  // For long-running tasks (10 seconds or more) use WorkManager.
                  scheduleJob();
              } else {
                  // Handle message within 10 seconds
                  handleNow();
              }
      
          }
      

      【讨论】:

      • 是的,数据正在传入 RemoteMessage。不像这样,但数据正在进入这个领域。
      【解决方案3】:

      您是否正在检查 onCreate?如果是这样,请检查 onNewIntent。

      【讨论】:

      • 这有什么帮助?
      • 事实上,这可能会有所帮助,如果活动已经在运行,则应在“onNewIntent”中检查意图。无论如何,他应该详细描述答案。
      • 覆盖 onNewIntent 活动方法。你会在这里收到一个意图对象,你可以得到额外的东西。
      • 如果活动没有运行怎么办?例如,关闭应用时,通知托盘中传递云消息?
      • 根据你的代码,点击通知后会启动activity并启动activity生命周期。这就是 PendingIntent 的工作原理。
      【解决方案4】:

      如果“notTitle”是整数而不是像这样使用 Intent 发送它

       resultIntent.putExtra("pushNotHead", notTitle.toString);
      

      【讨论】:

      • 1) 这不是有效的代码。 2)这应该有什么帮助?
      • 当我转换为像 ""+notTitle 这样的字符串时,有时会遇到问题。试一试。
      • I face problem sometime 你面临什么问题,""+someInt 和 someInt.toString() 都会导致完全相同的结果。
      • 有时数据以整数形式发送,但我们在接收端做了 getString 代码。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-13
      • 1970-01-01
      • 1970-01-01
      • 2014-11-16
      • 2014-03-01
      相关资源
      最近更新 更多