1
votes

I am developing iOS app with Phonegap.

I am implementing the process which incrementing badge number when app receives push notification usijng phonegap-plugin-push.

On the server side which sending push notification, the badge number is not configured.

I want to implement the process when app receives push notification, app counts the current badge number, increments it and set the badgenumber.

Using push.getApplicationIconBadgeNumber and push.setApplicationIconBadgeNumber, below my code works well when app is Foreground, But it doesn't work when app is Background, Suspended、Not running.

var app = {
    // Application Constructor
    initialize: function() {
        this.bindEvents();
    },
    bindEvents: function() {
    document.addEventListener('deviceready', this.onDeviceReady, false);
    },
    onDeviceReady: function() {
        console.log('Received Device Ready Event');
        console.log('calling setup push');
        console.log('platform : '+device.platform);

        if ((device.platform == 'iOS') || (device.platform == 'Android')) {
            app.setupPush();
        }
    },
    setupPush: function() {
        console.log('calling push init');
        var push = PushNotification.init({
            "android": {
                "senderID": "xxxxxxxxxx"
            },
            "browser": {},
            "ios": {
                "sound": true,
                "vibration": true,
                "badge": true
            },
            "windows": {}
        });

        push.on('registration', function(data) {
            console.log('registration event: ' + data.registrationId);

            var oldRegId = localStorage.getItem(key);

            console.log('oldRegId : ' + oldRegId);

            if (oldRegId !== data.registrationId) {
                console.log('different ID');
                localStorage.setItem(key, data.registrationId);
            }
        });

        push.on('error', function(e) {
            console.log("push error = " + e.message);
        });

        push.on('notification', function(data) {
            console.log('notification event');
            navigator.notification.alert(
                data.message,         // message
                null,                 // callback
                data.title,           // title
                'Ok'                  // buttonName
            );

            push.getApplicationIconBadgeNumber(function(count) {
                console.log('get badge : ' + count);
                count++;

                push.setApplicationIconBadgeNumber(function() {
                    console.log('set badge : ' + count);
                }, function() {
                    console.log('set badge error');
                }, count);

            }, function() {
                console.log('get badge error');
            });
        });
    }
};

On this site,

https://github.com/phonegap/phonegap-plugin-push/blob/master/docs/API.md#pushfinishsuccesshandler-errorhandler-id---ios-only

push.setApplicationIconBadgeNumber is explained "Set the badge count visible when the app is not running", and push.getApplicationIconBadgeNumber is explained "Get the current badge count visible when the app is not running".

But this my code, both 2 function works when app is Foreground, and the badge number is not changed when app receives push notification on background state.

So, is the explanation written on that site wrong?

App works the same, nevertheless the push notification option "content-available" is configured 1 or not on the push notification sending server side.

So, please tell me what the code should I write.

-- Versions --

crodova version : 7.1.0

platform iOS : 4.5.4

phonegap-plugin-push : 1.10.6

1

1 Answers

0
votes

I resolved it by myselr.

But, badge number is not updated when app states is "Not runnnig".

I supposes it is not possible to update badge number when app states is "Not runnnig".

So, it would be the specification of this app.

If you want to update badge number when app states is "Not running", I think you can realize it with define badge number at the sending server side.

Below steps can resolve my problem.

*** 1 : Using cordova-plugin-background-fetch

Write below in config.xml.

<plugin name="cordova-plugin-background-fetch" source="npm" />

*** 2 : Enable "Background Refresh" of this app

For iPhone, you can enable it with tapping "Settings -> General -> Background App Refresh".

*** 3 : Configure content-available=1 & id to sending data.

Below is the example of the sending data.

{ \"aps\": { \"alert\": \"Hello World\",\"content-available\": 1 }, \"id\" : 3 }

In this example, id is configured to 3.

This id is used at push.finish function (See next section).

*** 4 : Add push.finish function

Add this function into "push.on('notification', function(data){});".

The name of id of data.additionalData is corresponding to that of id of the sending data.

(Checking the specification of push.finish function at official site)

push.on('notification', function(data) {
    console.log('notification event');

    navigator.notification.alert(
        data.message,         // message
        null,                 // callback
        data.title,           // title
        'Ok'                  // buttonName
    );

    push.getApplicationIconBadgeNumber(function(count) {
        //alert('get badge : ' + count);
        console.log('get badge : ' + count);
        count++;

        push.setApplicationIconBadgeNumber(function() {
            console.log('set badge : ' + count);
        }, function() {
            console.log('set badge error');
        }, count);

    }, function() {
        console.log('get badge error: ' + count);
    });


    //alert('notId: ' + data.additionalData.notId);
    console.log('notification id: ' + data.additionalData.id);

    // do something with the push data
    // then call finish to let the OS know we are done
    push.finish(function() {
        console.log("processing of push data is finished");
    }, function() {
        console.log("something went wrong with push.finish for ID =", data.additionalData.id)
    }, data.additionalData.id);

});