Created
February 10, 2024 16:05
-
-
Save Ayush783/194be1ba2aace6d778961334db907844 to your computer and use it in GitHub Desktop.
Re triggering a notification on Android
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
@SuppressLint("NotificationTrampoline") | |
void reTriggerNotification(Context context, Bundle extras, StatusBarNotification notification, String channelId, NotificationManager nm) { | |
NotificationCompat.Builder nb = new NotificationCompat.Builder(context, channelId); | |
Notification n = notification.getNotification(); | |
// ... | |
// Set all attributes you want on your notification. | |
// Set unique group IDs if you're disabling Notification bundling | |
// Set custom layouts, stale notification etc | |
// ... | |
// Now you have to create a new ID for this notification | |
// If you push it to the old ID then Android will de-prioritize it | |
// Moving this notification down in the tray | |
nb.setPriority(NotificationCompat.PRIORITY_MAX); | |
try { | |
int notificationId = (int)(Math.random() * 100); | |
Notification notif = nb.build(); | |
nm.cancel(notification.getId()); | |
nm.notify(notificationId, notif); | |
} catch (Exception e) { | |
// Handle exception | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment