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 ?

Marat
  • 6,142
  • 6
  • 39
  • 67
Hubert Solecki
  • 2,611
  • 5
  • 31
  • 63

3 Answers3

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"

Trouble Maker
  • 99
  • 1
  • 5
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 "/>
Marat
  • 6,142
  • 6
  • 39
  • 67
  • 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 ? – Hubert Solecki Oct 11 '16 at 15:17
  • 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 closed – Marat Oct 11 '16 at 15:22
  • 2021 This does not work anymore. AlarmManager will not trigger if App is closed! – user1034912 Aug 30 '21 at 14:18
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.

  • 2021 This is ridiculous, gmail notifications work fine! – user1034912 Aug 30 '21 at 14:22
  • 2
    @user1034912 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 .. – Taki Jul 23 '22 at 03:33