Android 8(奥利奥)通知未显示

时间:2018-06-13 08:01:40

标签: android notifications

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

它曾经有效,不知道Android O等新Android设备上发生了什么。我没有尝试回旧设备,但它发生在像素上。

4 个答案:

答案 0 :(得分:2)

  

从Android 8.0(API级别26)开始,所有通知都必须是   分配到一个频道。对于每个频道,您可以设置视觉和   应用于该通知中的所有通知的听觉行为   渠道。然后,用户可以更改这些设置并确定哪个   来自您应用的通知渠道应该是侵入性的或可见的   所有

Notification channels

答案 1 :(得分:1)

我自己想出了这个,你需要在奥利奥设置频道,在问题上看我的previous post - 我虽然这是一个主要细节问题!事实证明奥利奥有一个你需要的额外属性,但如果你不提供它,它似乎会无声地失败。

答案 2 :(得分:1)

设置通知渠道。下面的示例代码

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

}

答案 3 :(得分:1)

尝试此代码对我来说非常合适。

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