【发布时间】:2013-07-09 21:31:54
【问题描述】:
我正在尝试构建使用 GCM 通过推送通知实现的新闻应用程序。 senario 基于以下内容:
当用户点击通知时,应启动服装意图 包含通知的正文。
我已经弄清楚了上面的大部分情况,但我仍然有一个问题:
服装意图正在显示最新通知的消息
我的代码是:
@Override
protected void onMessage(Context context, Intent intent) {
String message = intent.getExtras().getString("price");
displayMessage(context, message);
// notifies user
generateNotification(context, message);
}
private static void generateNotification(Context context, String message) { int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(context, NotificationActivity.class);
// set intent so it does not start a new activity
Bundle b = new Bundle();
b.putString("message", message);
notificationIntent.putExtras(b);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|
Intent.FLAG_ACTIVITY_SINGLE_TOP);
UNIQUE_INT_PER_CALL ++;
PendingIntent contentIntent = PendingIntent.getActivity(
context,
UNIQUE_INT_PER_CALL,
notificationIntent, // add this
PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(context, title, message, contentIntent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Play default notification sound
notification.defaults |= Notification.DEFAULT_SOUND;
// Vibrate if vibrate is enabled
notification.defaults |= Notification.DEFAULT_VIBRATE;
Random generator = new Random();
int r = generator.nextInt();
notificationManager.notify(r, notification);
System.exit(0);
}
这里是我如何阅读服装意图中的消息
Bundle b = getIntent().getExtras();
String message = null ;
if (b != null) {
message = b.getString("message");
//Log.i(null,"message = "+message);
}
我是android开发的新手,上面的代码是很多例子的结果(也许我有一些无用的代码)
【问题讨论】:
-
我不明白你的问题是什么。你能具体说明你期望得到什么以及你将得到什么吗?
-
当我在通知中心有很多通知时,如果用户点击任何通知,通知正文应该显示在服装意图(NotificationActivity)中。 (这应该发生)。但在我的情况下,如果用户点击任何通知,最后一次的消息会出现在服装意图(NotificationActivity)中
标签: android push-notification android-pendingintent google-cloud-messaging