iOS 的 Firebase FCM 静默推送通知

Firebase FCM silent push notifications for iOS

我在 iOS 上遇到静默通知问题。

当我的应用程序在后台运行时,我没有收到 FCM 发送的静默通知。但是,如果我尝试直接发送到 APNS,则通知已成功收到。

这是发送给 FCM 的 JSON:

{ 
"to" : "<token>",
"priority": "high",
"content_available": true,
"data" : {
  "<key>" : "<string>",
  "<key2>" : "<string>"
}

}

这是直接发送到 APNS 的JSON:

{
  "aps": {
    "content-available": 1
  },
  "<key>": "<string>",
  "<key>": "<string>"
}

我已经尝试删除 "priority" 键,因为我看到有人说如果 "content_available" 已经设置,我不应该设置优先级。没用。

  1. 我在 XCode > 功能中启用了 "Push Notifications"。
  2. 我已经 "Remote notifications" 在 XCode > 功能中检查了背景模式。
  3. 当应用程序在前台时,FCM 通知工作正常,有时当应用程序在后台时。

请按照 documentation for server side 并按照文档中的说明为 json 进行设置。我之前遇到过类似的问题并解决了这个文档的问题。

    {
  "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
  "priority" : "high",
  "content_available": true,
  "notification" : {
    "body" : "",
    "title" : "",
    "icon" : "new"
  },
  "data" : {
    "volume" : "3.21.15",
    "contents" : "http://www.news-magazine.com/world-week/21659772"
  }
}

content_available = true and body, title empty does the task.

我找到了解决方法。我在 "notification" 字段中为 "sound" 设置了一个空值,即使应用程序处于后台,也会发送静默通知。

{ 
    "to" : "...",
    "priority": "high",
    "notification": {
        "sound": ""
    },
    "data" : {
      ....
    }
}

我的直觉是 Apple 不允许具有 'high' 优先级的静默通知,并且 "notification": {"sound": ""} 以某种方式欺骗 APNS,使该通知不是沉默的。

删除“通知”键值对并添加“content_available”:true

看起来像这样

{ 
    "to" : "...",
    "priority": "high",
    "content_available": true,
    "data" : {
      ....
    }
}

这应该使其成为静默 APNS,您需要使用相应的 APNS 委托方法进行处理。

您需要通过委托来处理这个问题 有关详细信息,请参阅此 firebase 文档:https://firebase.google.com/docs/cloud-messaging/concept-options

我正在使用 nodejs 处理 Firebase 静默推送通知。当我尝试下面的代码时,它工作正常。当我添加 "priority": "high" 和 "content_available": true 时出现以下错误。

Worked below code

const admin = require('firebase-admin');
const serviceAccount ="...."; //service account path
admin.initializeApp({
  credential: admin.credential.cert(serviceAccount)
});

let  fcmToken = "...."; // Your token
let message ={
    "token": fcmToken,
    "data": {
        "updateApi": "activity"
    }
} 

admin.messaging().send(message)
  .then((response) =>{
    console.log('Successfully sent notification:', response);
})
  .catch((error) =>{
    console.log('Error while sending notification:', error);
});

Error when I added the priority and content_available in message object

{ code: 'messaging/invalid-argument',
     message: 'Invalid JSON payload received. Unknown name "priority" at \'message\': Cannot find field.\nInvalid JSON payload received. Unknown name "content_available" at \'message\': Cannot find field.' },
  codePrefix: 'messaging' }

如果你用的是cloud function,我查了很多资料,连官方文档都过时了,或者和typescript接口不一样。

这终于对我有用了。与其设置 alert: "",不如 将其保留 ,它是可选的。对于自定义属性,您可以添加到 message.apns.payload。 fcmToken 应该是来自其设备的用户令牌。

  const message: TokenMessage = {
    token: fcmToken,
    apns: {
      headers: {
        "apns-priority": "5",
      },
      payload: {
        aps: {
          contentAvailable: true,
        },
        my_custom_parameter: true,
      },
    },
  };

  admin
    .messaging()
    .send(message)
    .then(() => {
      // do something.
    })
    .catch((error) => {
      functions.logger.error(
        "Error sending push notification: " + error.toString()
      );
      // Do something.
    });
};