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

clearAllNotifications not working on iOS #1080

Closed
purinagripal opened this issue Jul 13, 2016 · 26 comments
Closed

clearAllNotifications not working on iOS #1080

purinagripal opened this issue Jul 13, 2016 · 26 comments

Comments

@purinagripal
Copy link

Expected Behaviour

When the notification event is fired clear the notifications in the notification center.

Actual Behaviour

Notifications stay in the notification center.

Reproduce Scenario (including but not limited to)

Steps to Reproduce

       push.on('notification', function(data) {
            // para que solo descargue las notificaciones una vez (no por cada notificacion)
            if( window.notif_vistas == 0 ) {
                eventosNotificados();
            }
            // borrar notificaciones del centro de notificaciones
            push.clearAllNotifications();
            push.finish(); // para iOS
        });

Platform and Version (eg. Android 5.0 or iOS 9.2.1)

iOS 9.3.2

(Android) What device vendor (e.g. Samsung, HTC, Sony...)

iPad 4

Cordova CLI version and cordova platform version

cordova --version                                    # cli-6.0.0
cordova platform version ios                     # 4.0.1 

Plugin version

cordova plugin version | grep phonegap-plugin-push   # 1.6.2    

Sample Push Data Payload

$body['aps'] = array(
'alert' => 'mensaje de ejemplo',
'sound' => 'default'
);

Sample Code that illustrates the problem

Logs taken while reproducing problem

@macdonst
Copy link
Member

@purinagripal what version of the plugin are you using?

@purinagripal
Copy link
Author

plugin version is 1.6.2, I realized that this version not include the clearAllNotifications function!

I try using a newer version

Thanks

@purinagripal
Copy link
Author

I try with the last version of this plugin 1.7.4 (cordova cli-6.1.0) and I still have the notifications on the notification center...

Is the next code the right way to use clearAllNotifications???

       push.on('notification', function(data) {
            // para que solo descargue las notificaciones una vez (no por cada notificacion)
            if( window.notif_vistas == 0 ) {
                eventosNotificados();
            }
            // borrar notificaciones del centro de notificaciones
            push.clearAllNotifications();
            push.finish(); // para iOS
        });

@purinagripal purinagripal reopened this Jul 14, 2016
@jcesarmobile
Copy link
Collaborator

Is the notification event being fired in first place?

Is your app in foreground, background or fully closed?
Do you open the app tapping a notification from notification center?

@purinagripal
Copy link
Author

I open the app tapping a notification from notification center and the notification event is fired. But the rest of notifications stay in the notification center

@macdonst
Copy link
Member

@purinagripal and by all notifications we only mean the ones sent by your app. It can't clear notifications from other apps.

@purinagripal
Copy link
Author

