同时发出多个警报通知

时间:2019-09-02 09:00:23

标签: android notifications alarmmanager reminders

我的应用需要在每次吃药时显示警报通知。如果要同时服用两片药,则应显示两个通知。现在,如果药丸时间不同,我的应用程序可以显示单独的通知。问题是如果它们同时显示,则只显示最后一个。

我尝试了StackOverflow的一些解决方案。但任何事情对我都不起作用。

....................
    Intent intent = new Intent(AddReminder.this, LocalNotification.class);
                AlarmManager alm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

      int id = dbHelper.getAlarmCount() + 1;
     dbHelper.addToLocalNotification(id, dateStr, getResources().getString(R.string.reminder), descp,
                            param,"");
                    intent.putExtra("title",getResources().getString(R.string.reminder));
                    intent.putExtra("desc",descp);
                    PendingIntent pi = PendingIntent.getBroadcast(
                            getApplicationContext(), id, intent, 0);
                    setAlarmValue(alm,dateStr,pi);
.....................................................

    private void setAlarmValue(AlarmManager alm, String date, PendingIntent pi){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            try {
                Date dateVal = sdf.parse(date);
                String repeat = s1.get(weiderholen.getSelectedItemPosition()).toLowerCase();

                if (repeat.equals("daily")) {
                    alm.setRepeating(AlarmManager.RTC_WAKEUP, dateVal.getTime(), AlarmManager.INTERVAL_DAY, pi);
                } else if (repeat.equals("weekly")) {
                    alm.setRepeating(AlarmManager.RTC_WAKEUP, dateVal.getTime(), AlarmManager.INTERVAL_DAY * 7, pi);
                } else if (repeat.equals("monthly")) {
                    alm.setRepeating(AlarmManager.RTC_WAKEUP, dateVal.getTime(), AlarmManager.INTERVAL_DAY * 30, pi);
                } else if (repeat.equals("yearly")) {
                    alm.setRepeating(AlarmManager.RTC_WAKEUP, dateVal.getTime(), AlarmManager.INTERVAL_DAY * 365, pi);
                } else {
                    alm.set(AlarmManager.RTC_WAKEUP, dateVal.getTime(), pi);
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }

和我的广播接收器

public class LocalNotification extends BroadcastReceiver {
    int m;
    public void onReceive(Context context, Intent intent) {
        m = (int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE);

        NotificationManager mNotificationManager;

        Notification notification = getNotification(context,intent.getStringExtra("title"),intent.getStringExtra("desc"));


        mNotificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            /* Create or update. */
            NotificationChannel channel = new NotificationChannel("my_channel_01",
                    "Channel human readable title",
                    NotificationManager.IMPORTANCE_DEFAULT);
            mNotificationManager.createNotificationChannel(channel);
        }
        mNotificationManager.notify(m, notification);


    }

    @TargetApi(Build.VERSION_CODES.O)
    public Notification getNotification(Context context, String title, String message){
        long when = System.currentTimeMillis();
        Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
//        Intent viewIntent = new Intent(context, DashBoardActivity.class);
        Intent viewIntent = new Intent(context, SplashActivity.class);
        viewIntent.putExtra("TRIGGERED_FROM", "NOTIFICATION_TASK");
        PendingIntent pendingIntent = PendingIntent.getActivity(context, m, viewIntent, PendingIntent.FLAG_ONE_SHOT);



        Uri uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context,"my_channel_01")
                .setSmallIcon(R.mipmap.applogo)
                .setContentTitle(title)
                .setContentText(message).setSound(alarmSound)
                .setAutoCancel(true).setWhen(when)
                .setContentIntent(pendingIntent);

        Notification notification = mBuilder.build();
        notification.flags=Notification.FLAG_AUTO_CANCEL;
        return notification;
    }

}

1 个答案:

答案 0 :(得分:0)

问题出在 def valid_for_custom_authentication?(password) self.has_role?(:admin) end mNotificationManager.notify(m, notification);同时与通知相同,因此请创建一个静态通知ID,该ID将在onReceive方法中进行更新。

onReceive方法之上

m

在您的onReceive增量中,此ID

`private static int NOTIFICATION_ID = 1; `

然后传递此ID

NOTIFICATION_ID++

希望它能起作用! :)