问题描述
我正在尝试创建一个通知,以启动带有附加信息的活动。但是,目前无法正常工作。
private void showNotification(RemoteMessage remoteMessage){
try{
// Create an explicit intent for an Activity in your app
Intent i = new Intent(getBaseContext(),MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
i.putExtra(EXTRA_MESSAGE,remoteMessage);
PendingIntent pendingIntent = PendingIntent.getActivity(this,i,0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this,CHANNEL_ID)
.setSmallIcon(R.drawable.ic_action_name)
.setContentTitle("Title")
.setContentText("Content")
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_CALL)
.setAutoCancel(true)
// Set the intent that will fire when the user taps the notification
.setFullScreenIntent(pendingIntent,true);
// notificationId is a unique int for each notification that you must define
notificationId++;
//https://developer.android.com/training/notify-user/time-sensitive
// Provide a unique integer for the "notificationId" of each notification.
startForeground(notificationId,builder.build());
}catch (Exception e){
Log.d(TAG,e.getMessage());
}
}
活动在单击通知后启动。但是,在Activity的onCreate内部检查捆绑包中的多余内容时,找不到它:
编辑:实际上,我想要的是活动显示而无需用户单击任何内容,因此,我为什么要使用setFullScreenIntent。
if(bundle!=null && bundle.containsKey(CustomFirebaseMessagingService.EXTRA_MESSAGE)){
Log.d(TAG,"MainActivity has extra message");
}else{
Log.d(TAG,"MainActivity doesn't have extra message");
}
我的日志说 MainActivity没有多余的消息
解决方法
尝试
setContentIntent(pendingIntent)
由于您希望在通知点击时触发意图,因此该方法的文档会显示:
,在单击通知时提供要发送的待处理意图。
创建FLAG_UPDATE_CURRENT
时需要设置标志PendingIntent
:
PendingIntent pendingIntent = PendingIntent.getActivity(this,i,PendingIntent.FLAG_UPDATE_CURRENT);
这将确保您的“附加”实际上已添加到PendingIntent
。