Skip to content

Commit

Permalink
fix(local-notifications): Handle case of not allowed exact notificati…
Browse files Browse the repository at this point in the history
…ons (#954)
  • Loading branch information
jcesarmobile authored May 5, 2022
1 parent 9cac196 commit 5016996
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
10 changes: 10 additions & 0 deletions local-notifications/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ npm install @capacitor/local-notifications
npx cap sync
```

## Android

Starting on Android 12, scheduled notifications won't be exact unless this permission is added to your `AndroidManifest.xml`:

```xml
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
```

Note that even if the permission is present, users can still disable exact notifications from the app settings.

## Configuration

<docgen-config>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,11 +349,7 @@ private void triggerScheduledNotification(Notification notification, LocalNotifi
long interval = at.getTime() - new Date().getTime();
alarmManager.setRepeating(AlarmManager.RTC, at.getTime(), interval, pendingIntent);
} else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && schedule.allowWhileIdle()) {
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC, at.getTime(), pendingIntent);
} else {
alarmManager.setExact(AlarmManager.RTC, at.getTime(), pendingIntent);
}
setExactIfPossible(alarmManager, schedule, at.getTime(), pendingIntent);
}
return;
}
Expand All @@ -375,14 +371,30 @@ private void triggerScheduledNotification(Notification notification, LocalNotifi
long trigger = on.nextTrigger(new Date());
notificationIntent.putExtra(TimedNotificationPublisher.CRON_KEY, on.toMatchString());
pendingIntent = PendingIntent.getBroadcast(context, request.getId(), notificationIntent, flags);
setExactIfPossible(alarmManager, schedule, trigger, pendingIntent);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Logger.debug(Logger.tags("LN"), "notification " + request.getId() + " will next fire at " + sdf.format(new Date(trigger)));
}
}

private void setExactIfPossible(
AlarmManager alarmManager,
LocalNotificationSchedule schedule,
long trigger,
PendingIntent pendingIntent
) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && !alarmManager.canScheduleExactAlarms()) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && schedule.allowWhileIdle()) {
alarmManager.setAndAllowWhileIdle(AlarmManager.RTC, trigger, pendingIntent);
} else {
alarmManager.set(AlarmManager.RTC, trigger, pendingIntent);
}
} else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && schedule.allowWhileIdle()) {
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC, trigger, pendingIntent);
} else {
alarmManager.setExact(AlarmManager.RTC, trigger, pendingIntent);
}

SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Logger.debug(Logger.tags("LN"), "notification " + request.getId() + " will next fire at " + sdf.format(new Date(trigger)));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import com.getcapacitor.JSObject;
import com.getcapacitor.Logger;
import java.text.SimpleDateFormat;
Expand Down Expand Up @@ -49,7 +50,11 @@ private boolean rescheduleNotificationIfNeeded(Context context, Intent intent, i
long trigger = date.nextTrigger(new Date());
Intent clone = (Intent) intent.clone();
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, id, clone, PendingIntent.FLAG_CANCEL_CURRENT);
alarmManager.setExact(AlarmManager.RTC, trigger, pendingIntent);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && !alarmManager.canScheduleExactAlarms()) {
alarmManager.set(AlarmManager.RTC, trigger, pendingIntent);
} else {
alarmManager.setExact(AlarmManager.RTC, trigger, pendingIntent);
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Logger.debug(Logger.tags("LN"), "notification " + id + " will next fire at " + sdf.format(new Date(trigger)));
return true;
Expand Down

0 comments on commit 5016996

Please sign in to comment.