Create Notification in Android

27-01-2019

Create a notification on Android:

String name="";
String message="";
int MID=12345;
NotificationManager notificationManager = (NotificationManager) getActivity()
.getSystemService(Context.NOTIFICATION_SERVICE);
if(notificationManager==null)return;
String NOTIFICATION_CHANNEL_ID = "step_counter_channel";

NotificationChannel mChannel;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
    int importance = NotificationManager.IMPORTANCE_LOW;
    mChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID,
    "step_counter_channel",importance);
    mChannel.setDescription(message);
    mChannel.enableLights(true);
    mChannel.setLightColor(Color.RED);
    notificationManager.createNotificationChannel(mChannel);
}
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getActivity(), 
NOTIFICATION_CHANNEL_ID);

mBuilder.setContentTitle(name) 
.setContentText(message)
.setSmallIcon(R.drawable.icon)
.setWhen(System.currentTimeMillis())
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
.setAutoCancel(true);
Intent mesajListIntent = new Intent(getContext(), getActivity().getClass());

PendingIntent contentIntent = PendingIntent.getActivity(getContext(), 0, mesajListIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(contentIntent);
notificationManager.notify(MID, mBuilder.build());

© 2019 All rights reserved. Codesenior.COM