Die*_*rez 6 android reboot broadcastreceiver push-notification
我的应用程序推送每日通知(这是正常工作),但设备重启后通知不会再次触发。
我正在尝试设置一个 BroadcastReceiver 来监听 BOOT_COMPLETED 无济于事。
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>
Run Code Online (Sandbox Code Playgroud)
广播接收器:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
我在 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);
...
}
Run Code Online (Sandbox Code Playgroud)
显然代码中一切正常,但重新启动设备不会再次推送通知。
在华为 Android 9 设备以及较旧的 Android 4.4 手机中尝试过。没有一个工作。
有什么帮助吗?
附注。TMNotification.scheduleNewThoughtAlarm 方法是无关紧要的,因为它工作正常(设置一个简单的 toast 甚至不会在 onReceive 中显示)。
PS2。这很奇怪,但完全遵循此处的官方文档也不行:https : //developer.android.com/training/scheduling/alarms.html#java(其中标题是“设备重启时启动警报”)。
设置多个 . 可能会受到限制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>
Run Code Online (Sandbox Code Playgroud)
对于所有其他意图过滤器,请使用单独的Receiver. 现在您可以在您的方法中接收事件。只需打印日志,以确保事件即将发生。几乎没有设备可以检查它不是特定于设备的。
public class AlarmRebootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context oContext, Intent intent) {
Log.d("", "Event Received");
/**
* .....
**/
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2605 次 |
| 最近记录: |