1
public class RegisterIntentService extends JobIntentService {


    static void enqueueWork(Context context, Intent work) {
        enqueueWork(context, RegisterIntentService.class, JOB_ID, work);
    }

    @Override
    protected void onHandleWork(@NonNull Intent intent) {
        Timber.d("onHandleIntent");

        String token = null;

        InstanceID id = InstanceID.getInstance(this);
        Registrar registrar = RegistrarFactory.create();

        try {
            token = id.getToken(registrar.getSenderId(), GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);

            Timber.d("received token");
        } catch (Exception e) {
            Timber.e(e, "unable to handle intent");
        }

        Intent registrationComplete = new Intent(Preferences.REGISTRATION_COMPLETE);

        if (token != null) registrationComplete.putExtra(Preferences.REGISTRATION_ID, token);

        this.sendBroadcast(registrationComplete);
    }
}

in Manifest file :

 <service
            android:name="com.tesco.pushnotification.RegisterIntentService"
            android:permission="android.permission.BIND_JOB_SERVICE"
            android:exported="false" />

below exception is getting when I send the push notification when APP is in Background.

> Process: com.tesco.clubcardmobile, PID: 8326
> java.lang.RuntimeException: Unable to start receiver com.google.android.gms.gcm.GcmReceiver:
> java.lang.IllegalStateException: Not allowed to start service Intent {
> act=com.google.android.c2dm.intent.RECEIVE flg=0x1000010
> pkg=com.tesco.clubcardmobile
> cmp=com.tesco.clubcardmobile/com.tesco.pushnotification.GCMListener_Service
> (has extras) }: app is in background uid UidRecord{f0a6ce6 u0a2340
> RCVR idle procs:1 seq(0,0,0)}

using given code i am able to get Push Notification when APP is in Foreground but not in Notification manager. i have changed my code for Android Oreo before Android Oreo i was able to get Notification in background but I don't know why i am not getting When app is in Background after changed API 26 Migration I have changed IntentService to JOBIntentservice please suggest me what i am doing wrong suggest me in given Error what wrong i am doing .

Anil Thakur
  • 123
  • 1
  • 11
  • In Oreo background services won't work the normal way. [Check out the answer with firebase job dispacter](https://stackoverflow.com/questions/52722211/foregroundservice-on-android-oreo-gets-killed/52722651#52722651) – Amit Jangid Oct 10 '18 at 04:14
  • @AmitJangid is there not other way so that we can get Notification in android oreo when APP is in Backgrouund – Anil Thakur Oct 10 '18 at 04:59
  • @AnilThakur There are two types of Firebase notifications - data messages and notification messages. Every app that targets SDK 26 or above (Android O) must implement notification channels and add its notifications to at least one of them. – Ashish Jain Oct 10 '18 at 05:59
  • i have added Notification channel but GcmListenerService is not getting trigger when app is in background can you please suggest me in this – Anil Thakur Oct 10 '18 at 06:08
  • Any reason why you're still implementing GCM instead of FCM? In just a few more months, GCM will be completely discontinued. – AL. Oct 11 '18 at 09:24
  • yes i know we will change next month – Anil Thakur Oct 25 '18 at 16:02

0 Answers0