新PendingIntent更新当前意图

Mah*_*esh 8 android android-pendingintent

我试图在一段时间间隔后显示不同的通知但发生的事情是它更新当前PendingIntent的新的一个因此我得到只有一个通知,即使我触发4 - 5个待处理的意图请求

在按钮上单击我执行以下操作

 try{
                 adapter.OpenDB();
             int id = adapter.getMaxId("reminder")+1;
             adapter.CloseDB();
             Intent intent = new Intent(MilestonesActivity.this, AlarmReceiver.class);
             intent.putExtra("message", ""+editMsg.getText()+" "+editDate.getText());               
             intent.putExtra("id", id);
              PendingIntent pendingIntent = PendingIntent.getBroadcast(MilestonesActivity.this, 0,
                intent, PendingIntent.FLAG_UPDATE_CURRENT);
                am.set(AlarmManager.RTC_WAKEUP,
                reminddate.getTimeInMillis(), pendingIntent);

             adapter.CloseDB();
             }catch (Exception e) {
                // TODO: handle exception
            }
Run Code Online (Sandbox Code Playgroud)

AlarmReceiver.java

 @Override
 public void onReceive(Context context, Intent intent) {

     Intent i =new Intent(context, NotificationService.class);

     try{

     int id = intent.getExtras().getInt("id");
        i.putExtra("id", id);
     CharSequence message = intent.getExtras().getString("message");
        i.putExtra("message", message);
     }catch (Exception e) {
        // TODO: handle exception
    }
     context.startService(i);

 }
Run Code Online (Sandbox Code Playgroud)

NotificationService.java

 public void  show(Intent intent) {
 try{
        NotificationManager nm;
         Log.e("recieve", "ok");
      nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
      CharSequence from = "Lawyer app Reminder";
      CharSequence message = intent.getExtras().getString("message");
      PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(), 
                PendingIntent.FLAG_ONE_SHOT);// also tried FLAG_UPDATE_CURRENT
      int id =intent.getExtras().getInt("id");
      Log.e("I", ""+id);
      Notification notif = new Notification(R.drawable.message_active,
        "Lawyer app Reminder.", System.currentTimeMillis());
      notif.defaults = Notification.DEFAULT_ALL;
      notif.flags = Notification.FLAG_AUTO_CANCEL;

      notif.setLatestEventInfo(this, from, message, contentIntent);
      nm.notify(id, notif);

 }catch (Exception e) {         
     e.printStackTrace();            
}
Run Code Online (Sandbox Code Playgroud)

plaese提前帮助我.谢谢

Mah*_*esh 37

我找到了

pendingintent构造函数的第二个参数不应该相同(这是0硬编码)

我将其更改为(int)System.currentTimeMillis(),以便它不应该是相同的:)

Intent ni =new Intent(NotificationService.this,ModifyDatabaseService.class);
        ni.putExtra("id", ""+id);
        PendingIntent contentIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), ni, 
                PendingIntent.FLAG_ONE_SHOT);
Run Code Online (Sandbox Code Playgroud)

  • 妈妈,皇家海军十字戏!我有完全相同的问题.-ing文档甚至说"当前没有使用".目前没用过我的屁股. (14认同)
  • *更新2 *:忽略我之前的评论。FLAG_ONE_SHOT使之前的Intent不起作用。即一旦使用了FLAG_ONE_SHOT Intent,它将变成nop Intent()。因此,您应该始终使用唯一的请求代码。 (2认同)

Viv*_*wal 3

如果 id 的值nm.notify(id, notif);相同,则通知将被覆盖。

所以你需要确保不同的Notification的id是不同的