当我创建通过C2DM发送并在我的应用程序中收到的通知时,我想要在intent extras中传递一些随推送通知一起接收的数据。第一次打开通知时这很好运行。然后根据活动的状态,在onNewIntent或in onCreate中接收数据。
但是,如果我通过C2DM发送第二个推送通知,它会正确接收新的数据,但是当从意图中获取extras时,我仍然得到来自上一条消息的数据。标题和我想看到的数据在通知中正确显示。因此,我的意图肯定有问题。无论活动是否正在运行都会出现此问题。
为了创建通知,我执行以下操作:
但是,如果我通过C2DM发送第二个推送通知,它会正确接收新的数据,但是当从意图中获取extras时,我仍然得到来自上一条消息的数据。标题和我想看到的数据在通知中正确显示。因此,我的意图肯定有问题。无论活动是否正在运行都会出现此问题。
为了创建通知,我执行以下操作:
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_stat_notify_push, "Message received", System.currentTimeMillis());
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
notification.defaults |= Notification.DEFAULT_LIGHTS;
Intent intent = new Intent(context, DesktopApp.class);
intent.setAction("android.intent.action.MAIN");
intent.addCategory("android.intent.category.LAUNCHER");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra("msg_id", msg_id);
intent.putExtra("title", title);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
notification.setLatestEventInfo(context, "New message", title + String.valueOf(msg_id), pendingIntent);
notificationManager.notify(0, notification);
接着读取意图:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent myIntent = getIntent(); // this is just for example purpose
int i = myIntent.getIntExtra("msg_id", -1);
if (i != -1) Toast.makeText(this, "Got message! " + String.valueOf(i), Toast.LENGTH_LONG).show();
}
@Override
public void onNewIntent(Intent intent){
super.onNewIntent(intent);
Bundle extras = intent.getExtras();
if (extras != null) {
int i = extras.getInt("msg_id", -1);
if (i != -1) Toast.makeText(this, "Got message! " + String.valueOf(i), Toast.LENGTH_LONG).show();
}
}
任何建议?