小编KKr*_*zek的帖子

Android Oreo - BroadcastReceiver + JobIntentService无法正常工作(GCM通知)

我有一个带有GCM推送通知的应用程序.通知从CMS发送.
我的appTargetSdkVersion = 27.
一切正常,除了两个事实:

  1. 应用程序在Android ver低于Oreo的设备上被杀死时不会收到通知.

  2. 在奥利奥我必须打开设备屏幕才能接收通知.

我不知道为什么它会像那样.
*在BroadcastReceiver的onReceive方法中,我将enqueueWork和启动JobIntentService onHandleWork方法发布通知.
*在我使用WakefulBroadcastReceiver + startWakefulService + IntentService之前,它运行良好.但是我改变了这个实现,因为在将targetSdk升级到27个通知后停止在Oreo上工作(我知道它是由后台服务限制引起的).

编辑:
我通过将通知通道参数IMPORTANCE从IMPORTANCE_DEFAULT更改为IMPORTANCE_HIGH来修复此问题!

我的GcmReceiver类:

public class GcmReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    ComponentName comp = new ComponentName(context.getPackageName(),
            GcmNotificationService.class.getName());
    GcmNotificationService.enqueueWork(context, (intent.setComponent(comp)));
    setResultCode(Activity.RESULT_OK);
}
Run Code Online (Sandbox Code Playgroud)

}

我的JobIntentService类:

public class GcmNotificationService extends JobIntentService {

private static final String TAG = "GcmNotificationService";
private Random random = new Random();

static final int JOB_ID = 1000;

static void enqueueWork(Context context, Intent work) {
    enqueueWork(context, GcmNotificationService.class, JOB_ID, …
Run Code Online (Sandbox Code Playgroud)

service notifications android receiver google-cloud-messaging

8
推荐指数
0
解决办法
2325
查看次数