关于发送多条notifications所遇到的一些问题

由于我需要开发的app是面向特定人群使用的。服务器根据需要向用户及时推送内容。可以认为每一条内容都很重要,并且客户都希望我们能够准确的推送。所以采用发送多条推送。也就是notification的ID 不能是一个定值, 本来我以为这个设置就OK了。结果发现到了一个情况:

        比如我发送了三条推送消息,每条消息都包含一个JSON数据, APP收到会显示该JSON数据。现在碰到的问题是按照网上给的代码。 如果三条信息都不点击会发现 这三个notification只包含一个Intent。 也就是你点击一个会执行你期望的动作, 其他两个失效了。最后查到stackoverflow上面有人解决了这个问题, 就是给PendingIntent的getActivity()中的requestCode设置一个unique ID 就OK了 。附上链接: http://stackoverflow.com/questions/3009059/android-pending-intent-notification-problem

发送notifications的代码

	
NotificationManager mNotificationManager =(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent mIntent = new Intent(context, MainActivity.class);
mIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				
mIntent.putExtra(TAG, GCM);
mIntent.putExtra(MESSAGE_ID, message_id);
mIntent.putExtra(TITLE, title);
mIntent.putExtra(TEXT, msg);
				
int requestID = (int) System.currentTimeMillis();
				
PendingIntent contentIntent = PendingIntent.getActivity(context, requestID, mIntent, PendingIntent.FLAG_ONE_SHOT);
				
				NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
				.setContentTitle(title)
				.setContentText(msg)
				.setSmallIcon(R.drawable.ic_launcher)
				.setAutoCancel(true)
				.setLights(Color.parseColor("#2E6AD7"), 5000, 5000)
				.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
				
				mBuilder.setContentIntent(contentIntent);
				
				SharedPreferences prefs = context.getSharedPreferences("Notifications", Context.MODE_PRIVATE);
				int notifiId = prefs.getInt("notifiId_count", 0);
				notifiId = notifiId + 1;
				
				mNotificationManager.notify(notifiId,mBuilder.build());
				
				
				SharedPreferences.Editor editor = prefs.edit();
				editor.putInt("notifiId_count", notifiId);
				editor.commit();




http://www.oschina.net/code/snippet_698594_23944
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值