我有一个带有GCM推送通知的应用程序 . 通知从CMS发送 .
我的appTargetSdkVersion = 27 .
一切正常,除了两个事实:
-
app在Android ver低于Oreo的设备上被杀死时不会收到通知 .
-
在奥利奥上我必须打开设备屏幕才能接收通知 .
我不知道为什么它会像那样 .
*在BroadcastReceiver的onReceive方法中,我将enqueueWork和启动JobIntentService onHandleWork方法发布通知 .
*在我使用WakefulBroadcastReceiver startWakefulService IntentService之前它运行良好 . 但是我改变了这个实现,因为在将targetSdk升级到27个通知后停止在Oreo上工作(我知道它是由后台服务限制引起的) .
EDIT:
I fixed this issue by changing notification channel parameter IMPORTANCE from IMPORTANCE_DEFAULT to 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);
}
}
我的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);
}
}
}
}