我在用户点击通知后尝试“阅读更多”。
我需要什么,以独特的额外内容“id”,“title”和“text”开始新的活动。
我的代码:
void notificationSend(String id, String bar, String title, String text, String image, int delay) {
Bitmap bitmap = getBitmapFromURL(image, 130);
if(bitmap == null){
bitmap = getBitmapFromURL("https://pp.vk.me/c621316/v621316641/119d5/HB9s2z5mX-s.jpg", 130);
}
Intent notificationIntent = new Intent(this, SingleNewsActivity.class);
notificationIntent.putExtra("id", id);
notificationIntent.putExtra("title", title);
notificationIntent.putExtra("text", text);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentIntent(contentIntent);
builder.setSmallIcon(R.drawable.ic_bitcoin_notification);
builder.setLargeIcon(bitmap);
builder.setTicker(bar);
builder.setContentTitle(title);
builder.setContentText(text);
builder.setWhen(System.currentTimeMillis());
builder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
Notification notification = builder.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
try {
notificationManager.notify(new Integer(id), notification);
sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
接下来要做的是,让我发送2个通知,在我点击第一个,活动开始但是第二个通知中有额外的数据,为什么?
答案 0 :(得分:0)
问题是由于使用:FLAG_UPDATE_CURRENT
如果你看到这个标志的文件:
Flag indicating that if the described PendingIntent already exists, then keep it but replace its extra data with what is in this new Intent. For use with getActivity(Context, int, Intent, int), getBroadcast(Context, int, Intent, int), and getService(Context, int, Intent, int).
This can be used if you are creating intents where only the extras change, and don't care that any entities that received your previous PendingIntent will be able to launch it with your new extras even if they are not explicitly given to it.
在您的情况下,您只是更新额外的,因此,额外的值会更新。
要解决您的问题,您应该传递针对所有情况的唯一请求代码:
醋栗代码:
PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
实际值:
PendingIntent.getActivity(this, unique_code, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
答案 1 :(得分:0)
所以原因是你正在为通知设置唯一的id,假设你的第一个通知出现时id值为1,那么通知对象是用你的id创建的,当你第二次通知时,你的id又是1所以您的通知已更新,当您点击它时,您会收到第二个通知。 您可以做的是为通知设置不同的ID。看看下面的代码
private NotificationManager mNotificationManager;
private NotificationCompat.Builder builder;
在函数
中写下代码 mNotificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
Random random = new Random();
int NOTIFICATION_ID = random.nextInt(9999 - 1000) + 1000;
Intent intent = new Intent(this, SingleNewsActivity.class);
Bundle bundle = new Bundle();
bundle.putExtra("id", id);
bundle.putExtra("title", title);
bundle.putExtra("text", text);
intent.putExtras(bundle);
PendingIntent contentIntent = PendingIntent.getActivity(this, NOTIFICATION_ID,
intent, 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this).setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("TITLE")
.setStyle(new NotificationCompat.BigTextStyle().bigText("ASD"))
.setContentText("Summary").setAutoCancel(true);
mBuilder.build().flags |= Notification.FLAG_AUTO_CANCEL;
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
Log.d("", "Notification sent suessfully.");
在上述代码中,只要您收到通知,就会在 NOTIFICATION_ID
中获得唯一代码