-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(android): restore local notifications after device reboot (#3027)
- Loading branch information
Showing
6 changed files
with
145 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
.../src/main/java/com/getcapacitor/plugin/notification/LocalNotificationRestoreReceiver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.getcapacitor.plugin.notification; | ||
|
||
import android.content.BroadcastReceiver; | ||
import android.content.Context; | ||
import android.content.Intent; | ||
import android.os.UserManager; | ||
|
||
import com.getcapacitor.CapConfig; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Date; | ||
import java.util.List; | ||
|
||
import static android.os.Build.VERSION.SDK_INT; | ||
|
||
public class LocalNotificationRestoreReceiver extends BroadcastReceiver { | ||
|
||
@Override | ||
public void onReceive(Context context, Intent intent) { | ||
if (SDK_INT >= 24) { | ||
UserManager um = context.getSystemService(UserManager.class); | ||
if (um == null || !um.isUserUnlocked()) return; | ||
} | ||
|
||
NotificationStorage storage = new NotificationStorage(context); | ||
List<String> ids = storage.getSavedNotificationIds(); | ||
|
||
ArrayList<LocalNotification> notifications = new ArrayList<>(ids.size()); | ||
ArrayList<LocalNotification> updatedNotifications = new ArrayList<>(); | ||
for (String id : ids) { | ||
LocalNotification notification = storage.getSavedNotification(id); | ||
if(notification == null) { | ||
continue; | ||
} | ||
|
||
LocalNotificationSchedule schedule = notification.getSchedule(); | ||
if(schedule != null) { | ||
Date at = schedule.getAt(); | ||
if(at != null && at.before(new Date())) { | ||
// modify the scheduled date in order to show notifications that would have been delivered while device was off. | ||
long newDateTime = new Date().getTime() + 15 * 1000; | ||
schedule.setAt(new Date(newDateTime)); | ||
notification.setSchedule(schedule); | ||
updatedNotifications.add(notification); | ||
} | ||
} | ||
|
||
notifications.add(notification); | ||
} | ||
|
||
if(updatedNotifications.size() > 0){ | ||
storage.appendNotifications(updatedNotifications); | ||
} | ||
|
||
CapConfig config = new CapConfig(context.getAssets(), null); | ||
LocalNotificationManager localNotificationManager = new LocalNotificationManager(storage, null, context, config); | ||
|
||
localNotificationManager.schedule(null, notifications); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters