gpt4 book ai didi

java - Android推送通知: Multiple notifications displays same data

转载 作者:行者123 更新时间:2023-12-02 01:11:33 25 4
gpt4 key购买 nike

我有一个应用程序,它显示从后端生成的通知,每个通知都包含不同的数据,单击时应在通知页面上显示适当的内容。

例如:假设我收到 3 个通知,其中一个通知的数据为红色,id 为 1,然后我收到另一个通知,其中的数据为蓝色,id 为 2,另一个通知的数据为绿色,id 为 3,所以每当我单击任何通知时,都会说“红色”、“蓝色”或绿色它总是打开红色的通知页面

@Override    
public void onReceive(Context context, Bundle bundle) {
ctx = context;
model = new Model();
String subtitle = bundle.getString("subtitle");
String title = bundle.getString("title");
String body = bundle.getString("body");
String eventJsonString = bundle.getString("extraData");
JSONObject extraObj = null;
try {
extraObj = new JSONObject(eventJsonString);
} catch (JSONException e) {
e.printStackTrace();
}
String Id = null;
try {
deviceId = extraObj.getString("Id");
} catch (JSONException e) {
e.printStackTrace();
}
if( Id == null){
Log.d("Notification","ready notification "+bundle.get("Id"));
Intent result = new Intent();
result.setAction(ServiceConstants.NOTIFICATION_RECEIVED);
LocalBroadcastManager.getInstance(context).sendBroadcast(result);
}
else{
isNotification = true;
try {
model.setId(Integer.parseInt(extraObj.getString("Id")));
} catch (Exception e) {
e.printStackTrace();
}

//Type is not available with notification bundle
try {
model.setType(extraObj.getString("Type"));
} catch (Exception e) {
e.printStackTrace();
model.setType("NA");
}

}
sendNotification(title, subtitle, body, context);

}

private void sendNotification(String title, String subtitle, String body, Context context) {

Intent intent = null;
if(isNotification){
intent = new Intent(ctx, NotificationActivity.class);
intent.putExtra("model", model);
}
else{
intent = new Intent(ctx, MyActivity.class);
}
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
mNotificationManager = (NotificationManager)
ctx.getSystemService(NOTIFICATION_SERVICE);

PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);


String id = "com.example.testnotification";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel mChannel = new NotificationChannel(id, title, NotificationManager.IMPORTANCE_DEFAULT);
mChannel.enableLights(true);
mChannel.enableVibration(true);
mChannel.setLightColor(Color.GREEN);
mNotificationManager.createNotificationChannel(mChannel);
}

NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(ctx)
.setSmallIcon(R.drawable.launcher_icon)
.setContentTitle(title)
.setChannelId(id)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(subtitle +"\n" + body))
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
.setContentText(body);

mBuilder.setContentIntent(contentIntent);
mBuilder.setAutoCancel(true);
int random = (int) (Math.random() * 50 + 1);
mNotificationManager.notify(random, mBuilder.build());
}

最佳答案

在此处传递唯一 ID

 PendingIntent contentIntent = PendingIntent.getActivity(ctx,id,
intent, PendingIntent.FLAG_UPDATE_CURRENT);

关于java - Android推送通知: Multiple notifications displays same data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59320526/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com