Notification Won't Appear on Android TV

2019-08-10 12:20发布

问题:

I am playing with notifications on Android TV. I have not been able to get a notification to appear on the screen though. I am using the Nexus player which is on Android 6.0.

When I run this code on my phone, the notification appears. But on TV, the notification does not appear. Am I missing something?

@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);
}

EDIT I think my code was working all along. I had the above code in my onCreate(). So, I was expecting to see some sort of notification pop up on screen when the app launched. However, I see that when I press the "home" button that there is a Recommendation in that home Carousel. When I click it, it correctly follows my pending intent. This won't happen if I remove the .setCategory(Notification.CATEGORY_RECOMMENDATION) line. So that is the key for Android TV "notifications"

回答1:

A TV notification is going to be different in some ways from a phone notification and may have specific additional parameters. Try adding a few more attributes. Here's a snippet of a TV notification I have implemented in one of my apps which works.

Notification notification = new NotificationCompat.BigPictureStyle(
            new NotificationCompat.Builder(mContext)
                    .setContentTitle(video.getString("title"))
                    .setContentText(mDescription)
                    .setPriority(mPriority)
                    .setLocalOnly(true)
                    .setOngoing(true)
                    .setColor(mContext.getResources().getColor(android.R.color.holo_green_dark))
                    .setCategory(Notification.CATEGORY_RECOMMENDATION)
                    .setLargeIcon(thumbnail)
                    .setSmallIcon(R.drawable.ic_note)
                    .setContentIntent(launchApp(mContext))
                    .setExtras(null))
            .build();


    return notification;