Skip to content

Instantly share code, notes, and snippets.

@nazmulidris
Created January 8, 2018 16:12
Show Gist options
  • Select an option

  • Save nazmulidris/4628e355109afe05b1c89d80360995a4 to your computer and use it in GitHub Desktop.

Select an option

Save nazmulidris/4628e355109afe05b1c89d80360995a4 to your computer and use it in GitHub Desktop.
@TargetApi(26)
public static class O {
public static final String CHANNEL_ID =
String.valueOf(getRandomNumber());
public static void createNotification(Service context) {
String channelId = createChannel(context);
Notification notification =
buildNotification(context, channelId);
context.startForeground(
ONGOING_NOTIFICATION_ID, notification);
}
private static Notification buildNotification(
Service context, String channelId) {
// Create Pending Intents.
PendingIntent piLaunchMainActivity =
getLaunchActivityPI(context);
PendingIntent piStopService =
getStopServicePI(context);
// Action to stop the service.
Notification.Action stopAction =
new Notification.Action.Builder(
STOP_ACTION_ICON,
getNotificationStopActionText(context),
piStopService)
.build();
// Create a notification.
return new Notification.Builder(context, channelId)
.setContentTitle(getNotificationTitle(context))
.setContentText(getNotificationContent(context))
.setSmallIcon(SMALL_ICON)
.setContentIntent(piLaunchMainActivity)
.setActions(stopAction)
.setStyle(new Notification.BigTextStyle())
.build();
}
@NonNull
private static String createChannel(Service ctx) {
// Create a channel.
NotificationManager notificationManager =
(NotificationManager)
ctx.getSystemService(Context.NOTIFICATION_SERVICE);
CharSequence channelName = "Playback channel";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel notificationChannel =
new NotificationChannel(
CHANNEL_ID, channelName, importance);
notificationManager.createNotificationChannel(
notificationChannel);
return CHANNEL_ID;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment