Duplicate notifications when sending to both iOS and watchOS


I’m writing an app that works across iOS and watchOS, and I’m trying to send push notifications to it. Apple’s documentation on sending notifications to watchOS recommends sending the same notification to both iOS and watchOS to ensure that it’s delivered properly, and it says that the system will only show one of those to the user. However, when I send the notification, it’s showing up twice on the watch.

Here’s the code I’m working with (I’m using the Node.js @parse/node-apn library for the server side):

import apn from "@parse/node-apn";

let devices = [
    {"token": "[iOS push token]", "topic": "[app bundle ID]"},
    {"token": "[watchOS push token]", "topic": "[app bundle ID].watchkitapp"}
];

const provider = new apn.Provider({
    token: <redacted>,
    production: false,
});

let num = Math.floor(Math.random() * 1000); // so I can tell different attempts apart
let id = crypto.randomUUID();

async function push(device) {
    let notification = new apn.Notification();
    notification.body = "Hello World " + num;
    notification.pushType = "alert";
    notification.topic = device.topic;
    notification.id = id;

    let res = await provider.send(notification, [device.token]);
    console.log(notification, res.sent[0] || res.failed[0]);
    return res;
}

(async () => {
    for (let d of devices) {
        await push(d);
    }
})()
        .then(r => {process.exit(0);})
        .catch(e => {console.error(e); process.exit(1);});

I’m assuming that the one notification on the watch is what’s sent to my iPhone and the other is what’s being sent to the watch directly, though the documentation seems to indicate that they should be automatically merged. Does anyone have an idea of why these aren’t being merged?

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img