Yes I know it, but I sent a few notifications and when I tap one and the notification event is fired I would like to clear the rest of notification from my app, but it not work... :(

@jcesarmobile
Copy link
Collaborator

jcesarmobile commented Jul 16, 2016

I've been testing and I've been able to reproduce it a few times, but other times it´s working fine, really strange.

You can also try to set the badge to a value and then to 0

push.setApplicationIconBadgeNumber(function() {
    push.setApplicationIconBadgeNumber(function() {
        console.log('success');
    }, function() {
        console.log('error');
    }, 0);
}, function() {
    console.log('error');
}, 2);

You can also use ios.clearBadge true on the init method

var push = PushNotification.init({
    android: {
        senderID: "12345679"
    },
    ios: {
        alert: "true",
        badge: true,
        sound: 'false',
        clearBadge: 'true'
    },
    windows: {}
});

@jcesarmobile
Copy link
Collaborator

Just found the problem, you have to pass a success callback function, if you open the app on safari remote web inspector to debug it you should see this message:

PushNotification.clearAllNotifications failure: success callback parameter must be a function

So you only have to do this and it will work:
push.clearAllNotifications(function(){});

But probably the clearBadge: 'true' option on the init method should be a better option in your case.

@macdonst, does it makes sense to force the use a success function in this case?
The example uses success and error callbacks, but looking into the code only success will be returned, and probably you don't need the callback (at least on iOS)

@purinagripal
Copy link
Author

Thank you @jcesarmobile , I'm going to try this way. Thank you very much!

@purinagripal
Copy link
Author

I try with push.clearAllNotifications(function(){}); but still not working.

Now I'm trying clearBadge: "true"...

@purinagripal
Copy link
Author

Still not working

@jcesarmobile
Copy link
Collaborator

Are you sure you updated the plugin correctly? the 3 ways I told you are working fine for me

@macdonst
Copy link
Member

@jcesarmobile I'll make a change so the success callback is no longer mandatory.

@macdonst
Copy link
Member

Pushed the change, will be in next point release.

@purinagripal
Copy link
Author

I'm using Phonegap Build and checking the log for the iOS build and I can read the next:

Fetching plugin "[email protected]" via npm
Installing "phonegap-plugin-push" at "1.7.4" for ios

So I supose the plugin is updated.

@purinagripal
Copy link
Author

@jcesarmobile how can I open my app on safari remote web inspector to debug it??

I'm starting debug in iOS and I don't know tools for that...

@purinagripal
Copy link
Author

purinagripal commented Jul 16, 2016

I'm testing with success function handler and error function handler and they are not fired...

push.on('notification', function(data) {
            // para que solo descargue las notificaciones una vez (no por cada notificacion)
            if( window.notif_vistas == 0 ) {
                eventosNotificados();
            }
            // borrar notificaciones del centro de notificaciones
            push.clearAllNotifications(function() {
                console.log('success clear notifications');
            }, function() {
                console.log('error clear notifications');
            });
            push.finish(); // para iOS
        });

@purinagripal
Copy link
Author

When the app is foreground and the notification event is fired, the successHandler function for clearAllNotifications is fired too

@purinagripal
Copy link
Author

Using setTimeout I get the message from the successHandler function but notifications still on notification center (only the one that I tap disapear)

setTimeout( function(){ 
                // borrar notificaciones del centro de notificaciones
                push.clearAllNotifications(function() {
                    console.log('success clear notifications');
                }, function() {
                    console.log('error clear notifications');
                });
            }, 3000);

Maybe something necesary in config.xml???

@kctang
Copy link

kctang commented Jul 17, 2016

Tested with with v1.7.4, this is my observation:

  • setApplicationIconBadgeNumber() to 0 will clear the badge number on Samsung/Android.
  • clearAllNotifications() does not.

Here's the code I used (this._plugin was assigned from PushNotification.init()):

      document.addEventListener('resume', () => {
        // --- #1 works!
        if (this._plugin) {
          this._plugin.setApplicationIconBadgeNumber(
            () => this._log('Push notifications cleared'),
            () => this._log('Error clearing push notifications')
            , 0);
        }

        // --- #2 does not work (on Samsung - Android API 21)
        this._plugin.clearAllNotifications(
          () => this._log('Push notifications cleared'),
          () => this._log('Error clearing push notifications')
        );
      });

@jcesarmobile
Copy link
Collaborator

The issue was about clearing the notifications on iOS notification center, if you are having problems with android open a new issue.

@slackernrrd
Copy link

I had the same issue with push.clearAllNotifications() not working on iOS (in version 1.8.2 of this plugin). This StackOverflow answer http://stackoverflow.com/a/9225972 suggested that you need to first increment the badge number, THEN set it to zero. So I tried replacing push.clearAllNotifications() with

push.setApplicationIconBadgeNumber(function() {}, function() {}, 1);
push.setApplicationIconBadgeNumber(function() {}, function() {}, 0);

and it did the trick.

It may be relevant that I don't use the badge number elsewhere in my app. I noticed that all push.clearAllNotifications() does in the native Objective-C code is indeed set the badge number to 0. I guess if it's already 0 it doesn't do anything. Unfortunately the documentation for UIApplication at https://developer.apple.com/reference/uikit/uiapplication/1622918-applicationiconbadgenumber?language=objc doesn't give any further clues.

@prosenjit-manna
Copy link

I am using plugin version 1.9.2 but still not working on ios.

iOS - 10.0.2

config :
ios: {
alert: true,
sound: true,
clearBadge: true
}

push.clearAllNotifications(function () {
console.log('All system notification removed');
}, function () {
console.log('All system notification not removed: error');
});

In debug mode console.log output >> All system notification removed, but ios notification not cleared.

@sertal70
Copy link

Hi all, I'm having the same issue on plugin v2.0.0 using Cordova iOS 4.4.0.

The only way to clear all notifications from notification center is to use the workaround suggested by @slackernrrd, that is setting the badge to 1 and then back to 0. Obviously that is not ideal if you want to use the badge in your app.

On Android the issue is not present, clearAllNotifications() works perfectly.

I'm wondering why this issue has been closed...

@lock
Copy link

lock bot commented Jun 3, 2018

This thread has been automatically locked.

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

No branches or pull requests

7 participants