Configuration of the Notificationcompat

Asked

Viewed 104 times

0

I have a simple app to send Notifications, vibrate, and tap on receipt. On my android 4.3 it works, on another my mobile with android 9 it just touch, does not show notification and neither vibrates.

Notification method is on only one button:

public void gerarNotificacao(View view) {
        NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        PendingIntent p = PendingIntent.getActivity(this, 0, new Intent(this, atividade2.class), 0);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        builder.setTicker("Ticket Texto");
        builder.setContentTitle("Título");
        //builder.setContentText("Descrição");
        builder.setSmallIcon(R.drawable.ic_launcher_background);
        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher_background));
        builder.setContentIntent(p);

        NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle();
        String[] descs = new String[]{"Descrição 1", "Descrição 2", "Descrição 3", "Descrição 4"};
        for (int i = 0; i < descs.length; i++) {
            style.addLine(descs[i]);
        }
        builder.setStyle(style);

        // Vibração
        Notification n = builder.build();
        n.vibrate = new long[]{150, 300, 150, 600};
        n.flags = Notification.FLAG_AUTO_CANCEL;
        nm.notify(R.drawable.ic_launcher_background, n);

        // Toque
        try {
            Uri som = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            Ringtone toque = RingtoneManager.getRingtone(this, som);
            toque.play();
        } catch (Exception e) {
        }
    }

In the androidManifest put this permission:

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

I think it’s a simple solution, thank you.

1 answer

1


as says the documentation of android.

Starting with Android 8.0 (API level 26), all notifications must be assigned to a channel. For each channel, you can set the visual and listening behavior applied to all notifications in that channel. Then users can change these settings and decide which notification channels of their application should be intrusive or visible.

You will then need to implement the channels in your receiving class and create the notifications. How to create channels for new versions of android

Browser other questions tagged

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