node-apn / node-apn

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

What is the right way to send a silent push notification(content-available = 1) in 2024? #731

Open skywalkerlw opened 2 months ago

skywalkerlw commented 2 months ago

I have tried different approaches mentioned in the issue list but none of them work. I'm debugging it in Xcode.

To be highlighted, if I remove content-available in following payload, I can see the push notification, which means my code/certificate/token, etc is right.

// try 1: 

  let note = new apn.Notification();
  note.rawPayload = {
    aps: {
      "content-available": 1,
    },
    alert: {
      categoryName: "AppEvent",
      action: "remindUser3Day",
      notificationId: "0",
      destinationType: "Multiple",
    },
  };;

  note.topic = "com.demo.apac.dev";

  service.send(note, [pushToken]).then((result) => {
    console.log("sent:", result.sent.length);
    console.log("failed:", result.failed.length);
  });
// try 2

  let note = new apn.Notification();
  note.rawPayload = {
    aps: {
      "content-available": 1,
    },
    alert: {
      categoryName: "AppEvent",
      action: "remindUser3Day",
      notificationId: "0",
      destinationType: "Multiple",
    },
  };;

  note.topic = "com.demo.apac.dev";

  // changes
  note.contentAvailable = 1;
  note.priority = 10; //send as soon as possible

  service.send(note, [pushToken]).then((result) => {
    console.log("sent:", result.sent.length);
    console.log("failed:", result.failed.length);
  });
// try 3

 // change
  let note = note = new apn.Notification({
      aps: {
        "content-available": 1,
      },
    });

  note.rawPayload = {
    aps: {
      "content-available": 1,
    },
    alert: {
      categoryName: "AppEvent",
      action: "remindUser3Day",
      notificationId: "0",
      destinationType: "Multiple",
    },
  };;

  note.topic = "com.demo.apac.dev";

  service.send(note, [pushToken]).then((result) => {
    console.log("sent:", result.sent.length);
    console.log("failed:", result.failed.length);
  });
// try 4

  let note = new apn.Notification();
  note.rawPayload = {
    aps: {
      "content-available": 1,
    },
    alert: {
      categoryName: "AppEvent",
      action: "remindUser3Day",
      notificationId: "0",
      destinationType: "Multiple",
    },
  };;

  note.topic = "com.demo.apac.dev";

 // change
 note.aps["content-available"] = 1;

  service.send(note, [pushToken]).then((result) => {
    console.log("sent:", result.sent.length);
    console.log("failed:", result.failed.length);
  });