【问题标题】:Android: Background execution not allowed: receiving IntentAndroid:不允许后台执行:接收意图
【发布时间】:2020-10-07 02:08:28
【问题描述】:

我正在尝试使用警报管理器发送通知,该代码在较低的 SDK 版本(如下面的 26)上运行良好。 android 隐式后台禁令不让通知广播。

在下面找到广播接收器的代码:

public class AlarmReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {

System.out.println("AlarmReceiver-Worked");

MainActivity.initNotificationChannels(context);
Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, "default")
       .setDefaults(Notification.DEFAULT_ALL)
       .setSmallIcon(R.mipmap.ic_launcher)                                      
       .setContentTitle(intent.getStringExtra("title"))
       .setContentText(intent.getStringExtra("text"))
       .setContentIntent(pendingIntent)
       .setPriority(Notification.PRIORITY_MAX);

NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
  }
 }

Manifext.xml

<receiver android:name="com.x.Controllers.Notification.AlarmReceiver"
          android:enabled="true"
          android:exported="true">
  <intent-filter>
     <action android:name="android.media.action.DISPLAY_NOTIFICATION" />
     <category android:name="android.intent.category.DEFAULT" />
  </intent-filter>
</receiver>

【问题讨论】:

    标签: java android android-intent notifications native


    【解决方案1】:

    首先

    无法克服这个错误

    哪个错误?没有日志,没有信息什么不起作用...这个BroadcastReceiver 注册了哪些操作?这是给BOOT_COMPLETED 还是别的什么?

    除此之外

    private static boolean notificationChannelsInitialized = false;
    
    if (notificationChannelsInitialized) {
        return;
    }
    

    这里总是return,你在这个方法后面将这个标志设置为true,它永远不会发生。

    也不要使用static 标志,只需检查Channel 是否已经存在

    NotificationChannel channel = manager.getNotificationChannel("default");
    notificationChannelsInitialized = Build.VERSION.SDK_INT < 26 || channel != null;
    

    在低于 26 的 API 上假设它在那里

    【讨论】:

    • 不允许后台执行:接收 Intent { act=android.media.action.DISPLAY_NOTIFICATION cat=[android.intent.category.DEFAULT] flg=0x14 (has extras) } 到 com.x/。 Controllers.Notification.AlarmRecei 版本
    • 为什么要创建notificationIntentstackBuilder?两者都没有在代码中的任何地方使用...pendingIntent 是基于intent 创建的,这是onReceive 的参数,并且不应再次为舒尔重新广播...尝试将pendingIntent 创建替换为这一行PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_ONE_SHOT);。另请阅读如何正确提问 SO,当发生此错误时,我仍然不放心,您的描述很混乱...Notification 是否显示?
    • 好吧,您已经向this error 提出了问题,但没有准确描述正在发生的事情,也没有发布异常日志。下一个而不是在您已发布登录评论的已编辑问题中...我可以在您的代码中看到许多问题,两个指向答案/建议和下一个在评论中,您是否尝试修复它们?
    • 这里你有下一个问题:BOOT_COMPLETED 会将你的static boolean notificationChannelsInitialized 设置为true 并且因为onResume 中的第二个if 这个方法不会产生任何下一个Notification只要这个static 字段将被保存在内存中。我建议您创建单独的方法来检查和创建Channel,然后始终运行代码以显示Notification,而无需任何if。现在尝试删除 static 字并检查它是否有效,接下来清理你的代码.. 我只是想帮助你,但你发布了不完整的问题,有很多错误
    • 我几乎改变了我的代码中的所有内容,基本思想是不使用 BOOT_COMPLETED,只是为了测试。此外,该错误是由于 Android API 26 及更高版本中的隐式意图禁令。我正在尝试不同的解决方案,但它们对我不起作用。
    【解决方案2】:
    Intent notificationIntent = new Intent(context, AlarmReceiver.class);                           
    notificationIntent.setAction("android.media.action.DISPLAY_NOTIFICATION");
    notificationIntent.putExtra("title", notificationData.getString("title"));
    notificationIntent.putExtra("text", notificationData.getString("text"));
    
    PendingIntent broadcast = PendingIntent.getBroadcast(this.context, i, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), broadcast);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多