当用于展开/折叠的不同布局时,无法从通知托盘取消通知

时间:2015-08-04 04:28:37

标签: android notifications android-remoteview

我在通知栏中有共享选项。所以,我在代码中使用了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);

//为Share Icon注册听众

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();

//将vies分配给通知构建器

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

//创建待处理的意图

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

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

2 个答案:

答案 0 :(得分:0)

试试这个,

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

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

将其替换为,

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

答案 1 :(得分:0)

我解决了我的问题。问题出在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)