我正在尝试在Android TV上使用通知功能。但我无法使通知显示在屏幕上。我使用的是搭载Android 6.0的Nexus player。
当我在手机上运行此代码时,通知会显示出来。但在电视上,通知不会显示。我是否漏掉了什么?
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
showNotification();
}
private void showNotification() {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("My notification")
.setContentText("Hello World!")
.setCategory(Notification.CATEGORY_RECOMMENDATION)
.setPriority(Notification.PRIORITY_HIGH) // heads up must be high priority
.setAutoCancel(true)
.setVibrate(new long[0]); // needed to guarantee heads up (need vibrate or ringtone)
Notification notification = new NotificationCompat.BigPictureStyle(mBuilder).build();
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
int mId = 0;
mNotificationManager.notify(mId, notification);
}
编辑
我认为我的代码一直在工作。我将上述代码放在我的onCreate()
中。因此,当我启动应用程序时,我希望能看到某种形式的通知弹出屏幕。然而,我发现当我按下“主页”按钮时,在主页旋转木马中会出现一个推荐。当我点击它时,它正确地跟随我的待处理意图。如果我删除.setCategory(Notification.CATEGORY_RECOMMENDATION)
行,这将不会发生。所以这是Android TV“通知”的关键。
onCreate()
里面。所以,当应用程序启动时,我期望看到屏幕上弹出某种通知。然而,我发现当我按下“主页”按钮时,在主页轮播中有一个推荐。当我点击它时,它正确地跟随了我的待处理意图。 - boltup_im_coding