如何在收到不同的GCM通知时处理GCM通知

时间:2016-03-17 08:50:52

标签: android google-cloud-messaging

我在我的app中使用GCM通知。我在" onMessageReceived "中解析数据方法,我得到一些独特的" URL"对应于GCM消息。当我收到单个通知时,一切都很好,但当我收到多个通知问题时,我点击任何通知上次收到通知网址加载。

public void onMessageReceived(String from, Bundle data) {

        String message = data.getString("message");
        String country = data.getString("country");
        String url = data.getString("url");
        String description = data.getString("description");
        String id = data.getString("id");
}

我从GCM通知中获取网址

Intent intent = new Intent(this, Detailview.class).putExtra("url", url); 
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(Mainscreen.class);
stackBuilder.addNextIntent(intent);

PendingIntent pendingIntent= stackBuilder.getPendingIntent( 0, PendingIntent.FLAG_ONE_SHOT );
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification_layout);
contentView.setImageViewResource(R.id.image, R.drawable.ic_stat_ic_notification);
contentView.setTextViewText(R.id.title, message);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.ic_stat_ic_notification)
        .setAutoCancel(true)
        .setContent(contentView)
        .setSound(defaultSoundUri)
        .setContentIntent(pendingIntent);
NotificationManager notificationManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(Integer.parseInt(id)
            /*ID of notification */
        , notificationBuilder.build());

所以当我点击通知时,我只能启动最后一个网址我怎么能解决这个问题?

2 个答案:

答案 0 :(得分:0)

只需将到达时间作为差异添加到每个通知中

PendingIntent pendingIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent,
                PendingIntent.FLAG_UPDATE_CURRENT);

答案 1 :(得分:0)

使用

PendingIntent.FLAG_UPDATE_CURRENT 

创建PendingIntent时。

在将意图传递给PendingIntent之前,每次都将其动作设置为不同。 e.g: -

intent.setAction(Long.toString(System.currentTimeMillis()));