invalid channel for service notification: Notification

2

My code works exactly fine as long as I use the Android Emulator to test it, but once I build it and try to use it on my mobile phone I get the following error:

    An uncaught Exception occurred on "main" thread.
Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification: Notification(channel=channel_01 pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x40 color=0x00000000 vis=PRIVATE)

StackTrace:
android.app.RemoteServiceException: Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification: Notification(channel=channel_01 pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x40 color=0x00000000 vis=PRIVATE)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1760)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:201)
    at android.app.ActivityThread.main(ActivityThread.java:6806)
    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:873)

this is my code:

@JavaProxy('org.nativescript.nstestandroid.ForegroundService')
class ForegroundService extends android.app.Service {
    onStartCommand(intent, flags, startId) {
        console.log('onStartCommand');
        super.onStartCommand(intent, flags, startId);
        return android.app.Service.START_STICKY;
    }

    onCreate() {
        console.log('onCreate')
        super.onCreate();
        this.startForeground(1, this.getNotification());
    }



 private getNotification() {
        const channel = new android.app.NotificationChannel(
            'channel_01',
            'ForegroundService Channel', 
            android.app.NotificationManager.IMPORTANCE_DEFAULT
        );
       // <const notificationManager = this.getSystemService(android.content.Context.NOTIFICATION_SERVICE) as android.app.NotificationManager;
        //notificationManager.createNotificationChannel(channel);
        const builder = new android.app.Notification.Builder(this.getApplicationContext(), 'channel_01');

        return builder.build();
    }
android
angular
nativescript
asked on Stack Overflow Oct 23, 2019 by (unknown user)

2 Answers

3

you need to create NotificationChannel before posting into it new Notification. Smth like this:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    CharSequence name = getString(R.string.channel_name);
    String description = getString(R.string.channel_description);
    int importance = NotificationManager.IMPORTANCE_DEFAULT;
    NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
    channel.setDescription(description);

    // Don't see these lines in your code...
    NotificationManager notificationManager = getSystemService(NotificationManager.class);
    notificationManager.createNotificationChannel(channel);
}

you are only creating new channel (as object), but never call createNotificationChannel

you probably have created notification channel on emulator, but not on device. there is also a chance that some devices with earlier OS versions may auto-create "default" notification channel for compatibility purposes, but newer OS versions may require channel creation before showing notification

some guide in HERE

answered on Stack Overflow Oct 23, 2019 by snachmsm • edited Apr 27, 2021 by snachmsm
3
  1. Create Application Class Like this

    public class App extends Application {
    
    public static final String CHANNEL_ID = "exampleServiceChannel";
    
    @Override
    public void onCreate() {
        super.onCreate();
        createNotificationChannel();
    }
    
    private void createNotificationChannel() {
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            NotificationChannel serviceChannel = new NotificationChannel(CHANNEL_ID, "Example Service Channel",NotificationManager.IMPORTANCE_DEFAULT);
            NotificationManager notificationManager = getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(serviceChannel);
    
        }
    }
    }
    
  2. Create notification in your service class and call startForeground

Intent notificationIntent =  new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this,
                0, notificationIntent, 0);        
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
            .setContentTitle("Example Service")
            .setContentText(userInput)
            .setSmallIcon(R.drawable.ic_android)
            .setContentIntent(pendingIntent)
            .build();
    
    
        startForeground(1,notification);
  1. Very Important -> Done forget to mention your application class App in manifest file as -> android:name=".App"
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.androidservicestemplate">
    
        <uses-permission android:name="android.permission.FOREGROUND_SERVICE" /> 
    <application
            android:name=".App"
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
            <activity android:name=".MainActivity"></activity>
            <service android:name=".ExampleService" />
        </application>
    
    </manifest>

answered on Stack Overflow Aug 18, 2020 by Umer Mehmood

User contributions licensed under CC BY-SA 3.0