GcmBroadcastReceiver IllegalStateException:不允许启动服务 Intent

GcmBroadcastReceiver IllegalStateException: Not allowed to start service Intent

我正在 Android 中处理 FCM 推送通知,我在那里遇到了这个异常:

GcmBroadcastReceiver IllegalStateException: Not allowed to start service Intent

我在这个论坛上搜索了很多问题,但仍然没有得到解决的帮助。我的日志和清单补丁也在下面给出。

清单:

<uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

<receiver android:name="com.parse.GcmBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND">
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
                <category android:name="com.kolbeh" />
            </intent-filter>
        </receiver>
        <meta-data android:name="com.parse.push.gcm_sender_id"
            android:value="id:85490######" />

        <service android:name="com.parse.PushService" />

        <receiver
            android:name="dinewhere.fcm.CustomPushReceiver"
            android:exported="false">
            <intent-filter>
                <action android:name="com.parse.push.intent.RECEIVE" />
                <action android:name="com.parse.push.intent.OPEN" />
                <action android:name="com.parse.push.intent.DELETE" />
            </intent-filter>
        </receiver>

错误日志:

10-16 16:52:19.621 25906-25906/com.kolbeh E/AndroidRuntime: FATAL EXCEPTION: main
                                                            Process: com.kolbeh, PID: 25906
                                                            java.lang.RuntimeException: Unable to start receiver com.parse.GcmBroadcastReceiver: java.lang.IllegalStateException: Not allowed to start service Intent { act=com.google.android.c2dm.intent.RECEIVE flg=0x1000010 pkg=com.kolbeh cmp=com.kolbeh/com.parse.PushService (has extras) }: app is in background uid UidRecord{2ac0a5c u0a888 RCVR idle procs:1 seq(0,0,0)}
                                                                at android.app.ActivityThread.handleReceiver(ActivityThread.java:3259)
                                                                at android.app.ActivityThread.-wrap17(Unknown Source:0)
                                                                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1677)
                                                                at android.os.Handler.dispatchMessage(Handler.java:105)
                                                                at android.os.Looper.loop(Looper.java:164)
                                                                at android.app.ActivityThread.main(ActivityThread.java:6541)
                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
                                                             Caused by: java.lang.IllegalStateException: Not allowed to start service Intent { act=com.google.android.c2dm.intent.RECEIVE flg=0x1000010 pkg=com.kolbeh cmp=com.kolbeh/com.parse.PushService (has extras) }: app is in background uid UidRecord{2ac0a5c u0a888 RCVR idle procs:1 seq(0,0,0)}
                                                                at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1505)
                                                                at android.app.ContextImpl.startService(ContextImpl.java:1461)
                                                                at android.content.ContextWrapper.startService(ContextWrapper.java:644)
                                                                at android.content.ContextWrapper.startService(ContextWrapper.java:644)
                                                                at com.parse.ServiceUtils.runIntentInService(ServiceUtils.java:37)
                                                                at com.parse.ServiceUtils.runWakefulIntentInService(ServiceUtils.java:68)
                                                                at com.parse.GcmBroadcastReceiver.onReceive(GcmBroadcastReceiver.java:21)
                                                                at android.app.ActivityThread.handleReceiver(ActivityThread.java:3252)
                                                                at android.app.ActivityThread.-wrap17(Unknown Source:0) 
                                                                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1677) 
                                                                at android.os.Handler.dispatchMessage(Handler.java:105) 
                                                                at android.os.Looper.loop(Looper.java:164) 
                                                                at android.app.ActivityThread.main(ActivityThread.java:6541) 
                                                                at java.lang.reflect.Method.invoke(Native Method) 
                                                                at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 

您 运行 Android 8.0+,targetSdkVersion 26+。 You cannot reliably call startService() from the background,例如来自 GCM 接收器。相反,您应该:

  • 切换到startForegroundService()并使用前台服务,或者

  • 切换到JobIntentService

在您的特定情况下,调用 startService() 的代码似乎来自 Parse。您应该查看是否有将 Android 8.0 考虑在内的 Parse 客户端更新。