如何在OnReceive中使用不同的通知链接多个alarmManagers?

时间:2015-12-22 18:26:42

标签: android notifications broadcastreceiver

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

例如,当我们到达存储在某个alarmManager中的独立日期时,我想显示“独立日”,并且当我们到达他的死亡日期时显示“史蒂夫工作死亡”存储在另一个警报管理器中,等等上。

所以我的问题是如何用不同的通知链接每个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);

2 个答案:

答案 0 :(得分:2)

您可以在安排闹钟时将该信息与意图一起传递。

Doctrine\DBAL\Connection

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

您也可以传递通知消息,而不是传递整数值,而不需要任何if ... else条件。

传递字符串数据:

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

获取字符串:

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

答案 1 :(得分:1)

你可以做出不同的意图并制作

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));