GcmBroadcastReceiver IllegalStateException: Not al

2019-01-25 08:32发布

问题:

I am working on FCM Push notification in Android, where I am getting this exception:

GcmBroadcastReceiver IllegalStateException: Not allowed to start service Intent

I have searched many question in this forum, but still didn't got help for solving it. My Log and Manifest patch is also given below.

Manifest:

<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>

Error Log:

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) 

回答1:

You are running on Android 8.0+, with a targetSdkVersion of 26+. You cannot reliably call startService() from the background, such as from a GCM receiver. Instead, you should either:

  • Switch to startForegroundService() and use a foreground service, or

  • Switch to JobIntentService

In your particular case, the code that is calling startService() appears to be from Parse. You should see if there is an update to the Parse client that takes Android 8.0 into account.