45 lines
1.9 KiB
Java
45 lines
1.9 KiB
Java
package com.denco.assemblrprintingservice;
|
|
|
|
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 androidx.core.app.NotificationCompat;
|
|
import com.denco.assemblrprintingservice.MainActivity;
|
|
import com.denco.assemblrprintingservice.R;
|
|
|
|
public class NotificationUtils {
|
|
private static final int NOTIFICATION_ID = 1;
|
|
private static final String NOTIFICATION_CHANNEL_ID = "Channel_Id";
|
|
|
|
public static void createNotificationChannel(Service service) {
|
|
CharSequence name = service.getString(R.string.channel_name);
|
|
String description = service.getString(R.string.channel_description);
|
|
int importance = NotificationManager.IMPORTANCE_DEFAULT;
|
|
NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, name, importance);
|
|
channel.setDescription(description);
|
|
NotificationManager notificationManager = service.getSystemService(NotificationManager.class);
|
|
notificationManager.createNotificationChannel(channel);
|
|
}
|
|
|
|
public static void startForeground(Service service) {
|
|
Intent notificationIntent = new Intent(service, MainActivity.class);
|
|
|
|
PendingIntent pendingIntent = PendingIntent.getActivity(service, 0,
|
|
notificationIntent, PendingIntent.FLAG_IMMUTABLE);
|
|
|
|
android.app.Notification notification = new NotificationCompat.Builder(service,
|
|
NOTIFICATION_CHANNEL_ID)
|
|
.setOngoing(true)
|
|
.setSmallIcon(R.drawable.ic_notifications_black_24dp)
|
|
.setContentTitle(service.getString(R.string.app_name))
|
|
.setContentText("Service is running background")
|
|
.setContentIntent(pendingIntent)
|
|
.build();
|
|
|
|
service.startForeground(NOTIFICATION_ID, notification);
|
|
}
|
|
}
|