Android 8 (Oreo) notification not showing up

别等时光非礼了梦想. 提交于 2020-07-22 07:04:08

问题


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.


回答1:


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




回答2:


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());
        }



回答3:


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.




回答4:


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);
    }

}


来源:https://stackoverflow.com/questions/50832023/android-8-oreo-notification-not-showing-up

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!