0

I have develop an android app. this app generate notification and I have also to dismiss them and act when dismiss.

If I receive one notif and dismiss it, the intent is received. and it works if I receive one, dismiss, receive one, dismiss...

But in case I receive 5 notifications and start dismissing them, as soon as I dismiss the first one, the intent is received but never after, I

I was expected to be able to get 5 intents as 5 dismisses. In my case, the notification manager dismiss them but app only receive one.

private final BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            //aVariable = 0; // Do what you want here
            mContext.unregisterReceiver(this);
            if(intent.getAction().equals(NOTIFICATION_DELETED_ACTION)) {
                mNotififDismissId = intent.getExtras().getInt("IdDismiss");

                if(mNotififDismissId == globalTweetNotif)
                    cancelAll(context);
                else
                    cancelNotification(context, mNotififDismissId);
            }
        }
    };

When creating a new notif, I'm doing :

private void singleNotification() {

    Intent resintent = new Intent(NOTIFICATION_DELETED_ACTION);
    resintent.putExtra(rIdDismiss",mNotification.get(mNotification.size() - 1).mTwitterNotifId);

    PendingIntent pendintIntent = PendingIntent.getBroadcast(mContext, 0, resintent, PendingIntent.FLAG_CANCEL_CURRENT);
    mContext.registerReceiver(receiver, new IntentFilter(NOTIFICATION_DELETED_ACTION));

    Notification notification = new Notification(android.R.drawable.btn_default,
            mNotification.get(mNotification.size() - 1).mTwitterNotifTitle.toString(), System.currentTimeMillis());

    Intent notificationIntent = new Intent(mContext, TwitterHomeActivity.class);
    notificationIntent.putExtra("IS_NOTIFICATION", true);
    notificationIntent.putExtra("Title", mContext.getResources().getString(R.string.app_name));
    notificationIntent.putExtra("Text", mNotification.get(mNotification.size() - 1).mTwitterNotifText.toString());

    notificationIntent.putExtra("Url", mNotification.get(mNotification.size() - 1).mTwitterImgUrl.toString());
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
            Intent.FLAG_ACTIVITY_CLEAR_TASK);
    PendingIntent contentIntent = PendingIntent.getActivity(mContext, mNotification.get(mNotification.size()-1).mTwitterNotifUniqueId, notificationIntent,PendingIntent.FLAG_CANCEL_CURRENT);// );

    notification.setLatestEventInfo(mContext,
            mContext.getResources().getString(R.string.app_name),
            mNotification.get(mNotification.size()-1).mTwitterNotifText.toString(),
            contentIntent);

    notification.contentIntent = contentIntent;
    notification.defaults |= Notification.DEFAULT_SOUND;
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notification.deleteIntent = pendintIntent;

    mNotificationManager.notify(mNotification.get(mNotification.size() - 1).mTwitterNotifId, notification);
}

The NotificationManager is got by:

 mNotificationManager = (NotificationManager)
                mContext.getSystemService(Context.NOTIFICATION_SERVICE);

The default behavior works fine, receive, dismiss,,, but I need to also be able to receive, receive, recieve, dismiss, dismiss, dismiss

Any Idea

Seb
  • 2,929
  • 4
  • 30
  • 73
  • You are unregistering your receiver when you get the first call to `onReceive()`. But you already figured that out, right? ;-) Please answer your own question and accept that as the correct answer. – David Wasser Jan 20 '16 at 18:46

1 Answers1

0

If you are receiving multiple notifications and you are not able to dismiss all of them. Then issue can be the id passed to your pending intent. You need to pass different id's for every notification which will be generated from the defined pending intent. Please change this line of your code.

PendingIntent pendintIntent = PendingIntent.getBroadcast(mContext, id, resintent, PendingIntent.FLAG_CANCEL_CURRENT);

where id will be different int values each time and not always 0.

Gautam
  • 3,252
  • 3
  • 23
  • 32
  • thanks Gautam. It's not fixing the issue. I still only have one intent get in the broadcast onReceive() – Seb Jan 18 '16 at 17:25
  • then you need to check mTwitterNotifid when you are sending and when you are receiving in your broadcast receiver. It can be the case the id which you are fetching in your broadcast receiver is same or different from the id which has generated the notification – Gautam Jan 18 '16 at 17:41
  • Thx Gautam but it's the same/ the id are different. I'm getting I generated the notification using the id as position in a list. the list is cleaned only when I dismiss. All Id are unique. – Seb Jan 18 '16 at 17:52
  • 1
    Yes but the broadcast receive seems not receiving anything. I have put a log in the onReceive without any checks. Perhaps I should not unregister in the onReceive – Seb Jan 18 '16 at 18:09