如何在 OnReceive 中 link 多个具有不同通知的 alarmManager?

how to link multiple alarmManagers with different notifications in OnReceive?

我想在到达特定的 alarmManager 时触发不同的通知。

例如我想在某个alarmManager中存储的独立日期显示"independence day",在另一个AlarmManager中存储的死亡日期显示"Steve jobs died" ,等等。

所以我的问题是如何 link 每个 alarmManager(cal 和 cal2)有不同的通知?

我一直这样做到现在,

在 MainActivity 中:

Calendar cal = Calendar.getInstance();
cal.set(Calendar.DATE,19);  
cal.set(Calendar.MONTH,11);  
cal.set(Calendar.YEAR,2015);
cal.set(Calendar.HOUR_OF_DAY, 21); 
cal.set(Calendar.MINUTE, 42);       
cal.set(Calendar.SECOND, 30);

Calendar cal2 = Calendar.getInstance();
cal2.set(Calendar.DATE,19);  
cal2.set(Calendar.MONTH,11);  
cal2.set(Calendar.YEAR,2015);
cal2.set(Calendar.HOUR_OF_DAY, 21);  
cal2.set(Calendar.MINUTE, 43);     
cal2.set(Calendar.SECOND, 10);


Intent alertIntent = new Intent(this, AlertReceiver.class);
AlarmManager alarmManager = (AlarmManager)  getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), PendingIntent.getBroadcast(this, 1, alertIntent, PendingIntent.FLAG_UPDATE_CURRENT));
alarmManager.set(AlarmManager.RTC_WAKEUP, cal2.getTimeInMillis(), PendingIntent.getBroadcast(this, 2, alertIntent, PendingIntent.FLAG_UPDATE_CURRENT));

请注意,我已将日期设置为触发通知并且它正在运行,onReceive 中的通知被触发了两次,第一次是在我到达 "cal" 时,第二次是在我到达时"cal2"。但是正如我上面提到的,我需要在到达 cal2

时触发不同的通知

这是 OnReceive:

public class AlertReceiver extends BroadcastReceiver {


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

     createNotification(context, "title1", "independence day", "event of today");
       //here i want to link this notification with AlarmManager containing Cal as a date
        createNotification(context, "title2", "steve jobs died", "event of today");
        //here i want to link this notification with AlarmManager containing Cal2 as a date




    }

    public void createNotification(Context context, String msg, String msgText, String msgAlert) {
        PendingIntent notificIntent = PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 0);

        NotificationCompat.Builder mBuilder=new NotificationCompat.Builder(context)
                .setSmallIcon(R.drawable.not)
                .setContentTitle(msg)
                .setTicker(msgAlert)
                .setContentText(msgText);
        //intent to fire when notification clicked on
        mBuilder.setContentIntent(notificIntent);
        //how the person will be notified
        mBuilder.setDefaults(NotificationCompat.DEFAULT_SOUND);
        //cancel notification when clicked in the taskbar
        mBuilder.setAutoCancel(true);
        NotificationManager mNotificationManager= (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(0,mBuilder.build());


    }

应用下面提到的内容后:我完成了这 4 个事件:

AlarmManager alarmManager = (AlarmManager)  getSystemService(Context.ALARM_SERVICE);
        Intent alertIntent = new Intent(this, AlertReceiver.class);
        alertIntent.putExtra("title", "event1");
        alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), PendingIntent.getBroadcast(this, 1, alertIntent, PendingIntent.FLAG_UPDATE_CURRENT));

        Intent alertIntent2 = new Intent(this, AlertReceiver.class);
        alertIntent2.putExtra("title", "event2");
        alarmManager.set(AlarmManager.RTC_WAKEUP, cal2.getTimeInMillis(), PendingIntent.getBroadcast(this, 2, alertIntent2, PendingIntent.FLAG_UPDATE_CURRENT));

        Intent alertIntent3 = new Intent(this, AlertReceiver.class);
        alertIntent3.putExtra("title", "event3");
        alarmManager.set(AlarmManager.RTC_WAKEUP, cal3.getTimeInMillis(), PendingIntent.getBroadcast(this, 3, alertIntent3, PendingIntent.FLAG_UPDATE_CURRENT));

        Intent alertIntent4 = new Intent(this, AlertReceiver.class);
        alertIntent4.putExtra("title", "event4");
        alarmManager.set(AlarmManager.RTC_WAKEUP, cal4.getTimeInMillis(), PendingIntent.getBroadcast(this, 4, alertIntent3, PendingIntent.FLAG_UPDATE_CURRENT));

并在 OnReceive 中:

 createNotification(context,intent.getStringExtra("title"), "event", " event");

