gpt4 book ai didi

Android oreo - 推送通知崩溃

转载 作者:行者123 更新时间:2023-11-29 14:30:02 27 4
gpt4 key购买 nike

我们现在将我们的应用程序从 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)}

最佳答案

尽管建议迁移到 Firebase,但这里是为无法升级的用户提供的解决方案。

  1. 将构建工具设置为 26.0.1+
  2. 确保不再扩展唤醒广播,而只是普通广播
  3. 创建一个扩展 JobServiceIntent 的类并为其安排任务。

使用 Job Service 可以在 Oreo 的后台工作。

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

希望对您有所帮助!

关于Android oreo - 推送通知崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46735052/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com