简体   繁体   中英

Android TV - Recommendation (Notification) will not auto-cancel

When creating a recommendation (or Notification) in Lollipop on Android TV, I cannot get it to Auto-cancel.

I am using the "NotificationCompat.BigPictureStyle" as recommended in the Android TV developer pages. The notification works as designed, triggering the PendingIntent as expected but does not auto-cancel and dissappear from recommendations bar. A second selection of the recommendation brings up a blank screen, so I guess the PendingIntent is null at that point. (ADB shows android.content.IntentSender$SendIntentException on 2nd invocation.)

Tested on Nexus Player and Android TV Emulator.

private void buildAndroidTVRecommendation(String name, PendingIntent pIntent,
        Context context2, Bundle extras) {

     NotificationManager mNotificationManager = (NotificationManager)
             context2.getSystemService(Context.NOTIFICATION_SERVICE);
     Bitmap smallBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.air_share);

    Notification notification = new NotificationCompat.BigPictureStyle(
            ( new NotificationCompat.Builder(context)
                    .setContentTitle("Air-Share - incoming share")
                    .setContentText("From: "+name)
                    .setContentInfo("Air-Share"))
                    .setGroup("Air-Share")
                    .setColor(0xFFFF2020)
                    .setCategory(Notification.CATEGORY_RECOMMENDATION)
                    .setLargeIcon(smallBitmap)
                    .setSmallIcon(R.drawable.air_share)
                    .setContentIntent(pIntent)
                    .setExtras(extras)
                    .setAutoCancel(true)

                    )
            .build();

    mNotificationManager.notify(pendingCounter, notification);
    mNotificationManager = null;


}

For what it's worth I have created a work-around to get by this issue.

I created a new Activity whose sole purpose is to receive a Pending intent from the Recommendation. I alter the original Pending Intent of the recommendation to invoke this new activity instead of my desired activity. (The desired activity is outside my app.) In the Extras, I bundle everything I need to know for my original desired intent as well as the notification ID. When the new activity is launched (after user clicks on recommendation), I extract the ID and cancel the recommendation. I then extract the information for the desired intent, create the intent and finally finish the activity.

public class TVRecommendationActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent in = (Intent) getIntent();
        String jsonString = in.getStringExtra("jsonString");
        String fileUri = in.getStringExtra("fileUri");

        int id  =in.getIntExtra("notificationID", -1);

        NotificationManager nm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        if (id >= 0 ) nm.cancel(id);

        JSONIntent newIntent = new JSONIntent(getApplicationContext());
        newIntent.setIncomingLocalFileURI(fileUri);
        Intent out = newIntent.buildIntentFromJSON(jsonString, fileUri);

        if (out != null) startActivity(out);
        finish();
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM