简体   繁体   中英

Android 8 (Oreo) notification not showing up

Intent intent = new Intent(this, SplashActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("splash", psd);
                bundle.putString("targetId", targetId);
                intent.putExtras(bundle);
                intent.setAction(psd);
                intent.setAction(targetId);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), id, intent,
                        0);


                Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
                NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                        .setSmallIcon(android.R.drawable.ic_notification_overlay)
                        .setContentTitle(title)
                        .setContentText(remoteMessage.getData().get("body"))
                        .setAutoCancel(true)
                        .setStyle(new NotificationCompat.BigTextStyle().bigText("" + remoteMessage.getData().get("body")))
                        .setContentIntent(pendingIntent)
                        .setSound(defaultSoundUri);

                NotificationCompat.InboxStyle inboxStyle =
                        new NotificationCompat.InboxStyle();
                String[] events = new String[6];
                inboxStyle.setBigContentTitle("" + title);
                for (int i = 0; i < events.length; i++) {
                    inboxStyle.addLine(events[i]);
                }

                //.setContentIntent(pendingIntent);
                NotificationManager notificationManager =
                        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                notificationManager.notify(id, notificationBuilder.build());

It used to be working, no clue what's happening on new Android devices like Android O. I didn't try back going to old devices, but it's happening on pixel.

Starting in Android 8.0 (API level 26), all notifications must be assigned to a channel. For each channel, you can set the visual and auditory behavior that is applied to all notifications in that channel. Then, users can change these settings and decide which notification channels from your app should be intrusive or visible at all

Notification channels

I just figured this one out myself, you need to setup Channels in Oreo, see my previous post on the issue - I though it was a master-detail issue!!! Turns out Oreo has an additional attribute you need but it seems to fail silently if you don't provide it.

Set up notification channels. Example code below

   public static void createNotificationChannel(final Context context, final 
          String channelId, final CharSequence channelName, final String channelDescription, final int importance, final boolean showBadge) {

    try {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            postAsyncSafely("createNotificationChannel", new Runnable() {
                @Override
                public void run() {

                    NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
                    NotificationChannel notificationChannel = new NotificationChannel(channelId, channelName, importance);
                    notificationChannel.setDescription(channelDescription);
                    notificationChannel.setShowBadge(showBadge);
                    notificationManager.createNotificationChannel(notificationChannel);
                    Logger.i("Notification channel " + channelName.toString() + 
                   " has been created");

                }
            });
        }
    }catch (Throwable t){
        Logger.v("Failure creating Notification Channel",t);
    }

}

try this code it works perfectly for me.

        Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_CANCEL_CURRENT);

        NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();

        inboxStyle.addLine(message);

        NotificationManager mNotificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext(),"channel_01")
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentText(message)
                    .setContentIntent(resultPendingIntent)
                    .setAutoCancel(true)
                    .setPriority(Notification.PRIORITY_HIGH)
                    .setChannelId("channel_01")
                    .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                    .setStyle(inboxStyle)
                    .setContentTitle(Title);

            mNotificationManager.notify(Notification_ID, mBuilder.build());

            NotificationChannel channel = new NotificationChannel(Notification_ID, "Playback Notification", NotificationManager.IMPORTANCE_HIGH);
            channel.enableLights(true);
            channel.enableVibration(true);
            channel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
            assert mNotificationManager != null;
            mBuilder.setChannelId("channel_01");
            mNotificationManager.createNotificationChannel(channel);

        }else {
            NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext(),Notification_ID)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentTitle(Title)
                    .setContentIntent(resultPendingIntent)
                    .setContentText(message)
                    .setStyle(inboxStyle)
                    .setSound(soundUri)
                    .setAutoCancel(true);
            mNotificationManager.notify(Notification_ID, mBuilder.build());
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM