LocalNotification with AlarmManager and BroadcastReceiver not firing up in Android O (oreo)

匿名 (未验证) 提交于 2019-12-03 01:27:01

问题:

Hello I've got my local notifications running on androids prior to SDK 26

But in a Android O I've got the following warning, and the broadcast receiver is not fired.

W/BroadcastQueue: Background execution not allowed: receiving Intent { act=package.name.action.LOCAL_NOTIFICATION cat=[com.category.LocalNotification] flg=0x14 (has extras) } to package.name/com.category.localnotifications.LocalNotificationReceiver

From what I've read broadcast receivers are more restricted in android O, but if so, how should I schedule the broadcast if I want it launching even if the main activity is not running?

Should I use services instead of receivers?

This is the AlarmManager launch code:

public void Schedule(String aID, String aTitle, String aBody, int aNotificationCode, long aEpochTime) {     Bundle lExtras = new Bundle();     lExtras.putInt("icon", f.getDefaultIcon());     lExtras.putString("title", aTitle);     lExtras.putString("message", aBody);     lExtras.putString("id", aID);     lExtras.putInt("requestcode", aNotificationCode);      Intent lIntent =        new Intent(LocalNotificationScheduler.ACTION_NAME)       .addCategory(NotificationsUtils.LocalNotifCategory)       .putExtras(lExtras);      PendingIntent lPendIntent = PendingIntent.getBroadcast(f.getApplicationContext(), aNotificationCode,                                                            lIntent, PendingIntent.FLAG_UPDATE_CURRENT);      AlarmManager lAlarmMgr = (AlarmManager) f.getSystemService(Context.ALARM_SERVICE);     lAlarmMgr.set(AlarmManager.RTC, 1000, lPendIntent); }

This is the receiver code:

public class LocalNotificationReceiver extends BroadcastReceiver {  public static native void   nativeReceiveLocalNotification (String aID, String aTitle, String aMessage, boolean aOnForeground );  /** This method receives the alarms set by LocalNotificationScheduler, *   notifies the CAndroidNotifications c++ class, and (if needed) ships a notification banner  */ @Override public void onReceive(Context aContext, Intent aIntent) {     Toast.makeText(context, text, duration).show(); }

}

This is the manifest:

                                                 

回答1:

Android O are pretty new to-date. Hence, I try to digest and provide as accurate possible information.

From https://developer.android.com/about/versions/oreo/background.html#broadcasts

  • Apps that target Android 8.0 or higher can no longer register broadcast receivers for implicit broadcasts in their manifest.
    • Apps can use Context.registerReceiver() at runtime to register a receiver for any broadcast, whether implicit or explicit.
  • Apps can continue to register explicit broadcasts in their manifest.

Also, in https://developer.android.com/training/scheduling/alarms.html , the examples are using explicit broadcast, and doesn't mention anything special regarding Android O.


May I suggest you try out explicit broadcast as follow?

public static void startAlarmBroadcastReceiver(Context context, long delay) {     Intent _intent = new Intent(context, AlarmBroadcastReceiver.class);     PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, _intent, 0);     AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);     // Remove any previous pending intent.     alarmManager.cancel(pendingIntent);        
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!