ramya nayak
ramya nayak

Reputation: 19

Android oreo - Push notification crash

We migrated our application now from Android 25 to

compileSdkVersion 26
buildToolsVersion "26.0.0"

I have modified all the dependencies version as well .

We are still using GCM.jar in our application .

When we receive push notification on Oreo Device , Application crashes.

Crash logs:

Caused by: java.lang.IllegalStateException: Not allowed to start service Intent { act=com.google.android.c2dm.intent.RECEIVE flg=0x1000010 app is in background uid UidRecord{13c38ce u0a297 RCVR bg:+3m5s626ms idle procs:1 seq(0,0,0)}

Upvotes: 1

Views: 5051

Answers (2)

Ayyappa
Ayyappa

Reputation: 1984

Even though its recommended to move to Firebase, here is the solution for those who can't upgrade.

  1. Set build tools to 26.0.1+
  2. Make sure you no more extent Wakeful Broadcast but instead just normal broadcast
  3. Create a class which extends JobServiceIntent and schedule a task to it.

With Job Service its possible to work in the background in Oreo.

Here is the sample of JobServiceIntent class

package com.voxelbusters.nativeplugins.features.notification.core;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.JobIntentService;

import com.google.android.gms.gcm.GoogleCloudMessaging;
import com.voxelbusters.nativeplugins.defines.CommonDefines;
import com.voxelbusters.nativeplugins.utilities.Debug;
import com.voxelbusters.nativeplugins.utilities.JSONUtility;

import org.json.JSONObject;

/**
 * Created by ayyappa on 09/02/18.
 */

public class NotificationJobService extends JobIntentService
{
    /**
     * Unique job ID for this service.
     */
    static final int JOB_ID = 1000;

    /**
     * Convenience method for enqueuing work in to this service.
     */
    public static void enqueueWork(Context context, Intent work) {
        enqueueWork(context, NotificationJobService.class, JOB_ID, work);
    }

    @Override
    protected void onHandleWork(Intent intent)
    {

        Bundle extras = intent.getExtras();
        GoogleCloudMessaging service = GoogleCloudMessaging.getInstance(this);

        String messageType = service.getMessageType(intent);

        Debug.log(CommonDefines.NOTIFICATION_TAG, "GCMIntentService received message type : " + messageType);

        if (!extras.isEmpty())
        {
            if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType))
            {
                // Post notification of the received message (extras).

            }
        }
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
    }

}

Now call enqueWork on receiving a broadcast as below.

package com.voxelbusters.nativeplugins.features.notification.serviceprovider.gcm;

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;

import com.voxelbusters.nativeplugins.features.notification.core.NotificationDefines;
import com.voxelbusters.nativeplugins.features.notification.core.NotificationJobService;

public class GCMBroadcastReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        if (!NotificationDefines.usesExtenralRemoteNotificationService(context))
        {
            // Explicitly specify that GCMIntentService will handle the intent.
            ComponentName comp = new ComponentName(context.getPackageName(), GCMIntentService.class.getName());
            intent.setComponent(comp);
            NotificationJobService.enqueueWork(context, intent);
        }
    }
}

Hope it helps!

Upvotes: 7

Sudheesh R
Sudheesh R

Reputation: 1775

Please use Goolgle-Cloud-Messaging gradle dependency as well in your gradle. Or the best solution is to move to Firebase. Firebase have tons of tools like GCM.

As per the official docs: Firebase Cloud Messaging (FCM) is the new version of GCM. It inherits the reliable and scalable GCM infrastructure, plus new features! If you are integrating messaging in a new app, start with FCM. GCM users are strongly recommended to upgrade to FCM, in order to benefit from new FCM features today and in the future.

For migrating to Firebase, refer here

Upvotes: 1

Related Questions