11

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) 
halfer
  • 19,824
  • 17
  • 99
  • 186
Usman Khan
  • 3,739
  • 6
  • 41
  • 89

1 Answers1

14

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Would changing the targetSdkVersion to 25 fix this? I'm asking as I cannot change the call methods, but I can change the SDK target. – clee2005 Nov 14 '17 at 19:51
  • @clee2005: "Would changing the targetSdkVersion to 25 fix this?" -- AFAIK, yes. – CommonsWare Nov 14 '17 at 19:53
  • 1
    I have this problem: the startService() is called from Parse. Does anyone know if a recent version of the Parse lib fixes this problem ? – matdev Jan 19 '18 at 11:32
  • @CommonsWare when I use startForegroundService() my notification are disappearing after service is done. However, I wanna keep it there, since I using it to receive notifications from Google. Any suggestions? – Oximer Jul 13 '18 at 14:47
  • @Oximer: "since I using it to receive notifications from Google" -- a `Notification` has nothing to do with FCM, if that is what you mean by "receive notifications from Google". – CommonsWare Jul 13 '18 at 21:15
  • @CommonsWare I have code in eclipse any solution for GCM ?. because I am not able to move on FCM and not upgrade code for the android studio. – Mr X Nov 01 '18 at 06:12
  • @MrX: There has been no official Google support for Eclipse in a few years, and I have not used it personally recently either. Sorry! – CommonsWare Nov 01 '18 at 10:43
  • @CommonsWareI found a solution at here, I need to update v4 / v13 lib https://stackoverflow.com/questions/19051088/wakefulbroadcastreceiver-cannot-be-resolved-to-a-type – Mr X Nov 01 '18 at 11:32