Skip to content

Instantly share code, notes, and snippets.

@MrCrambo
Last active July 11, 2020 07:53
Show Gist options
  • Select an option

  • Save MrCrambo/82bfb3f11b918882ab1425371edce6eb to your computer and use it in GitHub Desktop.

Select an option

Save MrCrambo/82bfb3f11b918882ab1425371edce6eb to your computer and use it in GitHub Desktop.
public class NotificationService extends Service {
private static final int ID_SERVICE = 101;
@Nullable @Override public IBinder onBind(Intent intent) {
return null;
}
@Override public void onCreate() {
super.onCreate();
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String channelId = createNotificationChannel(notificationManager);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId);
Notification notification = notificationBuilder.setOngoing(true)
.setContentTitle("Navigine Service Example")
.setCategory(NotificationCompat.CATEGORY_SERVICE)
.build();
startForeground(ID_SERVICE, notification);
}
@Override public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
return START_REDELIVER_INTENT;
}
private String createNotificationChannel(NotificationManager notificationManager){
String channelId = "NAVIGINE_SERVICE_CHANNEL_ID_EXAMPLE";
String channelName = "NAVIGINE CHANNEL NAME EXAMPLE";
NotificationChannel channel = new NotificationChannel(channelId,
channelName,
NotificationManager.IMPORTANCE_HIGH);
channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC); // will be shown in lock screen
notificationManager.createNotificationChannel(channel);
return channelId;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment