Skip to content

Instantly share code, notes, and snippets.

@michaeltys
Created October 10, 2018 15:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaeltys/3aed7ede1066e235602ad202f29ee62f to your computer and use it in GitHub Desktop.
Save michaeltys/3aed7ede1066e235602ad202f29ee62f to your computer and use it in GitHub Desktop.
Showing high importance notifications on different android OS versions
public void showNotification(String message, PendingIntent pendingIntent) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
createNotificationChannel();
}
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, "")
.setSmallIcon(R.drawable.ic_icon)
.setColor(ContextCompat.getColor(getApplicationContext(), R.color.color_primary_blue))
.setContentTitle(getString(R.string.app_name))
.setContentText(message)
.setAutoCancel(true)
.setChannelId("channel_id");
if (pendingIntent != null) {
mBuilder.setContentIntent(pendingIntent);
}
mBuilder.setDefaults(NotificationCompat.DEFAULT_ALL);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
mBuilder.setPriority(NotificationManager.IMPORTANCE_HIGH);
} else {
mBuilder.setPriority(Notification.PRIORITY_HIGH);
}
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(999, mBuilder.build());
}
@RequiresApi(api = Build.VERSION_CODES.O)
private void createNotificationChannel() {
String name = getString(R.string.app_name);
String description = getString(R.string.notifcation_description);
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel channel = new NotificationChannel("channel_id"), name, importance);
channel.setDescription(description);
channel.enableVibration(true);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (notificationManager != null) {
notificationManager.createNotificationChannel(channel);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment