【问题标题】:Android how to put and retrieve data from NotificationAndroid如何从通知中放置和检索数据
【发布时间】: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


【解决方案1】:

我要试一试,猜想m.getPersonID() 返回的是 long 而不是 int?所以你需要使用getIntent().getLongExtra("prsID", -1);

【讨论】:

  • public class Message { ... private int personID; ... public int getPersonID() { return personID; } ....
  • 你有没有调试你的onResume方法看看getIntent().getExtras()返回什么?
  • android manifest 是否需要用于 chatActivity 的社会标签?我的说:
  • 不,你不需要任何额外的标签
【解决方案2】:

在您的showNotification 方法中,创建第一个空行Log.v("NOTIFICATION", "id = " + m.getPersonID()); 并确保在将其添加到附加项时它实际上设置为-1 以外的值。此外,请确保 m.getPersonID() 的返回类型是 int,而不是 longdouble 或其他数字。

【讨论】:

  • 我已经调试过了,它确实添加了正确的 personID,它是 int
【解决方案3】:

要区分通知,您可以

你可以用这个

mManager.notify(0, notification); to  mManager.notify((int) when, notification);

在哪里

 long when = System.currentTimeMillis();

【讨论】:

  • 为什么会有负面评论?
  • 不是我 :) 但无论如何,我的问题不是关于区分通知。正如您在主要问题中看到的那样:notificationManager.notify(temp.getId(), noti);所以来自不同联系人的每条新消息通知都有不同的 id-s
猜你喜欢
  • 2021-08-27
  • 2023-03-20
  • 2019-08-10
  • 1970-01-01
  • 1970-01-01
  • 2014-11-17
  • 1970-01-01
  • 1970-01-01
  • 2019-02-20
相关资源
最近更新 更多