node-apn / node-apn

:calling: Apple Push Notification module for Node.js
MIT License
4.37k stars 681 forks source link

Send Push Notification To Apple Watch #726

Open remiroz opened 10 months ago

remiroz commented 10 months ago

I am building node.js back-end to send push notifications to Apple Watch.

"apn": "^2.2.0", "node": "16.14.2"

This is how I send notification to watch.

exports.sendNotification = async (req, res) => {
  try {
    var apnOptions = {
      token: {
        key: fs.readFileSync("./AuthKey_GZW32P73M4.p8"),
        keyId: process.env.APPLE_KEY_ID,
        teamId: process.env.APPLE_TEAM_ID,
      },
      production: true,
    };

    var apnProvider = new apn.Provider(apnOptions);
    var notification = new apn.Notification();
    notification.topic = process.env.APPLE_BUNDLE_ID;
    notification.payload = {
      aps: {
        alert: {
          title: "Notification Title",
          body: "Notification Body",
        },
        badge: 1,
        sound: "default",
        category: "CUSTOM_CATEGORY",
        "mutable-content": 1,
      },
    };
    const sendingStatus = await apnProvider.send(
      notification,
      "69baf9554ed8e8ed814460cf9465fc9fbfd5ef63234610b042c9db8aa60bdf01"
    );
    return res.send({ sendingStatus });
  } catch (error) {
    console.log(error);
    res.status(500).json({ error });
  }
};

FYI, the AuthKey file is in the right place and the .env file contains all env credentials including Key_id, Team_id and Bundle_id.

I tried to call this endpoint using Postman and got this response.

image

However, the watch never received a notification even though the APN returned a success status to me.

I wonder if there is something wrong with my notification payload. Can anyone help me with this?