Search code examples
androidleanback

Android leanback notification intent only taking the data of last movie


While clicking on any notification which my app sends to my Android TV, I am only getting the data from the last notification added and not the notification I clicked on.

My code:

for (Asset asset : assets) {
    ContentRecommendation.Builder builder = new ContentRecommendation.Builder()
            .setBadgeIcon(R.drawable.icon);
    int id = asset.getId().hashCode();

    Intent detailsIntent = new Intent(mContext, LoginActivity.class);
    Bundle bundle = new Bundle();
    bundle.putParcelable(mContext.getString(R.string.key_asset), asset);
    detailsIntent.putExtra(MainActivity.EXTRA_ASSET, bundle);
    detailsIntent.setAction(Intent.ACTION_VIEW);

    builder.setIdTag("Asset" + id)
        .setTitle(asset.getTitle())
        .setText(asset.getCast().replace(",", ", "))
        .setGenres(asset.getGenres().split(","))
        .setRunningTime(asset.getDuration())
        .setContentIntentData(ContentRecommendation.INTENT_TYPE_ACTIVITY,
                detailsIntent, 0, null)
        .setStatus(ContentRecommendation.CONTENT_STATUS_AVAILABLE)
        .setPricingInformation(ContentRecommendation.CONTENT_PRICING_SUBSCRIPTION, "$4.99")
        .setContentTypes(new String[]{ContentRecommendation.CONTENT_TYPE_MOVIE, ContentRecommendation.CONTENT_TYPE_SERIAL})
        .setContentImage(Picasso.with(mContext).load(asset.getThumbnailUrl()).get())
        .setProgress((int) (asset.getDuration() % 100000), (int) (asset.getPauseTime() % 100000))
        .setMaturityRating(asset.getRating());

    ContentRecommendation rec = builder.build();
    Notification notification = rec.getNotificationObject(mContext);
    mNotifManager.notify(id, notification);
}

What is causing this error and how do I solve it?


Solution

  • You have to pass different request code in setContentIntentData for each notification.

     .setContentIntentData(ContentRecommendation.INTENT_TYPE_ACTIVITY,
                    detailsIntent, diff_value, null)