有时是第一次运行应用程序所有事件都出现,有时第三次出现两次,前两次不出现,有时只出现最后一个,所以有错误或混乱在我的代码中, 有什么问题吗?

日历:

Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DATE,24);  
        cal.set(Calendar.MONTH,11);
        cal.set(Calendar.YEAR,2015);
        cal.set(Calendar.HOUR_OF_DAY, 23);  
        cal.set(Calendar.MINUTE, 55);     
        cal.set(Calendar.SECOND, 10);

        Calendar cal2 = Calendar.getInstance();
        cal2.set(Calendar.DATE,24);  
        cal2.set(Calendar.MONTH,11);  
        cal2.set(Calendar.YEAR,2015);
        cal2.set(Calendar.HOUR_OF_DAY, 23);  
        cal2.set(Calendar.MINUTE, 55);       
        cal2.set(Calendar.SECOND, 20);

        Calendar cal3 = Calendar.getInstance();
        cal3.set(Calendar.DATE,24);  
        cal3.set(Calendar.MONTH, 11); 
        cal3.set(Calendar.YEAR,2015);
        cal3.set(Calendar.HOUR_OF_DAY, 23); 
        cal3.set(Calendar.MINUTE, 55);     
        cal3.set(Calendar.SECOND, 30);

        Calendar cal4 = Calendar.getInstance();
        cal4.set(Calendar.DATE,24);  
        cal4.set(Calendar.MONTH, 11); 
        cal4.set(Calendar.YEAR,2015);
        cal4.set(Calendar.HOUR_OF_DAY, 23);  
        cal4.set(Calendar.MINUTE, 55);      
        cal4.set(Calendar.SECOND, 40);

您可以在安排警报时随意图一起传递该信息。

Intent alertIntent = new Intent(this, AlertReceiver.class);
alertIntent.putExtra("Notification Key", 1);
AlarmManager alarmManager = (AlarmManager)  getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), PendingIntent.getBroadcast(this, 1, alertIntent, PendingIntent.FLAG_UPDATE_CURRENT));

然后在您的 onReceive 中检查值并显示基于该值的通知:

Integer notificationId  = intent.getIntExtra("Notification Key", -1);
if (notificationId == 1)
{
  // Show indipendente day
}
else
{
   // do something else
}

除了传递整数值,您还可以传递通知消息并直接显示它,而无需任何 if...else 条件。

传递字符串数据:

alertIntent.putExtra("Notification Key", "Independence day");

取回字符串:

String message = intent.getStringExtra("Notification Key");
// Calling the notification
createNotification(context, "title1", message, "event of today");

你可以做出不同的意图,使

intent.putExtra("title","independence day"); 

并为每个通知创建一个单独的 ID,您可以这样做。

alertIntent.putExtra("id", 1);

并在 onReceive 中更改为:

createNotification(context, intent.getStringExtra("title"), "event", " event",intent.getIntExtra("id", 0));

和方法

 public void createNotification(Context context, String msg, String msgText, String msgAlert,int id) {

//你的代码并在下面更改这一行

    mNotificationManager.notify(id,mBuilder.build());
}

所以它看起来像这样:

 AlarmManager alarmManager = (AlarmManager)  getSystemService(Context.ALARM_SERVICE);
    Intent alertIntent = new Intent(this, MyReceiver.class);
    alertIntent.putExtra("title", "event1");
    alertIntent.putExtra("id", 1);
    alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), PendingIntent.getBroadcast(this, 1, alertIntent, PendingIntent.FLAG_UPDATE_CURRENT));

    Intent alertIntent2 = new Intent(this, MyReceiver.class);
    alertIntent2.putExtra("title", "event2");
    alertIntent2.putExtra("id", 2);
    alarmManager.set(AlarmManager.RTC_WAKEUP, cal2.getTimeInMillis(), PendingIntent.getBroadcast(this, 2, alertIntent2, PendingIntent.FLAG_UPDATE_CURRENT));

    Intent alertIntent3 = new Intent(this, MyReceiver.class);
    alertIntent3.putExtra("title", "event3");
    alertIntent3.putExtra("id", 3);
    alarmManager.set(AlarmManager.RTC_WAKEUP, cal3.getTimeInMillis(), PendingIntent.getBroadcast(this, 3, alertIntent3, PendingIntent.FLAG_UPDATE_CURRENT));

    Intent alertIntent4 = new Intent(this, MyReceiver.class);
    alertIntent4.putExtra("title", "event4");
    alertIntent4.putExtra("id", 4);
    alarmManager.set(AlarmManager.RTC_WAKEUP, cal4.getTimeInMillis(), PendingIntent.getBroadcast(this, 4, alertIntent4, PendingIntent.FLAG_UPDATE_CURRENT));