Skip to content

Instantly share code, notes, and snippets.

@aslamanver
Last active July 5, 2020 16:22
Show Gist options
  • Save aslamanver/bbf9f0fe60b43275e2226a877649f046 to your computer and use it in GitHub Desktop.
Save aslamanver/bbf9f0fe60b43275e2226a877649f046 to your computer and use it in GitHub Desktop.
Android - Foreground Service
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Binder;
import android.os.Build;
import android.os.IBinder;
import androidx.core.app.NotificationCompat;
public class ForegroundService extends Service {
private final int NOTIFICATION_ID = 2999;
private final String CHANNEL_ID = "DPService_ID";
private final CharSequence CHANNEL_NAME = "Service Channel";
private final IBinder mBinder = new LocalBinder();
public class LocalBinder extends Binder {
public ForegroundService getService() {
return ForegroundService.this;
}
}
@Override
public void onCreate() {
super.onCreate();
startForeground(NOTIFICATION_ID, createNotification("Service is running"));
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return Service.START_STICKY;
}
private Notification createNotification(String message) {
// RemoteViews notificationLayout = new RemoteViews(getPackageName(), R.layout.notification_main);
// notificationLayout.setTextViewText(R.id.txtTitle, message);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, CHANNEL_ID);
Intent notificationIntent = new Intent(this, Launcher.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 125, notificationIntent, 0);
Bitmap logo = BitmapFactory.decodeResource(getResources(), R.drawable.ic_notification);
mBuilder.setContentTitle(getString(R.string.app_name))
.setContentText(message)
.setPriority(Notification.PRIORITY_HIGH)
.setLargeIcon(logo)
.setSmallIcon(R.drawable.ic_notification)
.setContentIntent(pendingIntent)
.setAutoCancel(false);
// .setCustomBigContentView(notificationLayout);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String channelId = CHANNEL_ID;
NotificationChannel channel = new NotificationChannel(channelId, CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
mNotificationManager.createNotificationChannel(channel);
mBuilder.setChannelId(channelId);
}
return mBuilder.build();
}
private void showNotification(String message) {
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(NOTIFICATION_ID, createNotification(message));
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment