KKr*_*zek 8 service notifications android receiver google-cloud-messaging
我有一个带有GCM推送通知的应用程序.通知从CMS发送.
我的appTargetSdkVersion = 27.
一切正常,除了两个事实:
应用程序在Android ver低于Oreo的设备上被杀死时不会收到通知.
在奥利奥我必须打开设备屏幕才能接收通知.
我不知道为什么它会像那样.
*在BroadcastReceiver的onReceive方法中,我将enqueueWork和启动JobIntentService onHandleWork方法发布通知.
*在我使用WakefulBroadcastReceiver + startWakefulService + IntentService之前,它运行良好.但是我改变了这个实现,因为在将targetSdk升级到27个通知后停止在Oreo上工作(我知道它是由后台服务限制引起的).
编辑:
我通过将通知通道参数IMPORTANCE从IMPORTANCE_DEFAULT更改为IMPORTANCE_HIGH来修复此问题!
我的GcmReceiver类:
public class GcmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
ComponentName comp = new ComponentName(context.getPackageName(),
GcmNotificationService.class.getName());
GcmNotificationService.enqueueWork(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
Run Code Online (Sandbox Code Playgroud)
}
我的JobIntentService类:
public class GcmNotificationService extends JobIntentService {
private static final String TAG = "GcmNotificationService";
private Random random = new Random();
static final int JOB_ID = 1000;
static void enqueueWork(Context context, Intent work) {
enqueueWork(context, GcmNotificationService.class, JOB_ID, work);
}
@Override
protected void onHandleWork(Intent intent) {
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
String messageType = gcm.getMessageType(intent);
if (!extras.isEmpty()) {
if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
Log.e(TAG, "Send error: " + extras.toString());
} else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
Log.e(TAG, "Deleted messages on server: " + extras.toString());
} else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
String title = (String) extras.get(Config.MESSAGE_TITLE);
String description = (String) extras.get(Config.MESSAGE_TEXT);
boolean isUpdate = false;
try {
isUpdate = 1 == Integer.parseInt((String) extras.get(Config.MESSAGE_UPDATE));
} catch (NullPointerException | NumberFormatException e) {
e.printStackTrace();
}
if (new Preferences(this).getBool(Constant.PREFS_NOTIFICATIONS_ENABLED, true)) {
sendNotification(title, description, isUpdate);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2325 次 |
最近记录: |