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();
}
}