Skip to content

Instantly share code, notes, and snippets.

@imandaliya
Created June 11, 2018 06:32
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 imandaliya/e0b227e9d3d95bdca4f0dcbbbfd2a0d9 to your computer and use it in GitHub Desktop.
Save imandaliya/e0b227e9d3d95bdca4f0dcbbbfd2a0d9 to your computer and use it in GitHub Desktop.
Send Notification for Android O and Lower
private static final String NOTIFICATION_CHANNEL_ID = "1001";
private static final int NOTIFICATION_ID = 1001;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_demo);
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationChannel notificationChannel =
new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_DEFAULT);
// Configure the notification channel.
notificationChannel.setDescription("My Notification Description.");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationManager.createNotificationChannel(notificationChannel);
// notification.defaults = Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE;
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
.setSmallIcon(R.drawable.ic_save)
.setContentTitle(getString(R.string.app_name))
.setContentText("My Notification Description.");
notificationManager.notify(NOTIFICATION_ID, builder.build());
} else {
final Intent intent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder b = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
b.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.ic_save)
.setTicker("Success")
.setContentTitle(getString(R.string.app_name))
.setContentText("My Notification Description")
.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND)
.setContentIntent(contentIntent)
.setContentInfo("Image");
NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, b.build());
}
} catch (Exception e) {
e.printStackTrace();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment