Skip to content

Instantly share code, notes, and snippets.

@Ayush783
Created February 10, 2024 16:05
Show Gist options
  • Save Ayush783/194be1ba2aace6d778961334db907844 to your computer and use it in GitHub Desktop.
Save Ayush783/194be1ba2aace6d778961334db907844 to your computer and use it in GitHub Desktop.
Re triggering a notification on Android
@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