Rom*_*ner 9 notifications android push-notification google-cloud-messaging
我刚刚在我的Android应用程序中实现了GCM和通知,来自基于Apache/PHP的Web服务器.
该通知已经工作了,但我被困在堆叠的通知,如所描述这里.
我的应用程序中有两种类型的通知,使用来自GCM服务的数据:
类型1(消息):
[data] => Array
(
[t] => 1
[other data...]
)
Run Code Online (Sandbox Code Playgroud)
类型2(新闻):
[data] => Array
(
[t] => 2
[other data...]
)
Run Code Online (Sandbox Code Playgroud)
这两种类型是完全不同的通知,我想将它们彼此分开堆叠,但我不能让它工作.一旦有多个通知,我想像这样堆叠它们:
默认视图

扩展视图

2通知ID和原子整数
我试图使用2个不同的通知ID,以便覆盖相同类型的通知.
if (msg.get("t").toString().equals("1")) {
notificationNumber = messageCounter.incrementAndGet();
} else {
notificationNumber = newsCounter.incrementAndGet();
}
[...]
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setNumber(notificationNumber);
Run Code Online (Sandbox Code Playgroud)
如果同时发送2条消息,一切正常,计数器显示2.但如果两次通知之间有短暂的延迟,则计数器切换为1.
唯一通知ID
我还尝试使用生成的唯一ID
Date now = new Date();
Notification_id = now.getTime();
Run Code Online (Sandbox Code Playgroud)
这样根本就没有堆叠或覆盖.
我怎样才能解决我的问题?我是否可以访问以前发送的通知的内容,以便我可以在一行中显示每条消息,例如在Gmail的展开式视图中?如何查看当前显示的通知数量/数量?
很久的问题,非常感谢!
我终于找到了解决方案并最终使用原子整数,但是在一个单独的类中:
import java.util.concurrent.atomic.AtomicInteger;
public class Global {
public static AtomicInteger Counter1 = new AtomicInteger();
public static AtomicInteger Counter2 = new AtomicInteger();
}
Run Code Online (Sandbox Code Playgroud)
要在应用程序打开后重置计数器,我将它放在我的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);
}
Run Code Online (Sandbox Code Playgroud)
当我创建通知时,我检查计数器:
Counter1 = Global.Counter1.incrementAndGet();
ContentText = (Counter1 < 2) ? /* Single notification */ : /* Stacking */;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3401 次 |
| 最近记录: |