Created
June 11, 2018 06:32
-
-
Save imandaliya/e0b227e9d3d95bdca4f0dcbbbfd2a0d9 to your computer and use it in GitHub Desktop.
Send Notification for Android O and Lower
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 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