Bad notification for startForeground for Android 9

1

The foreground service notification works for android 8 but does not work on Android 9. Application crashed after I grant all the permissions with an error message

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=0x42 color=0x00000000 vis=PRIVATE semFlags=0x0 semPriority=0 semMissedCount=0)

My manifest has permission for foreground service

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

Code

@Override
public void onCreate() {
    super.onCreate();
    mScheduler = (ScheduledThreadPoolExecutor) Executors.newScheduledThreadPool(1);

    startForeground(NOTIFICATION_ID, getNotification(R.string.notification_title, R.string.sensor_disconnected, R.drawable.ic_stat_disconnected));

    log(TAG, "Create Service");
}

public Notification getNotification(int title, int text, int icon) {
    NotificationCompat.Builder foregroundNotification = new NotificationCompat.Builder(this);
    foregroundNotification.setOngoing(true);

    foregroundNotification.setContentTitle(getText(title))
            .setContentText(getText(text))
            .setSmallIcon(icon);

    return foregroundNotification.build();
}

private void updateNotification(int title, int text, int icon) {
    Notification notification = getNotification(title, text, icon);

    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(NOTIFICATION_ID, notification);
}

private void removeNotification() {
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.cancel(NOTIFICATION_ID);
}

private void setConnectionState(Boolean connected) {
    if (connected) {
        updateNotification(R.string.notification_title, R.string.sensor_connected, R.drawable.ic_stat_connected);
        Log.d("S: CONNECTION_STATUS", "sensor_connected");

        DateTime timestamp= new DateTime();

        HashMap<String, String> connectionStatus = new HashMap<>();
        connectionStatus.put(MOVISENS_CONNECTION_STATUS,"sensor_connected");
android
androidx
foreground-service
foregroundnotification
asked on Stack Overflow Jul 11, 2019 by Gauranga • edited Aug 6, 2019 by Azzabi Haythem

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0