android studio custom notification code example

Example: android create notification

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(yourContext.getApplicationContext(), "notify_001");
Intent ii = new Intent(yourContext.getApplicationContext(), YourMainActivty.class);
PendingIntent pendingIntent = PendingIntent.getActivity(yourContext, 0, ii, 0);

NotificationCompat.BigTextStyle bigText = new NotificationCompat.BigTextStyle();
bigText.bigText(notificationsTextDetailMode); //detail mode is the "expanded" notification
bigText.setBigContentTitle(notificationTitleDetailMode);
bigText.setSummaryText(usuallyAppVersionOrNumberOfNotifications); //small text under notification

mBuilder.setContentIntent(pendingIntent);
mBuilder.setSmallIcon(R.mipmap.ic_launcher); //notification icon
mBuilder.setContentTitle(notificationTitle); //main title
mBuilder.setContentText(notificationText); //main text when you "haven't expanded" the notification yet
mBuilder.setPriority(Notification.PRIORITY_MAX);
mBuilder.setStyle(bigText);

NotificationManager mNotificationManager = (NotificationManager) yourContext.getSystemService(Context.NOTIFICATION_SERVICE);

NotificationChannel channel = new NotificationChannel("notify_001",
                                                      "Channel human readable title",
                                                      NotificationManager.IMPORTANCE_DEFAULT);
if (mNotificationManager != null) {
  mNotificationManager.createNotificationChannel(channel);
}

if (mNotificationManager != null) {
  mNotificationManager.notify(0, mBuilder.build());
}

Tags:

Java Example