如果用相同的通知id, 该怎么告诉处理通知的活动,每个通知的内容呢?
如果用不同的通知id, 为什么处理通知的活动得到的Intent总是第一个通知的Intent呢?

解决方案 »

  1.   

    不是两位理解力低了,估计是因为从来没接触过通知吧。不过我还是当作两位没理解我的意思,把代码贴出来,我想,代码就是最好的意思。
    public class Main extends Activity {
    private Button btnTest;
    private TextView tvInfo;
    private int Count; // 未处理的通知个数
    private int Id; // 通知Id /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    InitialControl();
    } private void InitialControl() {
    btnTest = (Button) findViewById(R.id.btnTest);
    tvInfo = (TextView) findViewById(R.id.tvInfo);
    //
    btnTest.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
    btnTest_Click();
    }
    });
    } /**
     * 发出通知
     */
    protected void btnTest_Click() {
    Count++;
    Id++;
    String title = String.format("通知: %d", Id);
    String body = String.format("内容: Id=%d, Count=%d", Id, Count);
    Intent intent = new Intent(this, ShowNotify.class);
    intent.putExtra("Count", Count);
    intent.putExtra("Id", Id);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
    intent, 0);
    Notification notification = new Notification();
    notification.icon=R.drawable.icon;
    notification.tickerText = title;
    notification.defaults=Notification.DEFAULT_LIGHTS;
    notification.setLatestEventInfo(this, title, body, pendingIntent);
    notification.number = Count;
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    notificationManager.notify(2, notification);
    tvInfo.setText(body);
    }
    }public class ShowNotify extends Activity {
    private Button btnClose;
    private TextView tvInfo; /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.shownotify);
    InitialControl();
    ShowNotifyContent();
    } private void InitialControl() {
    btnClose = (Button) findViewById(R.id.btnClose);
    tvInfo = (TextView) findViewById(R.id.tvInfo);
    //
    btnClose.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
    btnClose_Click();
    }
    });
    } /**
     * 显示通知内容
     */
    private void ShowNotifyContent() {
            NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            notificationManager.cancel(2);
            
    Intent intent = getIntent();
    int Id = intent.getIntExtra("Id", 0);
    int Count = intent.getIntExtra("Count", 0);
    String content = String.format("收到通知: Id=%d, Count=%d", Id, Count);
    tvInfo.setText(content);
    } /**
     * 关闭
     */
    protected void btnClose_Click() {
    finish();
    }
    }点击btnTest就可以狂发通知了。但点击通知,进入ShowNotify活动,却总是得到第1个通知的内容。
    我的疑问是:
    1. 点击最新的通知,ShowNotify怎样才能得到最新的通知内容?
    2. 如何当通知被显示后,让Main活动的Count重置为0?
      

  2.   

    你好,我现在也是遇到和楼主类似的问题。请教是如何解决的该问题?
    QQ:117828133这是我QQ号。方便的加QQ聊吧。
      

  3.   

    想不到还有人来挖这个贴子, 我就大概说一下吧, 方便后来人如果用相同的通知id, 该怎么告诉处理通知的活动,每个通知的内容呢?
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
    intent, PendingIntent.FLAG_UPDATE_CURRENT);
    最后一个参数可以保证显示的是最新的那个通知
    如果用不同的通知id, 为什么处理通知的活动得到的Intent总是第一个通知的Intent呢?
    多个Intent是根据它们的Data属性来区分的,如果Data相同,将认为是同一个Intent
      

  4.   

    我刚好也正在找这个问题的解决方法。看到这个帖子。也查了其他,居然没有答案。
    查了谷歌的文档。有一个地方被遗漏了。就是
    PendingIntent.getActivity(参数若干)这个方法。在其参数的第二个request code。就是用于区别intent的
    标识,至少试下来没问题。谷歌文档中写这个request code 是“不使用”。我一开始以为没用。
    今天尝试下来发现可以区别各个通知中得intent内容。不然我原来做的程序也和楼主一样,即便通知id不同,
    第二第三个通知也会覆盖第一个通知的intent的现象。可能因为context是一样的(程序本身activity)的缘故。
      

  5.   

    为什么我把id改成不一样还是不行,求解
     mNotification.contentIntent = PendingIntent.getActivity(mContext, mItem.getId(),
                         intent,PendingIntent.FLAG_CANCEL_CURRENT);
      

  6.   

    看了LZ 帖子,修改了requestCode,修改id,结果还是不行,全部覆盖显示。。
    何解??。
      

  7.   


    成了!!!!分享一下,大牛请绕道~~
    public class MainActivity extends Activity
    {
    private NotificationManager mNotificationManager;
    private Context mContext = MainActivity.this; @Override
    protected void onCreate(Bundle savedInstanceState)
    {
    super.onCreate(savedInstanceState);
    super.setContentView(R.layout.activity_main);

    mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    } private Notification genreNotification(Context context, int icon, String tickerText, String title, String content, Intent intent, int id)
    {
    Notification notification = new Notification(icon, tickerText, System.currentTimeMillis());
    // 问题就在这里的id了
    PendingIntent pendIntent = PendingIntent.getActivity(context, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    notification.setLatestEventInfo(context, title, content, pendIntent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    return notification;
    }

    public void first(View view){
    mNotificationManager.notify(1, genreNotification(mContext, R.drawable.ic_launcher, "notifyText1", "notifyTitle1", "notifyText1", new Intent(mContext, Test_01.class), 1));

    }

    public void second(View view){
    mNotificationManager.notify(2, genreNotification(mContext, R.drawable.ic_launcher, "notifyText2", "notifyTitle2", "notifyText2", new Intent(mContext, Test_02.class), 2));

    }

    public void third(View view){
    mNotificationManager.notify(3, genreNotification(mContext, R.drawable.ic_launcher, "notifyText3", "notifyTitle3", "notifyText3", new Intent(mContext, Test_03.class), 3));

    }}
    Test_01、Test_02、Test_03随便建3个Activity 就可以了
      

  8.   

    忘记说,参考了这个  http://univasity.iteye.com/blog/1390445