Intent Extras始终为null

时间:2019-07-18 11:48:00

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

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

用于构建通知的代码:

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为空时,我都会检查自己在社区中发现的所有可能原因,但每次响应都是相同的。

我尝试了以下提到的链接

Intent's extra is always null

Android Notification PendingIntent Extras null

Always getting data null from notification intent android

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

4 个答案:

答案 0 :(得分:0)

您是否尝试过检查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();
        }

    }

答案 1 :(得分:0)

即使经过大量调试,我仍然找不到问题的原因。最后,我更改了向活动发送Intent额外内容的方式。现在,我正在使用捆绑软件发送数据及其工作,就像一个魅力。这是一种解决方法,而不是问题的答案,所以我不会接受它作为可接受的答案。

答案 2 :(得分:-1)

您要签入onCreate吗?如果是这样,请检查onNewIntent。

答案 3 :(得分:-2)

如果“ notTitle”是整数,则使用这样的Intent发送它

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