0

I want to send a notification to the local user at a specific date and time. I am using the alarm manager and Broadcast Receiver for this.

Here I schedule my notification:

private void createNotification (String name, String date) {
        int year = Integer.parseInt(date.substring(0,4));
        int month = Integer.parseInt(date.substring(4,6));
        int day = Integer.parseInt(date.substring(6,8));
        int hour = Integer.parseInt(date.substring(8,10));
        int min = Integer.parseInt(date.substring(10,12));

        AlarmManager mAlarm = (AlarmManager) act.getSystemService(Context.ALARM_SERVICE);
        Calendar calendar = Calendar.getInstance();
        calendar.set(year,month,day,hour,min,0);
        calendar.add(Calendar.HOUR_OF_DAY, -4);

        Intent intent = new Intent("partypooper.DISPLAY_NOTIFICATION");
        intent.putExtra("eventName", name);
        intent.putExtra("eventDate", hour+":"+min);
        PendingIntent broadcast = PendingIntent.getBroadcast(getContext(), 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        mAlarm.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), broadcast);
    } 

Here is my manifest where I have the receiver for the PendingIntent:

<receiver android:name=".Notification.NotificationReceiver">
     <intent-filter>
         <action android:name="partypooper.DISPLAY_NOTIFICATION"/>
         <action android:name="partypooper.eventName"/>
         <action android:name="partypooper.eventTime"/>
     </intent-filter>
</receiver>

I then have a class NotificationReceiver which extends BroadcastReceiver where I take action to display the notification inside the onReceive override method. However, this method never gets called for some reason. I have troubleshooted this problem and looked at many similar ways to implement this, however I still can't figure out why it doesn't work

Any help would be appreciated, thanks.

kobemoe
  • 43
  • 5
  • 1
    Since Oreo, you must use an explicit `Intent` for your manifest-registered Receiver. That is, `... new Intent(act, NotificationReceiver.class)`. You can still set your action `String` on that `Intent` – with `setAction()` – if you need to check that in the Receiver, but it's not required. You can then remove the `` from the `` element in the manifest. This will work on all Android versions, btw; it's just been enforced since Oreo. – Mike M. Nov 21 '19 at 02:18
  • Possible duplicate of [LocalNotification with AlarmManager and BroadcastReceiver not firing up in Android O (oreo)](https://stackoverflow.com/questions/45815899/localnotification-with-alarmmanager-and-broadcastreceiver-not-firing-up-in-andro) – Mike M. Nov 21 '19 at 11:18
  • No problem. Please select the "That solved my problem!" option, when you can, so that this can be marked as resolved. Glad you got it working. Cheers! – Mike M. Nov 21 '19 at 11:18

0 Answers0