Last active
October 22, 2019 10:28
-
-
Save gsandaru/20593d9eedc8ddb0e1894f3870651bfd to your computer and use it in GitHub Desktop.
Notification Snippets
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
private static final String CHANNEL_ID = "com.apps.testnotifications" ; | |
private void createNotificationChannel() { | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | |
CharSequence name = getString(R.string.app_name); | |
String description = getString(R.string.app_name); | |
int importance = NotificationManager.IMPORTANCE_DEFAULT; | |
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance); | |
channel.setDescription(description); | |
NotificationManager notificationManager = getSystemService(NotificationManager.class); | |
notificationManager.createNotificationChannel(channel); | |
} | |
} | |
// 1. Simple Notification | |
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(getApplicationContext()); | |
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(), CHANNEL_ID) | |
.setSmallIcon(R.drawable.ic_audiotrack_black_24dp) // icon | |
.setContentTitle("Simple Notification") // title | |
.setContentText("Hello World") // text in the notification body | |
.setPriority(NotificationCompat.PRIORITY_DEFAULT); // notification priority | |
notificationManager.notify(1, builder.build()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment