Last active
July 3, 2018 09:42
-
-
Save AhmadAyyaz1993/046357ae218669cd5838569972dfd56b to your computer and use it in GitHub Desktop.
Method for generating local notifications including API level 26
This file contains hidden or 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 int NOTIFICATION_ID = 1; | |
private static final String NOTIFICATION_CHANNEL_ID = "joblogic_notification_channel"; | |
... | |
public void showNotification( String contentTitle, String contentText ) { | |
int icon = R.drawable.icon; | |
long when = System.currentTimeMillis(); | |
Intent notificationIntent = new Intent(this,LocalNotificationActivity.class); | |
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,notificationIntent, 0); | |
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | |
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "JobLogic Notifications", NotificationManager.IMPORTANCE_HIGH); | |
// Configure the notification channel. | |
notificationChannel.setDescription("JobLogic"); | |
notificationChannel.enableLights(true); | |
notificationChannel.setLightColor(Color.RED); | |
notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000}); | |
notificationChannel.enableVibration(true); | |
notificationManager.createNotificationChannel(notificationChannel); | |
Notification n= new Notification.Builder(this,NOTIFICATION_CHANNEL_ID) | |
.setContentTitle(contentTitle) | |
.setContentText(contentText) | |
.setSmallIcon(icon) | |
.setAutoCancel(true) | |
.setContentIntent(pendingIntent) | |
.build(); | |
notificationManager.notify(NOTIFICATION_ID, n); | |
} | |
NotificationCompat.Builder builder = new NotificationCompat.Builder(this) | |
.setVibrate(new long[]{0, 100, 100, 100, 100, 100}) | |
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)) | |
.setSmallIcon(icon) | |
.setContentIntent(pendingIntent) | |
.setContentTitle(contentTitle) | |
.setContentText(contentText); | |
notificationManager.notify(NOTIFICATION_ID, builder.build()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment