Skip to content

Commit f326470

Browse files
authored
Merge pull request #910 from OneSignal/fix/crash-on-samsung
Intent flag FLAG_CANCEL_CURRENT may crash on some samsung devices
2 parents d93260b + c331cb0 commit f326470

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

OneSignalSDK/onesignal/src/main/java/com/onesignal/NotificationRestorer.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929

3030
import android.app.AlarmManager;
3131
import android.app.PendingIntent;
32-
import android.app.NotificationManager;
3332
import android.app.job.JobInfo;
3433
import android.app.job.JobScheduler;
3534
import android.content.ComponentName;
@@ -230,8 +229,7 @@ private static Intent addRestoreExtras(Intent intent, Cursor cursor) {
230229

231230
private static final int RESTORE_NOTIFICATIONS_DELAY_MS = 15_000;
232231
static void startDelayedRestoreTaskFromReceiver(Context context) {
233-
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
234-
// NotificationRestorer#restore is Code-sensitive to Android O
232+
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
235233
OneSignal.Log(OneSignal.LOG_LEVEL.INFO, "scheduleRestoreKickoffJob");
236234

237235
// set the job id to android notif id - that way we don't restore any notif twice
@@ -251,12 +249,15 @@ static void startDelayedRestoreTaskFromReceiver(Context context) {
251249
intentForService.setComponent(new ComponentName(context.getPackageName(),
252250
NotificationRestoreService.class.getName()));
253251

252+
// KEEP - PendingIntent.FLAG_UPDATE_CURRENT
253+
// Some Samsung devices will throw the below exception otherwise.
254+
// "java.lang.SecurityException: !@Too many alarms (500) registered"
254255
PendingIntent pendingIntent = PendingIntent.getService(context,
255-
RESTORE_KICKOFF_REQUEST_CODE, intentForService, PendingIntent.FLAG_CANCEL_CURRENT);
256+
RESTORE_KICKOFF_REQUEST_CODE, intentForService, PendingIntent.FLAG_UPDATE_CURRENT);
256257

257258
long scheduleTime = System.currentTimeMillis() + RESTORE_NOTIFICATIONS_DELAY_MS;
258259
AlarmManager alarm = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
259260
alarm.set(AlarmManager.RTC, scheduleTime, pendingIntent);
260261
}
261262
}
262-
}
263+
}

0 commit comments

Comments
 (0)