Android oreo - 推送通知崩溃

问题描述 投票:1回答:2

我们现在将我们的应用程序从Android 25迁移到

compileSdkVersion 26
buildToolsVersion "26.0.0"

我也修改了所有依赖项版本。

我们仍然在我们的应用程序中使用GCM.jar。

当我们在Oreo设备上收到推送通知时,应用程序崩溃了。

崩溃日志:

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)}
android google-cloud-messaging android-8.0-oreo
2个回答
7
投票

即使它建议迁移到Firebase,这里也是那些无法升级的解决方案。

  1. 将构建工具设置为26.0.1+
  2. 确保你没有更多程度的唤醒广播,而只是正常的广播
  3. 创建一个扩展JobServiceIntent并为其安排任务的类。

通过Job Service可以在奥利奥的后台工作。

以下是JobServiceIntent类的示例

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();
    }

}

现在在接收广播时调用enqueWork,如下所示。

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);
        }
    }
}

希望能帮助到你!


1
投票

请在您的gradle中使用Goolgle-Cloud-Messaging gradle依赖项。或者最好的解决方案是迁移到Firebase。 Firebase拥有大量像GCM这样的工具。

根据official docs:Firebase云消息传递(FCM)是GCM的新版本。它继承了可靠且可扩展的GCM基础架构以及新功能!如果要在新应用程序中集成消息传递,请从FCM开始。强烈建议GCM用户升级到FCM,以便从当前和未来的新FCM功能中受益。

要迁移到Firebase,请参阅here

© www.soinside.com 2019 - 2024. All rights reserved.