我需要有关Android通知的帮助。我的服务器将具有唯一ID的请求(发送到Firebase服务器)发送给新通知。如果我的设备将收到它,则会创建并通知新的通知。如果我要发送下一个具有其他唯一ID的请求,则会创建新的通知。状态栏上有两个通知。每个通知在Intent中都有一些数据,我想在Activity中显示它。但是在单击第一个或第二个或最后一个(如果设备收到2个或更多通知)之后,它将导致从最后一个接收到的(通知堆栈顶部)启动Activity(带有Intent)。我认为这个问题出在Intent或PendingIntent上。
@Override
public void onMessageReceived(RemoteMessage respose ) {
JSONObject response_intent=null;
try {
response_intent= new JSONObject( respose.getData().get("intent"));
} catch (JSONException e) {
e.printStackTrace();
}
int unique_id=Integer.valueOf(respose.getData().get("id_original"));
/*After notification click to open Activity ... */
Intent intent = new Intent("eu.energochemica.cat_notifications.DETAIL_SCREEN");
/*... with data from firebase via Intent */
intent.putExtra("fromNotification", true);
intent.putExtra("intentFromNotification", respose.getData().get("intent"));
intent.putExtra("id_original",unique_id);
intent.putExtra("header",respose.getData().get("header"));
intent.putExtra("text",respose.getData().get("text"));
/*HERE? What FLAG to use?* Intent.FLAG_ACTIVITY_NEW_TASK */
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
/*OR HERE? What FLAG to use?*/
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, Config.NOT_CHANNEL_ID)
.setSmallIcon(this.getResources().getIdentifier("cat_logo_white", "drawable", this.getPackageName()))
.setContentTitle(respose.getData().get("header"))
.setContentText(respose.getData().get("text"))
.setAutoCancel(false)
.setChannelId(Config.NOT_CHANNEL_ID)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setSound(Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE+ "://" +this.getPackageName()+"/"+R.raw.notif))
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(unique_id, notificationBuilder.build());
}
我需要 (如果我在行中收到fe四个通知:1、2、3、4,其中4是最后一个(最新)) 以打开“活动”具有正确的意图。 (单击通知1(最旧)将打开此通知1中具有意图的活动,而不会打开通知4中的意图) 。
我不知道我和FLAG代码有什么关系。 有人可以帮我吗?
答案 0 :(得分:1)
我的天哪...
我发现了问题所在...阅读了官方说明PendingIntent.class
之后,我尝试将unique_id传递给
PendingIntent.getActivity(this, u_id, intent, ...
代替
PendingIntent.getActivity(this, 0, intent, ...
其中常数0表示“重写”相同的PendingIntent(在我看来)=> 0是PendingIntent的ID。