Will Buffington
Will Buffington

Reputation: 1700

Trying to pass variables to BroadcastReceiver class but it only happens once

Let me explain what I'm doing before I get into the problem.

Ultimately, I'm wanting to send different notifications on certain days of the week every week. I've been able to get it working with a single notification. The problem happens when I try to set up multiple notifications.

So in my MainController's OnCreate method I'm making a call to a function called setAlarm and passing variables:

setAlarm(this, 3, 14, 04, 1, 1);

where 'this' is the context, 3 is a day of the week, 14 is an hour, 04 is a minute, 1 is a second and the last 1 is what I call a notification ID. Here is the setAlarm function:

public void setAlarm(Context context2, int dayofweek, int hourofday, int dayminute, int daysecond, int notiID){

        Intent alarmIntent = new Intent(this, AlarmReceiver.class);
        alarmIntent.putExtra("VALUE", notiID);

        PendingIntent pendingIntent = PendingIntent.getBroadcast(context2, 0, alarmIntent, 0);

        AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(Calendar.DAY_OF_WEEK, dayofweek);
        calendar.set(Calendar.HOUR_OF_DAY, hourofday);
        calendar.set(Calendar.MINUTE, dayminute);
        calendar.set(Calendar.SECOND, daysecond);
        Date date = calendar.getTime();


        //manager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 0, pendingIntent);
        manager.setExact(AlarmManager.RTC_WAKEUP, date.getTime(),pendingIntent);

    }

What I'm doing in this function is setting up the alarm manager to trigger on a certain day of the week, at a certain time. I know once this is triggered it will call the AlarmReceiver class which I've set up. But I'm also passing an integer variable to the AlarmReceiver class through the alarmIntent intent I set up.

The alarm manager works, and triggers on the specified time. Here's the alarmReceiver class I've set up:

public class AlarmReceiver extends BroadcastReceiver {

    private static AlarmReceiver instance = null;

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

        int value = intent.getIntExtra("VALUE", 0);

        if (value == 1){
            NotificationCompat.Builder noti = new NotificationCompat.Builder(context, "notify_001");

            noti.setSmallIcon(R.drawable.clocknoon);
            noti.setContentTitle("Bill Sez...");
            noti.setContentText("First Notification Test!!!");
            noti.setPriority(NotificationCompat.PRIORITY_DEFAULT);

            NotificationManager nm = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
                NotificationChannel channel = new NotificationChannel("notify_001", "Channel title", NotificationManager.IMPORTANCE_DEFAULT);
                nm.createNotificationChannel(channel);
            }

            nm.notify(0, noti.build());
        }

        if (value == 2){
            NotificationCompat.Builder noti = new NotificationCompat.Builder(context, "notify_002");

            noti.setSmallIcon(R.drawable.clocknoon);
            noti.setContentTitle("Bill Sez...");
            noti.setContentText("This is the second notification test");
            noti.setPriority(NotificationCompat.PRIORITY_DEFAULT);

            NotificationManager nm = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
                NotificationChannel channel = new NotificationChannel("notify_002", "Channel title", NotificationManager.IMPORTANCE_DEFAULT);
                nm.createNotificationChannel(channel);
            }

            nm.notify(0, noti.build());
        }


    }
}

As you can see, I'm passing the int variable Value to the class, which works fine as long as I only call the setAlarm function one time. But I'm trying to get this working to set up multiple notifications. What I expect to happen is that I call the setAlarm function with the notification ID (notiID) and at the time specified, the alarm manager triggers and the extra variable value is passed to the alarmReceiver class along with the flow of execution. In the Alarm Receiver class I've got an if statement set up to test the variable 'value' to see if it's 1 or 2 and based on that, a notification will fire and appear on the phone.

What's happening is when I call setAlarm function twice, the first notification is never received. Even more strange, the second notification is received, but it's variable 'value' is 1, not 2 as I expect it to be.

for example: So I call setAlarm and set the alarm to go off at 12:05:01 on Tuesday, and pass notiID of 1. Then I call setAlarm again and set the alarm to go off at 12:06:01 on Tuesday, and pass notiID of 2.

At 12:05:01, nothing happens. I should see the first notification, but I don't. At 12:06:01 I get a notification but instead of the second notification, I see the first one. Likewise if I put a breakpoint in the AlarmReceiver class and look at what 'value' is set to, it shows to be set to 1, not 2.

So I don't get what's happening. I'm hoping anyone experienced in making notifications with Android 8.0.0 and higher can help. Or even suggest a different way to send different notifications on different days.

Upvotes: 0

Views: 37

Answers (1)

N0000B
N0000B

Reputation: 449

Change the following line

PendingIntent pendingIntent = PendingIntent.getBroadcast(context2, 0, alarmIntent, 0);

to

    PendingIntent pendingIntent = PendingIntent.getBroadcast(context2, notiID, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);

Upvotes: 1

Related Questions