9

I develop an app that uses AlarmManager to set a bunch alarms (usually around 50) that need to be fired at a certain time during the year. This is the code I'm using since 4.4 kitkat changed the AlarmManager.

AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
long setDate = fireDate.getTime(); // it's a calendar date defined above
Intent intent = new Intent(LOCAL_DISPLAY_MESSAGE_ACTION);
PendingIntent pending = PendingIntent.getBroadcast(ctx,
                            id, intent, PendingIntent.FLAG_UPDATE_CURRENT);

if (Build.VERSION.RELEASE.startsWith("6")) {
    am.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, setDate, pending);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
    am.setExact(AlarmManager.RTC_WAKEUP, setDate, pending);
} else {
    am.set(AlarmManager.RTC_WAKEUP, setDate, pending);
}

Apart from the code above I'm using a broadcast receiver properly defined in the manifest.

public class LocalReceiver extends BroadcastReceiver {

    public void onReceive(Context context, Intent intent) {
        PushWakeLocker.acquire(context);

        // do some stuff

        PushWakeLocker.release();
    }
}

More info thay might help.

<uses-sdk
    android:minSdkVersion="10"
    android:targetSdkVersion="19" />

Since a few months ago I've been getting bad reviews only from Samsung devices (5.0 /5.1 android version) that don't get their local notifications at all. I mean, it's not firing the alarm, it seems that the device skips it or does not wake up.

In the tests, mainly with a Samsung S4 with 5.0.1, I always get the alarms on time, so this is driving me crazy. FYI this code has always worked pretty fine.

I researched a lot about this but unfortunately I got no helpful information. It's not that they get the alarm with delay (as I've read in some threads), it's that they don't get it at all. So this is not about the known issue in lollipop and alarmmanager.

I appreciate your time and any suggestion is welcomed!

Fran Palomares
  • 134
  • 1
  • 8
  • "This is the code I'm using" -- I'd change your `if` to check `Build.VERSION_CODES.M`. "I'm using a broadcast receiver" -- your `WakeLock` there is unnecessary, as `onReceive()` is called in a system-supplied `WakeLock` when triggered by `AlarmManager`. However, unless "do some stuff" is only a couple of milliseconds, you should move that logic into an `IntentService` and use a `WakefulBroadcastReceiver`. Beyond that, Samsung (like SONY and others) has their own Doze mode sort of setup pre-6.0, so `AlarmManager` is unreliable unless you get added to some device-specific whitelist. – CommonsWare Feb 13 '16 at 13:30

2 Answers2

15

Your problem (or nightmare) is the Samsung Smart Manager. This app comes pre-installed with all Samsung phones since 2015 and is supposed to deactivate Apps that are unused. "How does it know which apps are not used?" You may ask - simple:

Every app that has not been launched by the user for 3 days gets deactivated.

All remaining AlarmManager entries - of course - also get deleted. You can read about it on their "Developer Forums". Feel free to follow here or here until these threads get deleted by the staff. I have yet to see someone from Samsung respond to the topic.

The only way to "fix" this is to inform the Users of your app about the situation and show them how to whitelist your app in Smart Manager. We've had to setup a website with step-by-step instructions showing how to do this for our users.

You may think about setting up a background service, or calling AlarmManager every six hours or so - none of these hacks will work.

saibotd
  • 741
  • 8
  • 20
  • 2
    Thanks for sharing your finding with me! You saved my day. It's really annoying that Samsung does such a thing and takes no responsibility about it. smh. – Fran Palomares Mar 23 '16 at 07:52
  • 4
    UPDATE: If your package name contains "alert" or "alarm", Smart Manager will not deactivate the app. More here: http://stackoverflow.com/a/34085645/128092 – saibotd Jun 10 '16 at 15:28
  • Do you know whether using JobScheduler instead of AlarmManager helps in this case? – jakk Sep 06 '17 at 13:38
  • I don't understand anything. In my Samsung device this isn't consistent at all. I have an alarm application that doesn't always work, but when I put it to sleep in power settings and set some alarms for testing, they work. How come? – user3289695 Mar 16 '18 at 08:05
0

When devices are re-starterd it clears alarms so your on boot alarm has to set them up again.