如何管理来自同一应用程序的多个通知

时间:2015-03-02 07:47:41

标签: android notifications alarmmanager

我是android新手。我正在开发消防通知的应用程序。当我在状态栏上有多个通知时,我点击第二个,第一个将被打开。我只能获得第一次通知的ID。如何选择特定通知我点击。这是我的代码

 save.setOnClickListener(new OnClickListener() {

     for(int i=1;i<gpPeople.size();i++)
     {
         Context ctx = SetReminder.this.getApplicationContext();
         Intent intent = new Intent(SetReminder.this, AlarmReciever.class);
         intent.putExtra("contactName",gpPeople.get(i));
         intent.putExtra("id", id);//unique notification Id
         PendingIntent objPendingIntent = PendingIntent.getBroadcast(SetReminder.this, i, intent, 0);
         AlarmManager alarmManager = (AlarmManager)ctx.getSystemService(ctx.ALARM_SERVICE);
         alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, StartTime, cal.getTimeInMillis(), objPendingIntent);
     }
 }

AlarmReciever.java

 public void onReceive(final Context context, Intent intent) {

 name= extras.getString("contactName");
 String id= extras.getString("id");
 notificationIdentifier= Integer.parseInt(id);

 Intent intent1 = new Intent(context, Notifications.class);
 PendingIntent objPendingIntent = PendingIntent.getActivity(context, 0, intent1, 0);

 Intent intent2= new Intent(context, CancelNotification.class);
 intent2.putExtra("id", notificationIdentifier);
 PendingIntent objPendingCancel = PendingIntent.getActivity(context, 0, intent2, 0);

 Intent intent3= new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+phones.get(0).trim())); 
 PendingIntent objPendingCall = PendingIntent.getActivity(context, 0, intent3, 0);

 Intent intent4= new Intent(context, Snooze.class);
 intent4.putExtra("id", notificationIdentifier);
 intent4.putExtra("name", name);
 PendingIntent objPendingSnooze = PendingIntent.getActivity(context, 0, intent4, 0);

 notification = new Notification.Builder(context).
                setContentTitle(notificationTitle).
                setContentText(notificationMessage+" "+name).
                setAutoCancel(true).setSmallIcon(icon).
                setWhen(System.currentTimeMillis()).
                addAction(android.R.drawable.ic_menu_call,"call",objPendingCall).
                addAction(android.R.drawable.ic_menu_delete, "Cancel", objPendingCancel).
                addAction(android.R.drawable.ic_lock_idle_alarm,"Snooze",objPendingSnooze).
                setContentIntent(objPendingIntent).
                build();
            notification.defaults |= Notification.DEFAULT_SOUND;
            notification.defaults |= Notification.DEFAULT_VIBRATE;
            notification.defaults |= Notification.DEFAULT_LIGHTS;
            final NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify(notificationIdentifier, notification);
 }

当我点击任何一个通知的通话按钮时,只会先操作一个。请帮助我获取特定通知的ID。提前致谢

1 个答案:

答案 0 :(得分:0)

您的“notificationIdentifier”对于每个通知都应该是唯一的。

您可以将通知代码放在方法中:

    public void notification (int notificationIdentifier){
notification = new Notification.Builder(context).
                setContentTitle(notificationTitle).
                setContentText(notificationMessage+" "+name).
                setAutoCancel(true).setSmallIcon(icon).
                setWhen(System.currentTimeMillis()).
                addAction(android.R.drawable.ic_menu_call,"call",objPendingCall).
                addAction(android.R.drawable.ic_menu_delete, "Cancel", objPendingCancel).
                addAction(android.R.drawable.ic_lock_idle_alarm,"Snooze",objPendingSnooze).
                setContentIntent(objPendingIntent).
                build();
            notification.defaults |= Notification.DEFAULT_SOUND;
            notification.defaults |= Notification.DEFAULT_VIBRATE;
            notification.defaults |= Notification.DEFAULT_LIGHTS;
            final NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify(notificationIdentifier, notification);  
    };

并使用以下方法调用方法:

notification (notificationIdentifier);

如果id没有任何意义,您可以使用随机数:

Random rand = new Random();
notificationIdentifier = rand.nextInt(1000000) + 1;