我刚刚在我的Android应用程序中实现了GCM和通知,来自基于Apache / PHP的Web服务器。
通知已经在运行,但我仍然坚持堆叠通知,如此处所述。
我的应用程序中有两种types的通知,使用来自GCM服务的数据:
输入1(消息):
[data] => Array ( [t] => 1 [other data...] )
types2(新闻):
[data] => Array ( [t] => 2 [other data...] )
这两种types是完全不同的通知,我想将它们彼此分开堆叠,但我不能让它工作。 只要有多个通知,我想像这样堆叠它们:
默认视图
扩展视图
2个通知ID和primefaces整数
我尝试使用2个不同的通知ID,以便覆盖相同types的通知。
if (msg.get("t").toString().equals("1")) { notificationNumber = messageCounter.incrementAndGet(); } else { notificationNumber = newsCounter.incrementAndGet(); } [...] NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) .setNumber(notificationNumber);
如果同时发送2条消息,一切正常,计数器显示2.但如果两次通知之间有短暂的延迟,则计数器切换为1。
唯一通知ID
我还尝试使用生成的唯一ID
Date now = new Date(); Notification_id = now.getTime();
这样根本就没有堆叠或覆盖。
我怎样才能解决我的问题? 我是否可以访问以前发送的通知的内容,以便我可以在一行中显示每条消息,例如在Gmail的展开式视图中? 如何查看当前显示的通知数量/数量?
很久的问题,非常感谢!
我终于find了解决方案并最终使用了primefaces整数,但是在一个单独的类中:
import java.util.concurrent.atomic.AtomicInteger; public class Global { public static AtomicInteger Counter1 = new AtomicInteger(); public static AtomicInteger Counter2 = new AtomicInteger(); }
要在应用程序打开后重置计数器,我将它放在我的MainActivity中(在onCreate()
和onResume()
调用:
private void clearNotifications(){ NotificationManager mNotificationManager; mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); mNotificationManager.cancelAll(); Global.Counter1.set(0); Global.Counter2.set(0); }
当我创建通知时,我检查计数器:
Counter1 = Global.Counter1.incrementAndGet(); ContentText = (Counter1 < 2) ? /* Single notification */ : /* Stacking */;