首页 > 解决方案 > Flutter - 后台通知在 iOS 中不起作用

问题描述

我正在 Flutter 中创建一个公共聊天应用程序,当我从 Cloud Function触发该方法时,我无法iOS上接收后台通知。admin.messaging().send(payload)

这是我在 Cloud Function 中的有效负载:

var payload = {
  notification: {
    title: `My Title`,
    body: `My message`,
  },
  android: { priority: "high" },
  apns: {
    payload: {
      aps: {
        contentAvailable: true,
      },
    },
    headers: {
      "apns-push-type": "background",
      "apns-priority": "10",
      "apns-topic": "io.flutter.plugins.firebase.messaging", // bundle identifier
    },
  },
  topic: `mytopic`,
};

我尝试了一堆不同的有效载荷:

感谢官方文档,我仔细设置了我需要配置的所有内容: https ://firebase.flutter.dev/docs/messaging/usage/

我还检查了 Github 问题,例如: 104161125988164443004097

什么工作:

什么不工作:

标签: firebaseflutterbackgroundmessagingpayload

解决方案


我终于解决了这个问题!

我只是删除了"apns-push-type": "background"有效载荷中的行。现在它正在工作。

这条线似乎"apns-topic"也没用。

这是我的最终有效载荷:

 var payload = {
    notification: {
      title: `# ${context.params.passion}`,
      body: `${newMsg["senderPseudo"]} ${
        type == "image" ? "a envoyé une image." : `: ${newMsg["message"]}`
      }`,
    },
    // Set Android priority to "high"
    android: {
      priority: "high",
    },
    // Add APNS (Apple) config
    apns: {
      payload: {
        aps: {
          contentAvailable: true,
        },
      },
      headers: {
        //"apns-push-type": "background", // This line prevents background notifications to be displayed
        "apns-priority": "10",
      },
    },
    token: "dnqTQVso60GfnnuOjHv8_e:APA91bElr-K3xkQMdYHX8VMrMZNCYCjO4zJlGseRh25AS_GT7cg9zlOGdQl4KXvr88ypeWjZjrPzrLRHitsQ-JKQK057ZQb_36c_lfsNjHXbYMYI2iS3jV_HGWf7Ene-ZlPvOb0aRr8u"
 };

推荐阅读