处理多个通知/堆垛从GCM通知 [英] Handle multiple Notifications / Stacking Notifications from GCM
问题描述
我只是执行GCM和通知在我的Android应用程序,从Apache /基于PHP的Web服务器来了。
该通知已经工作了,但我卡在堆叠的通知,如所描述的这里。
I just implemented GCM and notifications in my Android app, coming from an Apache/PHP-based webserver.
The notifications are already working, but I'm stuck at stacking the notifications, as described here.
我有两种类型我的应用程序的通知,采用的数据来自GCM服务未来:
第1类(消息):
I have two types of notifications in my app, using data coming from the GCM Service:
Type 1 (Messages):
[data] => Array
(
[t] => 1
[other data...]
)
2型(新闻):
[data] => Array
(
[t] => 2
[other data...]
)
这些2种完全不同的通知,我想堆二者彼此分离,但我不能得到这个工作。
我想堆叠起来像这样,只要有多个通知:
These 2 types are completely different notifications, and I would like to stack both of them separate from each other, but I can't get this to work.
I would like to stack them like this, as soon as there are multiple notifications:
默认视图
Default View
扩展视图
Expanded View
2的通知ID和原子整数
我试图使用2个不同的通知的ID,以使用相同类型的通知获得overidden
2 Notification IDs and Atomic Integer
I tried to use 2 different notification IDs, so that notifications with the same type get overidden.
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。
If 2 messages are sent at the same time, everything works fine and the counter shows 2. But if there is a short delay between two notifications, the counter switches to 1.
唯一标识的通知
我也尝试过使用与生成的唯一的ID
Unique Notification IDs
I also tried to use unique IDs generated with
Date now = new Date();
Notification_id = now.getTime();
让没有完全没有堆放或压倒一切的。
so that there isn't no stacking or overriding at all.
我怎样才能解决我的问题?我可以访问$ P $的内容pviously发送的通知,让我可以在Gmail中的扩展视图显示每封邮件在一行,怎么样?我怎么能检查哪些/多少通知的当前显示?
长的问题,非常感谢你!
How can I solve my problem? Can I access the content of the previously sent notifications, so that I can show every message in one line, like in the expanded view of Gmail? How can I check which / how many notifications are currently displayed?
Long question, thank you very much!
推荐答案
我终于找到了解决方案,并最终使用原子整数,但在分离式类:
I finally found the solution and ended up using atomic integers, but in a seperated class:
import java.util.concurrent.atomic.AtomicInteger;
public class Global {
public static AtomicInteger Counter1 = new AtomicInteger();
public static AtomicInteger Counter2 = new AtomicInteger();
}
要重新申请开通后,计数器,我把这个在我的MainActivity(称为的onCreate()
和 onResume()
:
To reset the counter after the application opening, i put this in my MainActivity (called in onCreate()
and onResume()
:
private void clearNotifications(){
NotificationManager mNotificationManager;
mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.cancelAll();
Global.Counter1.set(0);
Global.Counter2.set(0);
}
当我创建通知,我检查计数器:
When I create the notification, I check the counter:
Counter1 = Global.Counter1.incrementAndGet();
ContentText = (Counter1 < 2) ? /* Single notification */ : /* Stacking */;
这篇关于处理多个通知/堆垛从GCM通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!