Android:BroadcastReceiver不会听BOOT_COMPLETED

时间:2019-11-04 16:46:19

标签: android push-notification broadcastreceiver reboot

我的应用推送了每日通知(此消息运行正常),但是在设备重启后通知不会再次触发。

我正在尝试将无法监听BOOT_COMPLETED的BroadcastReceiver设置为无效。

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

我在AndroidManifest中将引导接收器设置为false,并在创建警报时通过代码将其设置为true,这在官方文档中有建议(这是为了让您仅在需要时才听完引导)。

启动通知的位置:

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

    ...
}

显然,代码中的所有内容都很好,但是重新启动设备不会再次推送通知。

尝试使用华为Android 9设备和较旧的Android 4.4手机。没有一个。

有帮助吗?

PS。 TMNotification.scheduleNewThoughtAlarm方法无关紧要,因为它可以正常工作(设置简单的Toast甚至不会在onReceive中显示)。

PS2。太奇怪了,但是完全遵循此处的官方文档也不会使它们失真:https://developer.android.com/training/scheduling/alarms.html#java(标题为“设备重新启动时启动警报”)。

1 个答案:

答案 0 :(得分:0)

设置多个intent-filer action可能会有限制。因此,将您的定义更改为下一个。

<!-- 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>

对于所有其他意图过滤器,请使用单独的Receiver。现在您可以使用您的方法接收事件。仅打印日志,以确保偶数即将到来。很少有设备可以检查它不是特定于设备的。

public class AlarmRebootReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context oContext, Intent intent) {
           Log.d("", "Event Received");
           /**
            * .....
            **/
    }
}