【问题标题】:Wear OS: notifications won't showWear OS:通知不会显示
【发布时间】:2018-10-17 10:10:08
【问题描述】:

我正在 Wear OS(Android 8+)上开发一个独立的应用程序,但我遇到了通知问题。

我正在运行Foreground Service,并持续收到通知。该持续通知运行良好,并且没有来自 Wear OS 的功能(因此代码可以在独立的 Android 上运行)。

但是,每当我想显示其他通知时,这是不可能的。

没有错误消息,什么都没有:我的通知没有显示。 我确保创建单独的频道并启用它们(通过设置)。

这是我的代码,通过HandlerLooper.mainLooper() 中运行。

final Notification notification = new NotificationCompat.Builder(MonitorService.this, LOGS_CHANNEL_ID)
                        .setSmallIcon(R.drawable.ic_backup_logs) // vector (doesn't work with png as well)
                        .setContentTitle(getString(R.string.monitor_service_notification_log_file_backed_up_process))
                        .setContentText("test")
                        .setPriority(NotificationCompat.PRIORITY_HIGH)
                        .build();

notificationManagerCompat.notify(LOGS_ID, notification); // unique final static id

我在这里错过了什么吗?

感谢您的帮助!

【问题讨论】:

    标签: android android-notifications wear-os android-wear-notification


    【解决方案1】:

    使用此代码

    Notification.Builder b = new Notification.Builder(ctx);
    
        //FIX android O bug Notification add setChannelId("shipnow-message")
        NotificationChannel mChannel = null;
        b.setSmallIcon(R.drawable.ic_backup_logs) // vector (doesn't work with png as well)
                        .setContentTitle(getString(R.string.monitor_service_notification_log_file_backed_up_process))
                        .setContentText("test")
                        .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
    
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            mChannel = new NotificationChannel("your-channel", "yourSubjectName",NotificationManager.IMPORTANCE_HIGH);
            b.setChannelId("your-channel");
        }
    
        NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            notificationManager.createNotificationChannel(mChannel);
        }
        notificationManager.notify(id, b.build());
    

    【讨论】:

    • 谢谢。我注意到我错误地创建了我的频道,您的信息是一个很好的见解。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多