Skip to content

Instantly share code, notes, and snippets.

@gsandaru
Last active October 22, 2019 10:28
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 gsandaru/20593d9eedc8ddb0e1894f3870651bfd to your computer and use it in GitHub Desktop.
Save gsandaru/20593d9eedc8ddb0e1894f3870651bfd to your computer and use it in GitHub Desktop.
Notification Snippets
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