>我们现在将应用程序从Android 25
迁移到
compileSdkVersion 26
buildToolsVersion "26.0.0"
我也修改了所有依赖项版本.
我们仍在使用GCM.jar在我们的应用程序中。
当我们在奥利奥设备上收到推送通知时,应用程序崩溃。
崩溃日志:
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,但对于那些无法升级的人来说,这是解决方案。
- 将生成工具设置为 26.0.1+
- 确保不再扩展唤醒广播,而只是正常广播
- 创建一个扩展 JobServiceIntent 的类,并为其安排任务。
通过工作服务,可以在奥利奥的后台工作。
下面是 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);
}
}
}
希望对您有所帮助!
请在您的 gradle 中使用 Goolgle-Cloud-Messaging gradle 依赖项。或者最好的解决方案是迁移到Firebase。Firebase有大量的工具,如GCM。
根据官方文档:Firebase Cloud Messaging (FCM) 是 GCM 的新版本。它继承了可靠且可扩展的 GCM 基础架构以及新功能!如果您要在新应用中集成消息功能,请从 FCM 开始。强烈建议 GCM 用户升级到 FCM,以便从现在和将来的 FCM 新功能中受益。
要迁移到 Firebase,请参阅此处