3

I am trying to show a notification once a day at exact time. I am using Evernote Android Job library for this https://github.com/evernote/android-job. The time is coming from backend API. Below code is working fine when app is in foreground or background. But when app is killed for long time (~1min) or after reboot I am not getting notification on One Plus 5T(Nougat) and also on Oppo device(Mashmallow). Also, sometimes when I relaunch the app, I get all the failed notifications at once. Everything's working fine on Asus Zenfone Max (Mashmallow). Alarms are triggering in every scenario in Asus (foreground, background, screen locked or even reboot)

Below is adb log for One Plus 5T

//when app is killed and before alarm scheduled time

Shikhars-MacBook-Pro:HOGAndroid_New shikhardeep$ adb shell dumpsys alarm | grep "com.tf.eros.faythTv"
    ELAPSED_WAKEUP #0: Alarm{3a8c0ae type 2 when 1118303576 com.tf.eros.faythTv}
      tag=*walarm*:com.tf.eros.faythTv/com.evernote.android.job.v14.PlatformAlarmReceiver
      operation=PendingIntent{e233e4f: PendingIntentRecord{217c9dc com.tf.eros.faythTv broadcastIntent}}
    ELAPSED_WAKEUP #0: Alarm{44439ba type 2 when 1118303589 com.tf.eros.faythTv}
      tag=*walarm*:com.tf.eros.faythTv/com.evernote.android.job.v14.PlatformAlarmReceiver
      operation=PendingIntent{a7f706b: PendingIntentRecord{a45a7c8 com.tf.eros.faythTv broadcastIntent}}
  u0a355:com.tf.eros.faythTv +1s718ms running, 4 wakeups:
      *walarm*:com.tf.eros.faythTv/com.evernote.android.job.v14.PlatformAlarmReceiver

//when app is killed and just after the alarm scheduled time

Shikhars-MacBook-Pro:HOGAndroid_New shikhardeep$ adb shell dumpsys alarm | grep "com.tf.eros.faythTv"
  u0a355:com.tf.eros.faythTv +1s722ms running, 6 wakeups:
      *walarm*:com.tf.eros.faythTv/com.evernote.android.job.v14.PlatformAlarmReceiver

Code

public class ShowHoroscopeNotificationJob extends Job {

public static final String TAG = "HOROSCOPE_NOTIFICATION";

public ShowHoroscopeNotificationJob() {
    super();
}

@Override
protected void onReschedule(int newJobId) {
    super.onReschedule(newJobId);
}

@NonNull
@Override
protected Result onRunJob(@NonNull Params params) {
    try {

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(getContext(), "HOG_NOTIFICATION")
                        .setSmallIcon(R.drawable.notification_icon)
                        .setLargeIcon(BitmapFactory.decodeResource(getContext().getResources(), R.mipmap.ic_launcher))
                        .setColor(ContextCompat.getColor(getContext(), R.color.colorPrimary))
                        .setContentTitle("Horoscope Reminder")
                        .setContentText("Check out Daily Horoscope !!!")
                        .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                        .setAutoCancel(true);

        NotificationManager notificationManager = (NotificationManager) getContext().getSystemService(Context.NOTIFICATION_SERVICE);

        if (notificationManager != null) {
            notificationManager.notify(new Random().nextInt(40) + 1, mBuilder.build()); 
            schedule(getExactTime(ZPreferences.getHoroscopeReminderTime(getContext()))); //again calling schedule() as setExact() can't be periodic 
            return Result.SUCCESS;
       }

    } catch (Exception e) {

        e.printStackTrace();
        return Result.RESCHEDULE;
    }
}

public static void schedule(long exactInMs) {

    if (exactInMs != 0L) {

        new JobRequest.Builder(ShowHoroscopeNotificationJob.TAG)
                .setExact(exactInMs)
                .build()
                .schedule();
    }

}
Shikhar Deep
  • 253
  • 2
  • 8
  • 19
  • 1
    Do you find any solution for this? I am also facing same issue. – Nik Apr 17 '19 at 13:56
  • no..try using WorkManager. It's part of Android. This problem occurs mostly in chinese phones like One Plus, Oppo, Vivo – Shikhar Deep Apr 17 '19 at 14:01
  • can we schedule workmanager for specific date and time as like alarm manager? – Nik Apr 17 '19 at 14:10
  • https://stackoverflow.com/questions/50299814/how-to-schedule-notifications-using-workmanager/55706333#55706333. According to this link AlarmManager is best choice but other answers also mention WorkManager – Shikhar Deep Apr 17 '19 at 14:18
  • I tried with Work Manager but in One Plus 6T it is not working when Application is killed. (in background). Can you help me in this? – Nik Apr 18 '19 at 06:50
  • try this: https://medium.com/mindorks/enable-background-services-in-chinese-roms-32e73dfba1a6 – Shikhar Deep Apr 18 '19 at 06:51

0 Answers0