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").
android:permission="android.permission.RECEIVE_BOOT_COMPLETED"
? – Parricide<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
before application tag? – ParricideApparently everythings fine in code but rebooting the device won't push notification again.
It is unclear if you receive the BOOT_COMPLETED broadcast. – Parricide