Android: BroadcastReceiver won't listen to BOOT_COMPLETED
Asked Answered
R

1

6

My app pushes a daily notification (this is working correctly) but after a device reboot notification won't fire again.

I'm trying to set a BroadcastReceiver that listens to BOOT_COMPLETED to no avail.

AndroidManifest.xml:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
...
<receiver android:name=".helpers.notification.AlarmRebootReceiver"
    android:enabled="false">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

BroadcastReceiver:

public class AlarmRebootReceiver extends BroadcastReceiver {

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

        try {    
            Intent notificationIntent = new Intent("android.media.action.DISPLAY_NOTIFICATION");
            notificationIntent.addCategory("android.intent.category.DEFAULT");

            String notificationMessage = TMLocale.getStringResourceByName("reminder_newthought");
            String notificationTitle = TMLocale.getStringResourceByName("app_name");

            TMNotification.Notify(notificationTitle, notificationMessage, Enum.ArtAndWordsNotification.NEWTHOUGHT, oContext);

            TMNotification.cancelNewThoughtAlarm(oContext);
            scheduleNewThoughtAlarm(oContext);
        } catch (Exception e) {
            ExceptionHandler.logException(e);
        }
    }

    private void scheduleNewThoughtAlarm(Context oContext) {

        Calendar cal = Calendar.getInstance();
        int day = cal.get(Calendar.DAY_OF_MONTH);
        int month = cal.get(Calendar.MONTH);
        int year = cal.get(Calendar.YEAR);
        int hour = cal.get(Calendar.HOUR_OF_DAY);
        int minutes = cal.get(Calendar.MINUTE) + 1;
        int seconds = cal.get(Calendar.SECOND);

        Calendar newCalendar = TMDate.generateCalendar(day, month, year, hour, minutes, seconds);
        TMNotification.scheduleNewThoughtAlarm(oContext, newCalendar, null);
    }
}

I set the boot receiver enabled to false in AndroidManifest and set it to true by code when alarm is created, as it suggest in official docs (this is so you listen to boot completed only when needed).

Where notification is launched:

private static void doNewThoughtSchedule(Context oContext, int reminderPreference, boolean randomNotificationsPreference,
                                             Calendar calendar, Intent notificationIntent){

    ComponentName receiver = new ComponentName(ApplicationContext.get(), AlarmRebootReceiver.class);
    PackageManager pm = ApplicationContext.get().getPackageManager();

    pm.setComponentEnabledSetting(receiver,
            PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
            PackageManager.DONT_KILL_APP);

    ...
}

Apparently everythings fine in code but rebooting the device won't push notification again.

Tried in a Huawei Android 9 device as well as an older Android 4.4 phone. None worked.

Any help?

PS. TMNotification.scheduleNewThoughtAlarm method is irrelevant as it is working fine (setting a simple toast won't even show in onReceive).

PS2. It's weird, but following exactly the official docs here doesn't wort either: https://developer.android.com/training/scheduling/alarms.html#java (where title is "Start an alarm when the device restarts").

Reneta answered 4/11, 2019 at 16:46 Comment(5)
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" ?Parricide
You have <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> before application tag?Parricide
Apparently everythings fine in code but rebooting the device won't push notification again. It is unclear if you receive the BOOT_COMPLETED broadcast.Parricide
Thank you guys, forgot to mention that BOOT_COMPLETED request permission is already in AndroidManifest. Edited question.Reneta
Thanks @blackapps. The only way I know to check if broadcast is received or not is to set a toast in it. Tried and toast is not shown, but it is shown on the first alarm before rebooting the device so I guess the BOOT_COMPLETED broadcast is not being received.Reneta
S
1

There might be restriction with setting up several intent-filer action. So change you defining to the next.

<!-- Don't forget about permission -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>


<!-- ... -->
<receiver android:name=".helpers.notification.AlarmRebootReceiver"
    android:permission="android.permission.RECEIVE_BOOT_COMPLETED">

            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
            </intent-filter>
</receiver>

For all other intent filters use separate Receiver. Now you could receive events in the your method. Just print logs, to ensure the even is coming. And few devices to check it's not a device specific.

public class AlarmRebootReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context oContext, Intent intent) {
           Log.d("", "Event Received");
           /**
            * .....
            **/
    }
}
Servo answered 4/11, 2019 at 18:2 Comment(6)
Thank you @GensaGames. Edited again. I already has simplified version of receiver in AndroidManifest and enabled to false as suggested in official docs. Then I set enabled to true by code when alarm is created. This is the suggested way to do it by Google. Still not working.Reneta
@DiegoPerez It's working right now, with my devices on Android O and emulator Q. Check what you have missed.Servo
No way @GensaGames. I almost give up :( ... I have a Huawei Android 9 and a "Blu" Android 4.4 devices and in none of both works. I hope I missed something but cannot figure out what because checked over and over again several times. I'll still investigating, but meanwhile I appreciate very much your help!Reneta
@DiegoPerez Install emulator, with any of those version to check it.Servo
Such a stupid thing @Servo I'd like to delete the question, but I will hold back and won't do it. What happened in the end is onReceive was crashing and I wasn't noticing it. It was crashing because I'm getting texts for notification in onReceive in a method that uses a global static context, which is obviously cleared after a reboot. When used the context that arrives to onReceive it started working as expected. Anyway, I won't create an answer with this, but will mark yours as correct because in fact it is, and because I appreciate your effort.Reneta
@DiegoPerez That is why I said to print logs, in onReceive.Servo

© 2022 - 2024 — McMap. All rights reserved.