faizy
faizy

Reputation: 739

Multiple Alarm notification at same time

My app needs to display alarm notification whenever it is time to take pills. If two pills are to be taken at the same time, then two notifications shall appear. Right now my app can display separate notifications if the pill times are different. The problem is if they are at the same time it only displays the one the last one.

I tried some solutions from StackOverflow. but anything doesn't work for me.

....................
    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();
            }
        }

and my Broadcast receiver

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

}

Upvotes: 1

Views: 390

Answers (1)

M.AQIB
M.AQIB

Reputation: 383

the problem is in mNotificationManager.notify(m, notification); The m is same for the notification on the same time so create a static notification id which will be updated in the onReceive method.

Above onReceive method

 `private static int NOTIFICATION_ID = 1; `

In your onReceive increment this id

NOTIFICATION_ID++

Then pass this ID

mNotificationManager.notify(NOTIFICATION_ID, notification);

Hope it works! :)

Upvotes: 1

Related Questions