【问题标题】:Unable to cancel the notification from the notification tray when different layouts used for expand/collapse当用于展开/折叠的布局不同时,无法取消通知托盘中的通知
【发布时间】:2015-08-04 04:28:37
【问题描述】:

我在通知托盘中有一个共享选项。所以,我在我的代码中使用了 2 个自定义视图。

1. Expandable Notification Layout
2. Normal Notification Layout

用户界面运行良好。但是,通知行为不端。如果我单击通知中的first notification or share first item,它会完美运行。但是,如果我点击last notification,它会打开应用程序,但通知不会从通知托盘中清除。另外,奇怪的是,点击通知后,状态栏中的small icon 消失了,现在如果我点击通知,它没有响应。我要取消通知。这就是为什么第二次点击它不起作用的原因。当我将默认构建器布局用于普通视图时,这并没有发生。 这是我的代码:

//设置扩展通知布局

 RemoteViews expandedView=new RemoteViews(context.getPackageName(), R.layout.custom_notification); 
  expandedView.setImageViewResource(R.id.image_logo, R.drawable.ic_launcher);
        SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm aa");
        String time = dateFormat.format(new Date(when));
            expandedView.setTextViewText(R.id.current_time, time);
            expandedView.setTextViewText(R.id.title, context.getResources().getString(R.string.app_name));


            expandedView.setTextViewText(R.id.text, message);
expandedView.setTextViewCompoundDrawables(R.id.share, R.drawable.gcm_share, 0, 0, 0);

//设置正常的通知布局

    RemoteViews collapsedView=new RemoteViews(context.getPackageName(), R.layout.custom_notification_normal_layout);
    collapsedView.setTextViewText(R.id.text, message);
            notificationId = ((int) System.currentTimeMillis() % 1000000);

//注册分享图标的监听器

setBtnListeners(expandedView, requestID, message, context, notificationId);


            `Bitmap largeIcon = ((BitmapDrawable) context.getResources().getDrawable(R.drawable.ic_launcher)).getBitmap()`;

//构造通知生成器

 NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(context);
         mNotifyBuilder
                            .setWhen(when)
                            .setSmallIcon(icon)
                            .setLargeIcon(largeIcon)
                            .setContentTitle(message)
                            .setStyle(new NotificationCompat.BigTextStyle().bigText(message));
            notification = mNotifyBuilder.build();

//将竞争分配给通知构建器

 notification.bigContentView = expandedView;
            notification.contentView = collapsedView;

//创建待处理的Intent

Intent notificationIntent;

        //notificationIntent = new Intent(context, Launcher.class);
        notificationIntent = new Intent(context, SplashActivity.class);//Sritapana189
        notificationIntent.putExtra(BundleKeys.NORMAL_PUSH, 1);
        notificationIntent.putExtra(BundleKeys.DEFAULT_NOTI_NAV, nav);
        notificationIntent.putExtra(BundleKeys.FROM_GCM, true);
        notificationIntent.putExtra(ApplicationConstants.GCMKeys.GCM_NOTIFICATION_ID, notificationId);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
  PendingIntent contentIntent = PendingIntent.getActivity(context, requestID, notificationIntent, 0); //Modified
        notification.contentIntent = contentIntent;
 notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notificationManager.notify(notificationId, notification);

//注册通知栏的分享图标

   private static void setBtnListeners(RemoteViews notificationView, int requestId, String message, Context context, int notificationId) {
        Intent shareBtnIntent = new Intent(context, GcmTransparentActivity.class);
        shareBtnIntent.putExtra(ApplicationConstants.GCMKeys.BREAKING_NEWS, message);
        shareBtnIntent.putExtra(ApplicationConstants.GCMKeys.GCM_NOTIFICATION_ID, notificationId);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, requestId, shareBtnIntent, 0);
        notificationView.setOnClickPendingIntent(R.id.share, pendingIntent);
    }

//SplashActivity 接收 Pending Inent

//这里我正在检索有效负载数据并保存它,然后取消通知

Utility.cancelNotification(gcmNotificationId, getApplicationContext());


public static void cancelNotification(int id, Context ctx) {
        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager nMgr = (NotificationManager) ctx.getSystemService(ns);
        nMgr.cancel(id);
    }

我在代码中所做的唯一更改是为普通视图添加了自定义布局。在此更改之后,单击最后一个通知项时行为异常。有什么我想念的吗。请帮我解决这个奇怪的问题。

【问题讨论】:

    标签: android notifications android-remoteview


    【解决方案1】:

    试试这个,

    问题可能是因为您没有正确获取通知 ID 可以快速修复,

    Notification nNotificationManager nMgr = (NotificationManager) ctx.getSystemService(ns); nMgr.cancel(id);

    将其替换为 ,

    Notification nNotificationManager nMgr = (NotificationManager) ctx.getSystemService(ns);
    nMgr.cancelAll();
    

    【讨论】:

    • 根据我的要求,只有被点击的项目必须被取消。所以,我不能使用 cancelAll() 方法
    【解决方案2】:

    我解决了我的问题。问题在于RemoteViews。我已将它们设为本地并最终解决了我的问题

     final RemoteViews expandedView=new RemoteViews(context.getPackageName(), R.layout.custom_notification); 
    final RemoteViews collapsedView=new RemoteViews(context.getPackageName(), R.layout.custom_notification_normal_layout)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-10
      • 2018-08-01
      相关资源
      最近更新 更多