Skip to content
This repository has been archived by the owner on Sep 4, 2020. It is now read-only.

Is there any way to clear notifications out from the app? #346

Closed
rwillett opened this issue Nov 17, 2015 · 11 comments
Closed

Is there any way to clear notifications out from the app? #346

rwillett opened this issue Nov 17, 2015 · 11 comments

Comments

@rwillett
Copy link

Hi,

We're trying to clear out notifications from our iOS app when in background mode.

Our notifications are very time dependent, we send personalised traffic information every 2-6 minutes to customers phones. If the customer is using the app and its in the foreground, then we can manage the information quite easily.

When the app is in the background, notifications keep getting sent and there can be a lot of them. A traffic incident could go on for 90 mins, so we could send 20 or so notifications updating the customer on the progress, if there are 2-3 incidents the customer is tracking then we could end up with 60-100 notifications, 95% of which are out of date and not longer relevant.

What we want to do is every time a new set of notifications come in AND the app is in the background, we want to clear out all the existing notifications and start again. This means that there should only be 3-4 notifications at any one time.

We've looked at doing this with phonegap-plugin-push and using cordova-plugin-local-notifications. If we send a silent notification using phonegap-plugin-push we can catch this in our app, and use cordova-plugin-local-notifications to send local notifications to the user. Since we control the local notifications, we can delete these as needed.

However when the app is not started, we no longer see any notifications as the app can't catch the silent notification, which means the app can't send the local notifications, which means the user is unaware.

So we have two choices (AFAIK),

  1. We send normal notifications which are persistent BUT there could be lots of them which confuses the user.
  2. We use silent notifications, manage the display of local notifications ourselves BUT this no longer works when the app is not in background or foreground mode.

Is anybody aware of how to clear out notifications from the app, @194 seems to indicate that this is intractable and not possible.

Thanks

Rob

@bau720123
Copy link

hi @rwillett
this is what you want
https://github.com/katzer/cordova-plugin-badge

or use
options.android.clearNotifications
options.ios.badge
https://github.com/phonegap/phonegap-plugin-push#pushnotificationinitoptions

@rwillett
Copy link
Author

@bau720123

Thanks, we'll look at these and see if the top one helps.

We've played with the options but with another commercial plugin and couldn't get them to work the way we wanted. We'll try again with this one.

Rob

@fredgalvao
Copy link
Collaborator

Or you could just use the stacking notification style so you can make a summary of the past notifications that belong to the same groupings.
Or you could even simply specify the notId on the payload so that only the latest on a group would be shown, making the previous one disappear when a new one arrives.

Read in details in here: https://github.com/phonegap/phonegap-plugin-push#stacking

@rwillett
Copy link
Author

@fredgalvao

Thanks, we were aware of stacking, we use it on the Android version and that seems to mostly solve the problem there. On the iOS version, stacking isn't an option for us. I realise I hadn't actually stated which OS we were using , so have updated the original post to state its iOS. Stupid dumb mistake on our part.

Rob

@netomia
Copy link

netomia commented Dec 5, 2015

Bump... cannot make this work. Clearing notifications work on iOS but not Android.

Did anyone find a solution?

@netomia
Copy link

netomia commented Jan 24, 2016

Can I request a label or a status for this issue?

@Moghul
Copy link

Moghul commented Feb 3, 2016

@netomia Hey, not sure if we're allowed to post code here but I made mine work like this:
In the plugin file/src/android/com/adobe/phonegap/push/GCMIntentService.java I added the following code at the bottom:

private void deleteOldNotification(Bundle extras)
{
    try
    {
        int delete = parseInt("delete", extras);
        if (delete!=0) {
            NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
            String appName = getAppName(this);
            notificationManager.cancel((String)appName, delete);
        }
    }catch (Exception e){
        Log.e(LOG_TAG, "EXCEPTION IN NOTIFICATIONS " + e.getMessage());
    }
}

And I'm calling the method before the if statement at line 97 where it says if(message!=null ...)...

This isn't necessarily the best solution but at the very least, from your server, you can send a delete tag in your payload ("delete" : 10) which will remove the notification you need. You can even send an otherwise empty push notification to clear it without creating a new notification.

@netomia
Copy link

netomia commented Feb 4, 2016

@Moghul - thanks!

Although I can't seem to find the if(message!=null ...) at line 97. Would you mind checking and maybe c/p the lines where you call the function?

@Moghul
Copy link

Moghul commented Feb 4, 2016

@netomia

Maybe it's because I am using an older version of the plugin. In the method onMessage(Context context, Intent intent), I have the following lines:

            //HOME MADE CODE
            deleteOldNotification(extras);

            // Send a notification if there is a message
            String message = this.getMessageText(extras);
            if (message != null && message.length() != 0) {
                createNotification(context, extras);
            }

@netomia
Copy link

netomia commented Feb 4, 2016

@Moghul

I just tried your code in the current version of the plugin, and I'm afraid it didn't work. I tested in on three devices with Android 4, 5 and 6.

Thanks for the attempt though.

@lock
Copy link

lock bot commented Jun 4, 2018

This thread has been automatically locked.

@lock lock bot locked as resolved and limited conversation to collaborators Jun 4, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Development

No branches or pull requests

6 participants