繁体   English   中英

在Android API 26中发送本地通知

[英]Send Local Notifications in Android API 26

我想在Android应用中安排简单的本地通知。

当我以API 24为目标时,这工作得很好,但是由于我必须移至最后一个API才能更新我的应用程序,因此我必须在代码中进行一些更改。

当前 ,我使用WakefulBroadcastReceiver和一个简单的Service发送通知。

每当我想发送通知时,我都会将所有信息发送给我的WakefulBroadcastReceiver一个Intent ,然后在onReceive方法中创建一个服务并调用startWakefulService(context, service);

WakefulBroadcastReceiver确保在触发通知之前,不会WakefulBroadcastReceiver该进程,并且该服务在后台执行繁重的工作。

但是现在由于最新的Android版本具有新的后台限制 ,我无法在我的应用程序在后台时简单地启动服务,因此每次发送通知的尝试都会使我的应用程序崩溃: java.lang.IllegalStateException:不允许启动服务:应用程序在后台 我还注意到现在不赞成使用WakefulBroadcastReceiver

设置闹钟时:

Intent broadcastIntent = new Intent(context, NotifBroadcastReceiver.class);
// ...
alarmIntent = PendingIntent.getBroadcast(context, idNotification, broadcastIntent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP,
    millis, interval, alarmIntent);

收到警报时:(在NotifBroadcastReceiver类中)

// onReceive method
startWakefulService(context, service);
// the service then does the heavy job and fires the notification...

我的问题:

  1. 如何通过确保CPU在整个过程中保持清醒状态,如何使用简单的BroadcastReceiver (而不是WakefulBroadcastReceiver )发送通知?
  2. 如何更改代码以完成后台服务现在要做的繁重工作?

我已经阅读了JobScheduler ,但似乎无法在这种情况下使用 ,因为它无法保证我将在确切的时间运行

通知是我应用程序中非常重要的一部分,因此,如果不再触发通知,则该应用程序实际上是无用的...

我最终使用了IntentJobService因为它与Pre-O的Service相同,而Jobs用于IntentJobService

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM