【问题标题】:The notification does not appear on the screen, only the icon in the status bar Oreo通知没有出现在屏幕上,只有状态栏中的图标奥利奥
【发布时间】:2019-03-25 09:40:44
【问题描述】:

你好,我写了一个类来创建通知,但是状态栏出现通知图标,通知块没有出现甚至锁屏的问题。在文档中,它被写入使用NotificationManagerCompat 来显示通知(Docs link)。这是我的代码:

public class NotificationsService {
    private Context mContext;
    private NotificationManager mNotificationManager;

    public NotificationsService(Context context) {
        mContext = context;
    }

    public void sendNotification(int iconResource) {
        Notification notification = buildNotification(iconResource);
//        mNotificationManager.notify(0 /* ID of notification */, notification);

        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(mContext);
        notificationManager.notify(70, notification);
    }

    public Notification buildNotification(int iconResource) {
        String CHANNEL_ID = "type"; // The id of the channel.
        CharSequence channelName = "dsfsf";

        NotificationChannel channel = null;

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            channel = new NotificationChannel(CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_DEFAULT);
        }

        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(mContext, CHANNEL_ID)
                .setSmallIcon(iconResource)
                .setContentTitle("title")
                .setContentText("message")
                .setAutoCancel(true)
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                .setSound(defaultSoundUri)
                .setChannelId(CHANNEL_ID);
//                .setContentIntent(pendingIntent);

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

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && mNotificationManager != null) {
            mNotificationManager.createNotificationChannel(channel);
        }

        return notificationBuilder.build();
    }

并像这样使用:

NotificationsService notif = new NotificationsService(context);
  notif.sendNotification(R.mipmap.ic_launcher);

仅显示应用图标,仅在状态栏中显示通知

【问题讨论】:

    标签: android notifications


    【解决方案1】:

    您需要将您的通知通知通知管理员。我开发了一种为 Oreo 或上层 SDK 生成通知的方法。

    在代码中使用正确的参数调用此方法...

    //This method is for Oreo
        @RequiresApi(api = Build.VERSION_CODES.O)
        public static void createSimpleNotificationForOreo(Context context, PendingIntent pendingIntent, String notificationTitle, String notificationContent, int notificationId, String notificationChannelId) {
            Notification.BigTextStyle bigText = new Notification.BigTextStyle();
            bigText.bigText(notificationContent);
            bigText.setBigContentTitle(notificationTitle);
            Notification.Builder notificationBuilder = new Notification.Builder(context)
                    .setSmallIcon(R.drawable.ic_small_notification)
                    .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher))
                    .setColor(context.getResources().getColor(R.color.colorPrimary))
                    .setContentText(notificationContent)
                    .setStyle(bigText)
                    .setChannelId(notificationChannelId)
                    .setContentIntent(pendingIntent);
    
            Notification notification = notificationBuilder.build();
            notification.flags |= Notification.FLAG_AUTO_CANCEL;
            notification.defaults |= Notification.DEFAULT_SOUND;
            notification.defaults |= Notification.DEFAULT_VIBRATE;
            NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            NotificationChannel mChannel = new NotificationChannel(notificationChannelId, NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
            if (notificationManager != null) {
                notificationManager.createNotificationChannel(mChannel);
                notificationManager.notify(notificationId, notification);
            }
        }
    

    【讨论】:

    • 不工作只在状态栏上显示我用 Android 8.0.0 测试的图标。您不使用文档developer.android.com/training/notify-user/…中所写的NotificationManagerCompat
    • 如果您的设备在 Android Oreo 上运行,则不必使用 NotificationManagerCompat。上面的方法是使用@RequiresApi(api = Build.VERSION_CODES.O)注解,所以只需要NotificationManager就够了。如果你想要一个向后兼容的 SDK,那么只有你应该使用NotificationManagerCompat。以上方法在生产环境中Tested OK,所以你的设备或代码肯定有其他问题。您可以尝试使用任何其他设备吗?
    • 我找到了这个答案,我认为它与stackoverflow.com/a/42754344/10512612 相关。但是在答案中没有官方消息来源,你确认他说了什么吗?我应该过度劳累将用户重定向到屏幕设置通知吗?
    • 如果设备有启用/禁用浮动通知的设置选项,这意味着设备赋予用户控制通知显示方式的权力。所以开发者不应该强制它启用该选项,因为它是不推荐的。
    猜你喜欢
    • 1970-01-01
    • 2018-06-19
    • 2018-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多