0

I have a share option in the notification tray. So, I have used 2 custom views in my code.

1. Expandable Notification Layout
2. Normal Notification Layout

The UI is working fine. But, notifications are misbehaving. If I click the first notification or share first item in the notification, it works perfectly. But, If I click the last notification, then it opens the app but notification is not cleared from the notification tray. Also, the strange thing is, after clicking the notification,small icon which comes in the status bar disappears and now if i click the notification, it doesn't respond. I'm cancelling the notification. That's why second time, when i click it doesn't work. This was not happening when I used default builder layout for normal view. Here is my code:

//setup a expanded notification layout

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

//set a normal notification layout

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

//register listenr for Share Icon

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


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

//Construct notification Builder

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

//assign the vies to the notification builder

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

//create pending 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);

//Registering the share icon of notification tray

   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 recieving Pending Inent

//here I'm retrieving the payload data and saving it and then cancelling the notification

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

The only change i did in my code is I added custom layout for the normal view.After this change, it's misbehaving when last notification item is clicked. Is there anything I'm missing. please help me to sort out this strange issue.

Sangeetha Pinto
  • 1,022
  • 3
  • 14
  • 32

2 Answers2

0

try this,

problem may be because you are not getting the notification id properly quick fix can be,

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

replace this with ,

Notification nNotificationManager nMgr = (NotificationManager) ctx.getSystemService(ns);
nMgr.cancelAll();
Ankit
  • 101
  • 5
0

I resolved my issue. The problem was with the RemoteViews. I had made them local and making them final solved my issue

 final RemoteViews expandedView=new RemoteViews(context.getPackageName(), R.layout.custom_notification); 
final RemoteViews collapsedView=new RemoteViews(context.getPackageName(), R.layout.custom_notification_normal_layout)
Sangeetha Pinto
  • 1,022
  • 3
  • 14
  • 32