I've implemented a startForeground()
method to start the ongoing notification which is called in onCreate()
. There are two usages in two different classes. I'm testing on API 27 (Android 8.1)
First Usage (PictureCapturingService.java):
onCreate():
@Override
public void onCreate() {
super.onCreate();
Context mContext = this.getApplicationContext();
context = this.getApplicationContext();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startMyOwnForeground();
} else {
startForeground(1234, new Builder(getApplicationContext()).setContentTitle("Capturing Image").setContentText("").setSmallIcon(R.drawable.icon_notif).build());
}
startMyOwnForeground():
@RequiresApi(api = Build.VERSION_CODES.O)
private void startMyOwnForeground() {
String channelID = "com.example.code";
NotificationChannel notificationChannel = new NotificationChannel(channelID, "Background Service", NotificationManager.IMPORTANCE_NONE);
notificationChannel.enableLights(false);
notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
assert manager != null;
manager.createNotificationChannel(notificationChannel);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelID);
Notification notification = notificationBuilder.setOngoing(true)
.setSmallIcon(R.drawable.icon_notif)
.setContentTitle("Capturing Image")
.setPriority(NotificationManager.IMPORTANCE_MIN)
.setCategory(Notification.CATEGORY_SERVICE)
.build();
startForeground(1234, notification);
}
Second Usage (VideoRecordService.java):
Gets called from onCreate()
and startRecord()
onCreate():
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startMyOwnForeground("Sending Message");
}
else {
startForeground(1234, new Builder(this).setContentTitle("Sending Message").setContentText("").setSmallIcon(R.drawable.icon_notif).build());
}
startRecord():
private void startRecord() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startMyOwnForeground("Video Recording");
} else {
startForeground(1234, new Builder(getApplicationContext()).setContentTitle("Video Recording").setContentText("").setSmallIcon(R.drawable.icon_notif).build());
}
startMyOwnForeground:
@RequiresApi(api = Build.VERSION_CODES.O)
private void startMyOwnForeground(String str) {
String channelID = "com.example.code";
NotificationChannel notificationChannel = new NotificationChannel(channelID, "Background Service", NotificationManager.IMPORTANCE_NONE);
notificationChannel.enableLights(false);
notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
assert manager != null;
manager.createNotificationChannel(notificationChannel);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelID);
Notification notification = notificationBuilder.setOngoing(true)
.setSmallIcon(R.drawable.icon_notif)
.setContentTitle(str)
.setPriority(NotificationManager.IMPORTANCE_MIN)
.setCategory(Notification.CATEGORY_SERVICE)
.build();
startForeground(1234, notification);
}
I receive the following error:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.raudram.barathikannamma, PID: 18871
android.app.RemoteServiceException: Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification: Notification(channel=null pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x40 color=0x00000000 vis=PRIVATE)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1797)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:6651)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824)
Your icon is set to R.mipmap.ic_launcher
. By name, that suggests that you are trying to use your app launcher icon as your notification icon. That is not a valid notification icon, though.
Use the Image Asset Wizard in Android Studio to create a dedicated notification icon — you will find a dedicated "Notification Icons" category for the "Icon Type" in the wizard. Then, switch to using that resource, instead of R.mipmap.ic_launcher
, and see if you have better results.
Listen, the class in which you are creating a notification channel must inherit Application classenter image description here
User contributions licensed under CC BY-SA 3.0