我正在尝试构建新闻应用程序,它是通过使用 GCM 的推送通知实现的。senario 基于以下几点:
当用户单击通知时,应使用包含通知正文的装束意图。
我已经弄清楚了上述大部分情况,但我仍然有一个问题:
服装意图正在显示最新通知的消息
我的代码是:
@Override
protected void onMessage(Context context, Intent intent) {
String message = intent.getExtras().getString("price");
displayMessage(context, message);
// notifies user
generateNotification(context, 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开发的新手,上面的代码是很多例子的结果(也许我有一些无用的代码)