startForeground() under Android 9

0

I developed an app for Android 7.1 some time ago. Trying to run the app on another Smartphone (Android 9) failed, because of using startForeground().

Intent notificationIntent = new Intent(getApplicationContext(), MyService.class);

            PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0,
                    notificationIntent, 0);

            Notification notification = new Notification.Builder(getApplicationContext())
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentTitle("title")
                    .setContentText("Foreground-Service running")
                    .setContentIntent(pendingIntent).build();

            startForeground(101, notification);

I got the following error:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.test2app, PID: 14754
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:1737)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6669)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

I added this permission:

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

How do I fix this?

android
foreground-service
asked on Stack Overflow Mar 2, 2020 by Ludwigm • edited Mar 2, 2020 by Ludwigm

2 Answers

0
    NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);    
    String str = "ch_id_i";
            if (VERSION.SDK_INT >= 26) {
                NotificationChannel notificationChannel = new NotificationChannel(str, "name", 
                NotificationManager.IMPORTANCE_LOW);
                notificationChannel.enableLights(true);
                notificationChannel.setLightColor(SupportMenu.CATEGORY_MASK);
                notificationChannel.enableVibration(false);
                notificationChannel.setShowBadge(false);
               notificationmanager.createNotificationChannel(notificationChannel);
            }

builder = new NotificationCompat.Builder(context, str);
answered on Stack Overflow Mar 2, 2020 by Vishal Bhut
0

so it´s working:

Intent notificationIntent = new Intent(getApplicationContext(), MyService.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0,
                    notificationIntent, 0);
            createNotificationChannel();
            NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx, CHANNEL_ID);
                builder.setSmallIcon(R.drawable.ic_launcher_background);
                builder.setContentTitle("title");
                builder.setContentText("text");
                builder.setPriority(NotificationCompat.PRIORITY_DEFAULT);
                builder.setContentIntent(pendingIntent);
            startForeground(1, builder.build());

    private void createNotificationChannel() {
    // Create the NotificationChannel, but only on API 26+ because
    // the NotificationChannel class is new and not in the support library
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence name = "Foregroundservice MyService.java";
        String description = "Benachrichtigung über das Aktivieren des Foreground-Service";
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
        channel.setDescription(description);
        // Register the channel with the system; you can't change the importance
        // or other notification behaviors after this
        NotificationManager notificationManager = getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
    }

good reference: https://developer.android.com/training/notify-user/build-notification#java

answered on Stack Overflow Mar 2, 2020 by Ludwigm

User contributions licensed under CC BY-SA 3.0