NOTIFICATION_SERVICE with Channel=null error, how to resolve?

Asked

Viewed 149 times

2

I did work perfectly this notification on android 19 and 21 but does not work on 27, 28 e 29.

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

 private void openNotification(){

        Intent intent = new Intent( MainActivity.this, NotificationActivity.class );
        intent.putExtra( "msg", msg );
        intent.putExtra( "status", status );
        intent.putExtra( "time", DataTime );
        // int id = 1;
        int id = (int) (Math.random() * 1000);
        PendingIntent pi = PendingIntent.getActivity( getBaseContext(), id, intent, PendingIntent.FLAG_UPDATE_CURRENT );
        Notification notification = new Notification.Builder( getBaseContext() ).setContentTitle( "Evento de Pânico" ).setContentText( msg ).setSmallIcon( R.mipmap.panic ).setContentText( DataTime ).setContentIntent( pi ).build();
        NotificationManager notificationManager = (NotificationManager) getSystemService( NOTIFICATION_SERVICE );
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notificationManager.notify( id, notification );

}

In API 29 comes a warning Toast:

            Notification(channel=null pri=0 contentView=null vibrate=null sound=null 
    defaults=0x0 flags=0x0 color=0x00000000 vis=PRIVATE)  

when running in api19 comes the following:

Notification(pri=0 icon=7f0e0005 contentView=br.com.panico/0x109007d vibrate=null sound=null defaults=0x0 flags=0x0 when=1567779581326 ledARGB=0x0 contentIntent=Y deleteIntent=N contentTitle=16 contentText=19 tickerText=N kind=[null])

1 answer

1


This should be happening because from Android 8.0(API 26), you need to inform a notification channel, as is said on the android site:

Notification channels

Starting with Android 8.0 (Level 26 API), all notifications need to be assigned to a channel or won’t be displayed. By categorizing notifications into channels, users can disable specific notification channels for their app (instead of disabling all notifications) and can control the visual and sound options of each channel, all this from the Android system settings (Figure 11). Users can also tap a notification and keep it pressed to change the behaviors of the associated channel.

On devices with Android 7.1 (Level 25 API) and earlier versions, users can only manage notifications for individual apps (in fact, each app has only one channel on Android 7.1 and earlier versions).

Figura 11. Configurações de notificação para o app Relógio e um dos canais dele
Figure 11. Notification settings for the Clock app and one of its channels

For this you will need to update your code to use the NotificationCompat instead of Notification, besides having to create your channel:

Dependencies:

dependencies {
    implementation "com.android.support:support-compat:28.0.0"
}

28.0.0 is the lib version support-compat, may be that your project uses a different version of this, because if I’m not mistaken it depends on the version of build_sdk or target_sdk of your project (I can’t remember which of the two, but your Android Studio should show an alert).

Creating notification channel:

private void createNotificationChannel() {

    // Cria o canal de notificação para a API 26+
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        int importancia = NotificationManager.IMPORTANCE_DEFAULT;
        NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "nomeDoCanal", importancia);
//                                                            ^ ID DO CANAL
        channel.setDescription("Descrição do canal");

        // Registra o canal no sistema, você não pode mudar a importância ou outros comportamentos depois disso
        NotificationManager notificationManager = getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
    }
}

In the project where I had to use something similar to create the Notification Channel, I was creating/registering the channel within the onCreate.

Creating the notification using the NotificationCompat:

/// Criar uma Intent para abrir uma Activity
Intent intent = new Intent(this, AlertDetails.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
//                                                                        ^ ID DO CANAL EM QUE A NOTIFICACAO SERA DISPARADA

        .setSmallIcon(R.drawable.notification_icon)
        .setContentTitle("Meu titulo")
        .setContentText("Meu texto informativo")
        .setPriority(NotificationCompat.PRIORITY_DEFAULT)

        .setContentIntent(pendingIntent) /// Define a Intent que sera aberta ao clicar na notificação

        .setAutoCancel(true);

Reference: Create a notification - Developer.android.com

  • It worked! Just one thing, the icons that should appear in the status bar appears a dark ball. And in api 19 for example appears the same icone, which is the icone of the company that will use the app. I already changed the icon size, I changed the icone. I’ve changed a lot, but it doesn’t render the right icon.

  • 1

    For this icon problem this other answer may help - Notification icon in Android 5.0 , you can also try to create the icon using the Image Asset Studio android Studio - Create a notification icon with Image Asset Studio

  • If you can help me with this other question that’s going on : "Location loops only above API 23 version Android 6.0(Marshmallow), why?" https://answall.com/questions/406805/location-entra-em-looping-somente-acima-da-vers%C3%a3o-api-23-android-6-0marshmallow

Browser other questions tagged

You are not signed in. Login or sign up in order to post.