Android:BroadcastReceiver不会收听BOOT_COMPLETED

问题描述 投票:1回答:1

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

我正在尝试将广播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方法无关紧要,因为它可以正常工作(设置简单的吐司甚至不会在onReceive中显示)。

PS2。这很奇怪,但是完全遵循此处的官方文档也不会引起任何问题:https://developer.android.com/training/scheduling/alarms.html#java(标题为“设备重新启动时启动警报”)。

android push-notification broadcastreceiver reboot
1个回答
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");
           /**
            * .....
            **/
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.