appfeel / node-pushnotifications

Push notifications for GCM, APNS, MPNS, AMZ (automatic detection from device token)
MIT License
538 stars 126 forks source link

silent push on IOS contains alert field #127

Closed goutamreddy closed 4 years ago

goutamreddy commented 4 years ago

Hi,

When trying to send a silent push notification for IOS, the "alert" key IS sent, with empty content. I believe this prevents the notification from running code in the background. (https://stackoverflow.com/questions/36694963/what-is-silent-push-notification-when-does-the-device-receive-it).
"For a silent notification, take care to ensure there is no alert, sound, or badge payload in the aps dictionary. If you don’t follow this guidance, the incorrectly-configured notification might be throttled and not delivered to the app in the background, and instead of being silent is displayed to the user" Silent push on IOS requires that NO alert key be sent. The payload looks like this:

{"aps": {
alert:{}
"content-available":1
}

but it should look like this:

{"aps": {
"content-available":1
}

In testing, with my app suspended in the background due to memory pressure, the silent notification that contains the alert key does not execute code in the background, but rather waits to execute once the app has been woken up by state restoration / user bringing it into foreground.

Here's my setup (IOS 13.1.2, latest node-pushnotifications 1.2.1, node 13.8.0)

var PUSHNOTIFICATIONS = require('node-pushnotifications');

const settings = {
    gcm: {
        id: null,
        phonegap: false, // phonegap compatibility mode, see below (defaults to false)
    },
    apn: {
        token: {
            key: FS.readFileSync(ENV['APNS_CERTIFICATE']),
            keyId: ENV['APNS_NOTIFICATION_KEY'],
            teamId: ENV['APNS_TEAM_ID'],
        },
        production: false // true for APN production environment, false for APN sandbox environment,
    },
    isAlwaysUseFCM: false, // true all messages will be sent through node-gcm (which actually uses FCM)
};
var push = new PUSHNOTIFICATIONS(settings);

            push_notification_data = {
                topic: ENV['APNS_BUNDLE_ID'],
                contentAvailable: true,
                priority: 'normal',
                pushType: 'background',
                custom: {
                    bluetooth_sync_request: true
                }
            }

        push.send(registrationIds, push_notification_data, function (err, result){
            if (err) {
                console.log("error sending push notification");
                console.log(err);
            } else {
                console.log("success sending push notification");
                console.log(result);
                console.log(registrationIds);
                console.log(push_notification_data);
            }
            return cb(err, result);
        });
alex-friedl commented 4 years ago

The problem is that we set a default value for alert if it is not defined. To remove that default value should be considered a breaking change, because some users may rely on that default. Could it make sense to add a silent field to the data arg, which would always set alert, badge and sound to undefined?

alex-friedl commented 4 years ago

Added new config property silent to data payload. Released with v1.3.0. Docs: https://github.com/appfeel/node-pushnotifications#silent-push-notifications

goutamreddy commented 4 years ago

Hi Alexander,

Thanks for fixing this silent push notification issue. I tested and it is working:!

On Thu, Mar 5, 2020 at 12:23 PM Alexander Friedl notifications@github.com wrote:

Added new config property silent to data payload. Released with v1.3.0. Docs: https://github.com/appfeel/node-pushnotifications#silent-push-notifications

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/appfeel/node-pushnotifications/issues/127?email_source=notifications&email_token=ACBUMIIBWSD52JFGPANKCVTRGAC4TA5CNFSM4K5354GKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEN6YOUQ#issuecomment-595429202, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACBUMINV36DBDQAWF3W5JGTRGAC4TANCNFSM4K5354GA .