【发布时间】: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 中为空时,我都检查了我在社区中找到的所有可能原因,但每次响应都是一样的。
我已经尝试过下面提到的链接
Android Notification PendingIntent Extras null
Always getting data null from notification intent android
我不确定我在哪里做错了。
【问题讨论】:
标签: android push-notification firebase-cloud-messaging