AlarmManager is not triggered when App is closed
Asked Answered
A

3

5

I have some in-App notification to show to users at a specific time but nothing is shown when the App is closed.

Setting alarm:

Intent alarmIntent = new Intent(mMotherActivity, ReminderAlarmManager.class);

if (ReminderNotificationType.CHANGE_LENS.equals(notificationType)) {
    alarmIntent.putExtra("NOTIFICATION_TYPE", "REMINDER");
} else {
    alarmIntent.putExtra("NOTIFICATION_TYPE", "ORDER");
}

long scTime = alarmDate.getTime();
PendingIntent pendingIntent = PendingIntent.getBroadcast(mMotherActivity, 0, alarmIntent, 0);
AlarmManager alarmManager = (AlarmManager) mMotherActivity.getSystemService(mMotherActivity.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, scTime, pendingIntent);

broadcast receiver:

public class ReminderAlarmManager extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        String notificationType = intent.getStringExtra("NOTIFICATION_TYPE");
        if(notificationType.equalsIgnoreCase("ORDER"))
        {
            Intent i = new Intent(context, OrderReminderNotificationService.class);
            context.startService(i);
        }
        else if(notificationType.equalsIgnoreCase("REMINDER"))
        {
            Intent i = new Intent(context, ReminderNotificationService.class);
            context.startService(i);
        }
    }
}

So when it comes to scTime, even if the App is closed, I would like to trigger a notification. So I'm calling a service by the PendingIntent, as follows:

public class OrderReminderNotificationService extends IntentService {

    public OrderReminderNotificationService(String name) {
        super(name);
    }

    @Override
    protected void onHandleIntent(Intent intent) {

        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);

        Context context = getApplicationContext();
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
            context);

        Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        mBuilder.setContentTitle("Notification");
        mBuilder.setContentText(context.getString(R.string.renewOrderNotificationMsg));
        mBuilder.setSmallIcon(R.mipmap.ic_launcher);
        mBuilder.setSound(uri);
        mBuilder.setAutoCancel(true);

        Intent notificationIntent = new Intent(this, LoginActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
        mBuilder.setContentIntent(contentIntent);
        mNotificationManager.notify(2, mBuilder.build());
}

The part in the manifest concerning that:

<receiver android:name="com.company.utils.ReminderAlarmManager">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

But nothing shows up... What am I doing wrong ?

Amorita answered 11/10, 2016 at 14:31 Comment(5)
Are you calling service from BroadcastReceiver ?Amygdala
I've edited my post with full code. I've posted the BroadCastreceiver and completer the setting of the AlarmManager. Should I do something in the manifest on BOOT_COMPLETED something like that ?Amorita
your Manifest file is needed too. I mean did you register your broadcastReceiver and service in Manifest?Amygdala
I've just registered the broadcast receiver as you can see (edited). Should I register the both services also ?Amorita
Yes, service also needs to be thereAmygdala
A
4

Your AndroidManifest.xml should be like this:

<receiver android:name=".ReminderAlarmManager">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

<service android:name=".OrderReminderNotificationService "/>
Amygdala answered 11/10, 2016 at 14:52 Comment(3)
Just one more question. By telling to execute the Receiver on BOOT_COMPLETED, I understand that it will be triggered when the devide will boot. But I would like to fire the alarmat a specific time passed to the alarm manager, even if the App is closed ? It should work that way right ?Amorita
All alarms are deleted when the phone is turned off. Therefore, we have a BOOT_COMPLETED action that system sends to all receivers that has it in their intenr-filter. That's how we know the system was rebooted and we need to reset the alarms. However, if you don't care about the deletion of alarms after reboot, then you don't need intent-filter with BOOT_COMPLETED. Your alarms will be fired even if the app was closedAmygdala
2021 This does not work anymore. AlarmManager will not trigger if App is closed!Androgen
C
7

Your device won't allow you to trigger the alarm since it need to save some battery power. So, go to SETTINGS ->Battery/power saver->Apps then choose your app, after that select "no restriction"

Crapulent answered 3/10, 2020 at 12:50 Comment(1)
anyway to redirect to this settings via the app, coz i want it programiticallyPecker
A
4

Your AndroidManifest.xml should be like this:

<receiver android:name=".ReminderAlarmManager">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

<service android:name=".OrderReminderNotificationService "/>
Amygdala answered 11/10, 2016 at 14:52 Comment(3)
Just one more question. By telling to execute the Receiver on BOOT_COMPLETED, I understand that it will be triggered when the devide will boot. But I would like to fire the alarmat a specific time passed to the alarm manager, even if the App is closed ? It should work that way right ?Amorita
All alarms are deleted when the phone is turned off. Therefore, we have a BOOT_COMPLETED action that system sends to all receivers that has it in their intenr-filter. That's how we know the system was rebooted and we need to reset the alarms. However, if you don't care about the deletion of alarms after reboot, then you don't need intent-filter with BOOT_COMPLETED. Your alarms will be fired even if the app was closedAmygdala
2021 This does not work anymore. AlarmManager will not trigger if App is closed!Androgen
B
2

The alarm manager doesn't trigger the Intent services on certain phones. I was testing the app on a Oneplus 3T the entire day before I realized there was nothing wrong with the code. Just tested the same app on a Moto G, and it works as expected.

Burschenschaft answered 2/7, 2017 at 14:16 Comment(2)
2021 This is ridiculous, gmail notifications work fine!Androgen
@Androgen that's totally ridiculous yeah , the reason why it works fine on gmail because they are whitelisted in the system already to execute the normal way but when it comes to app that devs make , that's a no no , they keep us in struggle looking for workarounds ..Acis

© 2022 - 2025 — McMap. All rights reserved.