jus*_*ser 19 notifications android push-notification android-intent
当我创建通过C2DM发送并在我的应用程序中收到的通知时,我想传递一些来自C2DM的推送通知附带的数据.这在我第一次打开通知时工作正常.然后根据活动的状态使用onNewIntent或onCreate接收数据.
但是如果我通过C2DM发送第二个推送通知,则会使用新数据正确接收,但是当从意图中获取额外内容时,我仍然可以从上一个消息中获取数据.我希望看到的标题和数据在通知中正确显示.所以我的意图一定是错的.如果活动正在运行而不是,则会发生这种情况.
要创建通知,请执行以下操作:
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);
Run Code Online (Sandbox Code Playgroud)
然后阅读意图:
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();
}
}
Run Code Online (Sandbox Code Playgroud)
有什么建议?
Hei*_*upp 37
您需要设置一个干净的标志PendingIntent
:
PendingIntent pintent =
PendingIntent.getActivity(context,0,intent,PendingIntent.FLAG_CANCEL_CURRENT);
Run Code Online (Sandbox Code Playgroud)
看看这篇文章,给出了更长的解释.
小智 5
用这个
PendingIntent pi = PendingIntent.getActivity(context, UNIQUE_ID,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
8009 次 |
最近记录: |