8

I have an app with GCM push notifications. Notifications are send from CMS.
My appTargetSdkVersion=27.
Everything works OK except two facts:

  1. app doesn't receive notifications when it is killed on devices with Android ver lower than Oreo.

  2. 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);
            }
        }
    }
}
KKrzyzek
  • 101
  • 1
  • 6
  • Using a debugger or `Log` statements, where is the work breaking down? Does the receiver still receive the broadcast? Does `onHandleWork()` get called? – CommonsWare Mar 05 '18 at 18:58
  • Unfortunately, now i do not have an access to notification sending platform. I will try it tomorrow, thanks for guidance. Btw I have a suspicion about the cause of this problem. In original code I have an if statement in JobIntentService - before executing sendNotification method, which checks language - if(!Locale.getDefault().getDisplayLanguage().equals("polski")). Is it possible that app stops there - because it cannot get language when app is killed and that is why notification is not shown? – KKrzyzek Mar 05 '18 at 20:15
  • Any reason why you're not using FCM instead? – AL. Mar 06 '18 at 14:52
  • @AL. migration to FCM might be expensive, it requires changes in server-side code – hornet2319 Jul 25 '18 at 12:54
  • @hornet2319 [Not that much change if I remember correctly](https://stackoverflow.com/a/37517339/4625829) – AL. Jul 25 '18 at 15:08
  • As of April 10, 2018, Google has deprecated GCM. The GCM server and client APIs are deprecated and will be removed as soon as April 11, 2019. Migrate GCM apps to Firebase Cloud Messaging (FCM), which inherits the reliable and scalable GCM infrastructure, plus many new features. – parvez rafi Aug 10 '18 at 19:52

0 Answers0