【发布时间】:2013-07-08 08:18:36
【问题描述】:
我的应用想要在状态栏中的一个图标中捆绑多个推送通知。
当点击图标时,应用程序应该会收到多个通知,以便在列表视图模式下显示它们。
stackoverflow 中已经有一些条目接近我想要获得的内容,它确实让我对处理待处理的意图和通知标志有了更好的了解,但它们并没有完全解决我的问题。
第一步:创建通知:
根据stackoverflow中的一些条目,我做了以下假设:
- 一个通知ID(notifyID)只获取状态栏中的一个图标
- 待处理意图中的唯一 requestId 参数,用于区分同一通知 ID 中的各种通知请求
-
FLAG_ACTIVITY_NEW_TASK 用于通知意图,FLAG_UPDATE_CURRENT 用于待处理意图
Notification notification; int icon = R.drawable.ic_launcher; int smallIcon = R.drawable.ic_launcher; int notifyID = 1; long when = System.currentTimeMillis(); int requestID = (int) System.currentTimeMillis(); NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); Intent notificationIntent = new Intent(context, NewActivity.class); notificationIntent.putExtra("new_message", message); notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); PendingIntent contentIntent = PendingIntent.getActivity(context, requestID, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); int numMessages = 1; NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context); mBuilder.setNumber(numMessages) .setSmallIcon(smallIcon) .setAutoCancel(true) .setContentTitle("You have " +numMessages+ " new messages.") .setContentText(message) .setWhen(when) .setContentIntent(contentIntent) .setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE); notificationManager.notify(notifyID, mBuilder.build());
在我看来,每次 GCM 发送通知时,我的应用都会生成一个通知以发送给通知管理器,同时考虑到前面的假设。
第一个问题:如果我想通知用户剩余的待处理通知数量,我如何才能跟踪之前发送的通知数量?我必须将其存储在存储介质中吗?
第二步:点击状态栏中包含多个通知的通知图标:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
showNotifications();
}
public void onResume() {
showNotifications();
super.onResume();
}
public void showNotifications() {
if (getIntent().getExtras() != null) {
if (getIntent().getExtras().getString("new_message") != null) {
String newMessage = getIntent().getExtras().getString("new_message");
if (!newMessage.equals("")) {
handleMessage(newMessage);
getIntent().removeExtra("new_message);
}
}
}
}
public void onNewIntent(Intent intent) {
Bundle extras = intent.getExtras();
if (extras != null) {
if (intent.getExtras().getString("new_message") != null) {
String newMessage = intent.getExtras().getString("new_message");
if (!newMessage.equals("")) {
intent.removeExtra("new_message"); }
}
}
}
}
第二个问题:我只收到最后发送的通知。似乎用 requestId 区分待处理的意图并没有解决问题。我还认为与一个通知图标相关的不同待处理意图将由 onNewIntent 处理...... 我正在考虑的一个可能的解决方案是将来自 GCM 的传入通知保存在存储中,并在点击状态栏图标时获取它们,但可以肯定这不是 Google 想要的方式......
¿有什么帮助吗?
【问题讨论】:
-
您的问题解决了吗?如果是这样,怎么做?我正在尝试做一些非常相似的事情。
标签: android push-notification android-pendingintent