Android GCM在状态栏中带有一个图标的多个推送通知 [英] Android GCM multiple push notifications with one icon in status bar

查看:97
本文介绍了Android GCM在状态栏中带有一个图标的多个推送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用希望将多个推送通知捆绑在状态栏中的一个图标中.

My app wants to bundle multiple push notifications in one icon in the status bar.

单击图标时,应用程序应收到多个通知,以列表视图模式显示它们.

When clicking on the icon, the multiple notifications should be received by the app to show them in listview mode.

stackoverflow中已经有一些条目与我想要获得的条目非常接近,它确实使我可以更好地了解待处理的意图和通知标志,但是它们并不能完全解决我的问题.

There are already some entries in stackoverflow which come close to what I want to obtain and it did give me a better insight in handling pending intents and notification flags but they didn´t solve completely my problem.

第一步:创建通知:

在stackoverflow中的一些条目之后,我做了以下假设:

Following some entries in stackoverflow, I made the following assumptions:

  • 一个通知ID(notifyID)仅在状态栏中获得一个图标
  • 待定意向中的唯一requestId参数,用于区分同一通知ID中的各种通知请求
  • 用于通知意图的FLAG_ACTIVITY_NEW_TASK和用于待决意图的FLAG_UPDATE_CURRENT

  • One notification ID (notifyID) to obtain only one icon in status bar
  • A unique requestId parameter in the pending intent to differentiate between the various notifications requests within the same notification ID
  • FLAG_ACTIVITY_NEW_TASK used in notification intent and FLAG_UPDATE_CURRENT used in pending intent

 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发送通知时,我的应用程序都会在考虑到之前的假设的情况下生成一条通知,以发送给通知管理器.

The way I see it, each time a notification is sent by GCM, my app generates a notification to send to the notification manager taking into account the previous assumptions.

第一个问题:如果我想用剩余的待处理通知数通知用户,如何跟踪以前发送的通知数?我必须将其存储在存储介质中吗?

First problem: if I want to notify the user with the number of pending notifcations left, how can I get track of the previous number of sent notifications? Do I have to store it in storage media?

第二步:在状态栏中点击包含多个通知的通知图标:

   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想要的方式.

Second problem: I only receive the last notification sent. It seems to be that differentiating the pending intents with requestId didn´t do the trick. Also I thought that the different pending intents related to one notification icon would be handled by onNewIntent... A possible solution that I was thinking about is to save incoming notifications from GCM in storage and get them on tapping the status bar icon, but for sure this cannot be the way Google meant it to be…

¿有帮助吗?

推荐答案

第二步,尝试以下操作:PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

for second step try this: PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

这篇关于Android GCM在状态栏中带有一个图标的多个推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