I have an app with GCM push notifications. Notifications are send from CMS.
My appTargetSdkVersion=27.
Everything works OK except two facts:
app doesn't receive notifications when it is killed on devices with Android ver lower than Oreo.
On Oreo I must turn on the device screen in order to receive notifications.
I have no clue why it behaves like that.
*In onReceive method of BroadcastReceiver I enqueueWork and start JobIntentService onHandleWork method to post notification.
*Before I used WakefulBroadcastReceiver + startWakefulService + IntentService and it worked fine. But I changed this implementation, because after upgrading targetSdk to 27 notifications stopped working on Oreo (I know it was caused by background service restrictions).
EDIT:
I fixed this issue by changing notification channel parameter IMPORTANCE from IMPORTANCE_DEFAULT to IMPORTANCE_HIGH!
My GcmReceiver class:
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);
}
}
My JobIntentService class:
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);
}
}
}
}