穿戴式操作系统:通知不会显示

时间:2018-10-17 10:10:09

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

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

我正在运行Foreground Service,并有持续的通知。该正在进行的通知非常有效,并且没有Wear OS的功能(因此该代码可以在独立的Android上运行)。

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

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

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

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

我在这里想念东西吗?

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

使用此代码

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