【发布时间】:2014-05-15 18:52:03
【问题描述】:
我正在编写 android messenger。当应用程序被最小化并且一些联系人写信给我时,通知被成功抛出。如果两个联系人写信,则每条消息都显示在不同的框中。
所以我需要输入然后获取联系人 ID..
这是我在应用程序中的代码,从那里发出通知:
private void showNotification(Message m) {
Contact temp = dbHelper.getContact(m.getPersonID());
Intent notificationIntent = new Intent(getApplicationContext(), ChatActivity.class);
notificationIntent.putExtra("prsID", m.getPersonID());
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
Notification.Builder builder = new Notification.Builder(this);
builder.setTicker("Messenger: New Message");
builder.setContentTitle(temp.getName());
builder.setContentText(m.getMessage());
builder.setSmallIcon(R.drawable.ic_launcher);
if (temp.hasImage()){
Bitmap bmp = BitmapFactory.decodeByteArray(temp.getImageByteArr(), 0, temp.getImageByteArr().length);
builder.setLargeIcon(bmp);
}
Notification noti = builder.setContentIntent(pIntent).build();
noti.flags = Notification.FLAG_AUTO_CANCEL;
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(temp.getId(), noti);
}
这是 ChatActivity 中的代码,我正在尝试检索联系人 ID:
@Override
protected void onResume() {
super.onResume();
if (this.guest != null){
int prsid = getIntent().getIntExtra("prsID", -1);
if (prsid != -1){
this.guest = this.messageLoader.getContact(prsid);
ContactForChat.getInstance().setContact(this.guest.getId());
} else {
this.guest = this.messageLoader.getContact(this.guest.getId());
ContactForChat.getInstance().setCurrentID(this.guest.getId());
}
this.guest.setNewMessage(false);
this.messageLoader.updateContact(guest);
}
if (this.messageLoader != null)
this.messageLoader.chatOnPause(false);
}
但是 int prsid = getIntent().getIntExtra("prsID", -1);总是返回 -1
有人可以帮助我吗?如何正确地做到这一点? 提前谢谢你
更新
事实上,我猜 getIntent() 返回 null:
解决方案
在 ChatActivity 中,我必须重写一个方法(a 和 ab 都有正确的值):
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Bundle a = intent.getExtras();
int ab = a.getInt("prsID");
int b = intent.getIntExtra("prsID", -1);
}
【问题讨论】:
-
我找到了解决方案:我必须重写:`protected void onNewIntent(Intent intent)`,里面的代码看起来像:`super.onNewIntent(intent);捆绑 a = intent.getExtras(); int ab = a.getInt("prsID"); `
标签: android android-intent notifications