Created
October 10, 2018 15:11
-
-
Save michaeltys/3aed7ede1066e235602ad202f29ee62f to your computer and use it in GitHub Desktop.
Showing high importance notifications on different android OS versions
This file contains hidden or 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
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