Android Wear通知未显示 [英] Android Wear notification not showing

查看:144
本文介绍了Android Wear通知未显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在编写一个Android Wear应用,该应用允许用户从手表中控制音乐播放器.我正在尝试通过带有两个操作按钮的通知来实现.以下是在当前播放的歌曲更改时创建/更新通知的代码,它是从OnDataChanged()WearableListenerService的.数据按预期到达模拟器,并且通知构建器的构造正确,正如我在调试器中看到的那样.最后的日志行也被执行,我可以在logcat中看到它,但是通知不是在模拟器上创建的.我手机上其他应用程序发出的所有其他通知也会显示在模拟器上!

Hi I'm writing an Android Wear app that allows the user to control a music player from their watch. I'm trying to do it with a notification with two action buttons. Below is the code that creates/updates the notification when the currently playing song changes, it is from OnDataChanged() from the WearableListenerService. The data arrives on the emulator as expected and the notification builder is constructed correctly, as I can see it in the debugger. Also the final log line is executed and I can see it in logcat, however the notification is not created on the emulator. Also all other notifications from other apps on my phone show up on emulator!

非常感谢任何帮助,谢谢詹姆斯!

Any help greatly appreciated, thanks James!

for (DataEvent event : events) {
        if (event.getType() == DataEvent.TYPE_CHANGED) {
            String path = event.getDataItem().getUri().getPath();
            if ("/playmusicremotedata".equals(path)) {
                // Get the data out of the event
                DataMapItem dataMapItem =
                        DataMapItem.fromDataItem(event.getDataItem());
                final String songTitle = dataMapItem.getDataMap().getString("songTitle");
                final String artist = dataMapItem.getDataMap().getString("artist");
                final String album = dataMapItem.getDataMap().getString("album");
                Asset asset = dataMapItem.getDataMap().getAsset("albumArt");

                Bitmap albumArt = loadBitmapFromAsset(asset);

                PendingIntent skipForwardPendInt = PendingIntent.getBroadcast(getApplicationContext(), 8, new Intent("net.jamyspex.remoteforgoogleplaymusic.SKIP_FORWARD"), PendingIntent.FLAG_UPDATE_CURRENT);
                PendingIntent playPausePendInt = PendingIntent.getBroadcast(getApplicationContext(), 7, new Intent("net.jamyspex.remoteforgoogleplaymusic.PLAY_PAUSE"), PendingIntent.FLAG_UPDATE_CURRENT);

                Notification.Action playPauseBut = new Notification.Action(R.drawable.play, "Pause/Play", playPausePendInt);
                Notification.Action nextBut = new Notification.Action(R.drawable.skip_forward, "Skip", skipForwardPendInt);

                // Create the ongoing notification
                Notification.Builder notificationBuilder =
                        new Notification.Builder(this)
                                .setSmallIcon(R.drawable.ic_launcher)
                                .setContentTitle(songTitle)
                                .setContentText(artist + " - " + album)
                                .setLargeIcon(albumArt)
                                .setOngoing(true)
                                .addAction(playPauseBut)
                                .addAction(nextBut);

                // Build the notification and show it
                NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
                Log.i(TAG, "Should have created notification");
            } else {
                Log.d(TAG, "Unrecognized path: " + path);
            }
        }
    }

推荐答案

为了使该解决方案对可能受同一问题影响的用户更可见-我将其发布为答案.

To make the solution more visible to users who may be affected with the same issue - i'm posting this as an answer.

原因很简单:Android Wear设备上的应用已被静音(可能是偶然的),这阻止了手表上出现任何通知.

The reason was simple: app was muted (probably accidentally) on Android Wear device and this was preventing any notifications from appearing on watch.

在发布来自手表的任何正在进行的通知时,将自动添加额外的操作.它被称为"静音应用",可能是在开发过程中的某个时间点偶然被按下的.

While posting any ongoing notification from watch, there will be extra action added automatically. It is called "Mute app" and probably it was pressed in some point in time during development by accident.

静音的应用程序可以在设置"屏幕上的Android Wear配套应用程序中进行管理.这可能很难调试,因为调用了任何代码而没有任何错误,并且LogCat中也没有关于应用程序被静音的日志.

Muted apps can be managed in the Android Wear companion app on Settings screen. This can be fairly hard to debug because any code is invoked without any error and there is also no logs in the LogCat about the fact that app is muted.

我希望这对以后将搜索类似问题的其他用户有所帮助:)

I hope that it will be helpful for other users that will search for the similar issue in the future:)

这篇关于Android Wear通知未显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