gpt4 book ai didi

广播 Intent 过滤器生成的Android推送通知替换为旧通知

转载 作者:行者123 更新时间:2023-11-29 19:17:24 26 4
gpt4 key购买 nike

我正在处理一个项目,当我发送消息时,我将它作为通知发送给另一个用户使用广播它工作正常但是当我再次发送新消息然后替换为旧通知而不创建新通知

下面是我生成通知的代码

NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();

inboxStyle.addLine(message);

Notification notification;
notification = mBuilder.setSmallIcon(icon).setTicker(title).setWhen(0)
.setAutoCancel(true)
.setContentTitle(title)
.setContentIntent(resultPendingIntent)
.setSound(alarmSound)
.setStyle(inboxStyle)
.setColor(color)
.setSmallIcon(setNotificationIcon(mBuilder))
.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), icon))
.setContentText(message)
.build();

NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(Config.NOTIFICATION_ID, notification);

广播代码

public class IncomingReceiver extends BroadcastReceiver {
public static final String CUSTOM_INTENT = "jason.wei.custom.intent.action.TEST";

@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(CUSTOM_INTENT)) {

String message = intent.getStringExtra("message");
String chat_id = intent.getStringExtra("chat_id");
String username = intent.getStringExtra("username");
String chatType = intent.getStringExtra("chatType");
MyFirebaseMessagingService mMyFirebaseMessagingService = new MyFirebaseMessagingService();
Log.e("CHATID BROADCADST",chat_id);
Intent mIntent1;
if(chatType.equalsIgnoreCase("group")){
mIntent1=new Intent(context,ActivityChatMyGroup.class);
}else {
mIntent1=new Intent(context,ActivityChat.class);
}
Bundle mBundle=new Bundle();
mBundle.putString("name",username);
mIntent1.putExtras(mBundle);
mMyFirebaseMessagingService.showChatNotificationMessage(context, chat_id, username, message, mIntent1);

}
}
}

最佳答案

您应该在每次显示通知时递增 Config.NOTIFICATION_ID。如果两个通知具有相同的通知 ID,则之前的通知将被替换。

维护一个变量(静态或基于偏好等)并在您的方法中增加它

//偏好

int notificationId = PreferenceManager.getDefaultSharedPreferences(context).getInt("Notification_ID", 0);

notificationId++;
PreferenceManager.getDefaultSharedPreferences (context).edit().putInt("Notification_ID", notificationId).apply();

然后使用这个变量

notificationManager.notify(notificationId, notification);

关于广播 Intent 过滤器生成的Android推送通知替换为旧通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43085793/

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