如何在不同的GCM通知收到时处理GCM通知 [英] How to handle GCM Notifications when different GCM notifications receive
问题描述
我在我的app.I中使用GCM通知,解析 onMessageReceived 方法中的数据,其中我得到了一些与GCM Message相对应的唯一URL。当我收到单个通知时, ,但是当我收到多个通知问题发生时,当我点击任何通知上次收到通知url加载。
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 * /
,notificationBuilder.build());
所以当我点击通知时,我只能启动最后一个url我可以如何解决这个问题?
PendingIntent.FLAG_UPDATE_CURRENT
创建PendingIntent时。
在传递意图到PendingIntent之前,每次都设置它的动作不同。例如: -
intent.setAction(Long.toString(System.currentTimeMillis()));
I am using GCM Notification in my app.I parse the data in "onMessageReceived" method where i get some unique "URL" corresponding to GCM Message.When I receive single notification Every thing is fine,But when i receive multiple notifications problem occurs when i tap any notification last received notification url loads.
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");
}
I get url from GCM Notification
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());
so when i tap the notification i am able to launch only last url How i can solve this problem?
use
PendingIntent.FLAG_UPDATE_CURRENT
when creating PendingIntent.
Also before passing intent to PendingIntent set its action different everytime. e.g.:-
intent.setAction(Long.toString(System.currentTimeMillis()));
这篇关于如何在不同的GCM通知收到时处理GCM通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!