android – 更新多条消息的通知

android – 更新多条消息的通知,第1张

概述下面是我的代码,当新消息到达我的应用程序时生成通知, 码 private static void generateNotification(Context context, String message) { int icon = R.drawable.ic_launcher; long when = System.currentTimeMillis(); 下面是我的代码,当新消息到达我的应用程序时生成通知,

private static voID generateNotification(Context context,String message) {        int icon = R.drawable.ic_launcher;        long when = System.currentTimeMillis();        int counter = 0;        counter++;        notificationmanager notificationmanager = (notificationmanager)context.getSystemService(Context.NOTIFICATION_SERVICE);        Notification notification = new Notification(icon,message,when);        String Title = context.getString(R.string.app_name);        Intent notificationIntent = new Intent(context,MessageActivity.class);        // set intent so it does not start a new activity        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_top | Intent.FLAG_ACTIVITY_SINGLE_top);        PendingIntent intent = PendingIntent.getActivity(context,notificationIntent,0);        notification.setLatestEventInfo(context,Title,intent);        notification.flags |= Notification.FLAG_auto_CANCEL;        notification.defaults |= Notification.DEFAulT_SOUND;        notification.defaults |= Notification.DEFAulT_VIBRATE;        notificationmanager.notify(counter,notification);    }

当我收到多条消息时,会有多个通知.

例如

假设我收到3条新消息,那么有3条单独的通知.但我只想要多条消息的单一通知.当我收到多条消息时,应该像WhatsApp中看到的那样更新通知.我怎样才能做到这一点?

解决方法 如果您有多条消息,例如在JsonArray中,您可以遍历数组并为数组中的每个项创建通知.可以通过设置相同的Unique_ID来更新循环中创建的通知.如果要显示一个数字以显示此通知中的消息量(即未读消息的数量),请创建一个计数器并在循环中将其增加1.基于目标AndroID-SDK使用例如NotificationCompat.InBoxStyle(需要androID.v4.support库)或Notification.InBoxStyle来显示多行通知并使用InBoxStyle.addline在循环中添加每个Arrayitems的内容. 总结

以上是内存溢出为你收集整理的android – 更新多条消息的通知全部内容,希望文章能够帮你解决android – 更新多条消息的通知所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1125144.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存