【问题标题】:Firebase silent apns notificationFirebase 静默 apns 通知
【发布时间】:2016-09-30 21:53:48
【问题描述】:

有没有办法使用 google 的 firebase 发送静默 APNS? 似乎如果应用程序在后台,它总是会向用户显示通知。

谢谢?

【问题讨论】:

  • 使用 googles firebase 是指 Firebase 控制台吗?
  • 我的意思是 api。我想通过我们的 JAVA 服务器发送静默通知,而不是通过控制台。据我所知,您无法发送通知来唤醒您的应用程序并且不会自动在托盘中放置可见通知。

标签: push-notification firebase apple-push-notifications firebase-cloud-messaging silentpush


【解决方案1】:

您可以使用 FCM 服务器 API 发送静默 APNS 消息 https://firebase.google.com/docs/cloud-messaging/http-server-ref

特别是你需要使用:

  • 数据字段:

该参数指定消息的自定义键值对 有效载荷。

例如,使用数据:{"score":"3x1"}:

在 iOS 上,如果消息是通过 APNS 发送的,它代表自定义数据 字段。如果它是通过 FCM 连接服务器发送的,它将是 在 AppDelegate 中表示为键值字典 应用程序:didReceiveRemoteNotification:。

键不应是保留字(“from”或任何开头的字) 与“谷歌”或“gcm”)。不要使用本文中定义的任何词语 表(例如collapse_key)。

建议使用字符串类型的值。您必须将值转换为 对象或其他非字符串数据类型(例如,整数或布尔值) 字符串

  • content-available 字段:

在 iOS 上,使用此字段表示 APNS 中的可用内容 有效载荷。当发送通知或消息并将其设置为 是的,一个非活动的客户端应用程序被唤醒。在 Android 上,数据消息唤醒 默认情况下的应用程序。在 Chrome 上,目前不支持。

完整文档: https://firebase.google.com/docs/cloud-messaging/http-server-ref#downstream-http-messages-json

【讨论】:

  • 嗨,我有类似的情况,但需要稍作修改,比如当我收到推送通知时,应用程序应该在后台激活并执行中给出的任务,(如用户的位置更新代码), - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo ,但在用户点击通知之前,我的应用程序不会激活。那么,当应用程序处于后台时,我将如何完成这项工作?有谁知道这个的解决方案吗?
  • @Moxarth 即使我也在为同样的问题而苦苦挣扎。你能解决这个问题吗?
【解决方案2】:

对于使用 FCM 服务器的真正静默通知(前台和后台),请使用以下字段:

"to" : "[token]",
"content_available": true,
"priority": "high",
"data" : {
  "key1" : "abc",
  "key2" : 123
}

注意:确保您在 FCM 中使用“content_available”“content-available”。它已为 APNS 转换,否则将无法正确接收。这种差异让我困惑了一段时间。

【讨论】:

  • 谢谢! “content_available”正是我所需要的!
  • 如何在 iOS 上接收这个有效载荷,我已经在 Android 上实现了它,但在 iOS 上却没有得到它。请你帮助我好吗?谢谢
  • Error: Invalid JSON payload received. Unknown name "content_available"
  • 救命我的家伙!!
【解决方案3】:

我会在我的博客上更详细地解释这个主题。 http://blog.boxstory.com/2017/01/how-to-send-silent-push-notification-in.html

** 关键点是:“content_available:true”

这是示例 JSON

{
    "to" : "<device>",
    "priority": "normal",
    "content_available": true, <-- this key is converted to 'content-available:1'
    "notification" : {
      "body" : "noti body",
      "title" : "noti title",
      "link": "noti link "
    }
}

注意:如果上面的示例 JSON 被发送,那么通知将是 对用户可见。在下面使用 如果您不希望用户看到 推送通知。

{
  "to": "<device>",
  "priority": "normal",
  "content_available": true <-- this key is converted to 'content-available:1'
}

【讨论】:

  • 始终欢迎提供指向潜在解决方案的链接,但请add context around the link,以便您的其他用户知道它是什么以及为什么存在。始终引用重要链接中最相关的部分,以防目标站点无法访问或永久离线。考虑到仅仅是指向外部站点的链接是Why and how are some answers deleted? 的一个可能原因。
  • @spacitron fcm 是 Firebase 云消息传递,所以他这样做了 ;)
  • 请勿在通知内发送正文、标题、链接,否则用户将收到可见通知。如果您想发送完全静音的通知,请在通知中添加一些自定义参数或将其保留为空白。你可以参考这个链接,stackoverflow.com/questions/47770256/…
【解决方案4】:

对于不使用Legacy HTTP(如其他答案所示)并使用最新的v1 HTTP protocol 的人,我终于找到了发送静默通知的正确方法。

NodeJS 示例使用firebase-admin:

    const message = {
      apns: {
        payload: {
          aps: {
            "content-available": 1,
            alert: ""
          }
        }
      }
    };

    admin
      .messaging()
      .send(message)
      .then(response => {
        // Response is a message ID string.
        console.log("Successfully sent message:", response);
      })
      .catch(error => {
        console.log("Error sending message:", error);
      });

解释:

  • apns 中的有效负载似乎没有被 v1 HTTP protocol 中的 Firebase 转换,因此您需要原始的 "content-available": 1
  • alert: "" 也是必要的。如果您曾经尝试使用Pusher 之类的方式发送静默通知,您会发现只有content-available 无法触发它。相反,添加诸如soundalert 之类的附加字段可以使其工作。见Silent Push Notification in iOS 7 does not work。由于 Firebase 禁止空声,我们可以为此使用空警报。

【讨论】:

    【解决方案5】:

    其他解决方案对我不起作用。 我想要一个向 iOS 和 Android 发送数据消息的解决方案。

    从我的测试中我发现,当我的 iOS 应用程序在后台时可靠地发送数据消息的唯一方法是包含一个空的通知负载。

    另外,正如其他答案所提到的,您需要包含content_availablepriority

    要使用 curl 命令对此进行测试,您需要您的 FCM server key 和来自应用的 FCM 令牌。

    仅适用于 iOS 的 curl 命令示例。 (没有可见通知的可靠数据消息)

    curl -X POST \
      https://fcm.googleapis.com/fcm/send \
      -H 'authorization: key=server_key_here' \
      -H 'content-type: application/json' \
      -d '{
      "to": "fcm_token_here", 
      "priority": "high",
      "content_available": true,
      "notification": {
        "empty": "body"
      },
      "data": {
        "key1": "this_is_a_test",
        "key2": "abc",
        "key3": "123456",
      }
    }'
    

    将上面的server_key_herefcm_token_here 替换为您自己的。

    AppDelegate 类中的以下方法应在应用处于后台且不显示 UI 消息时调用。

    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
                     fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        //get data from userInfo
    
        completionHandler(UIBackgroundFetchResult.newData)
    }
    

    以下是使用云函数和打字稿发送到主题的方法。

    const payload = {
        notification: {
            empty: "message"
        },
        data: {
            key1: "some_value",
            key2: "another_value",
            key3: "one_more"
        }
    }
    
    const options = {
        priority: "high",
        contentAvailable: true //wakes up iOS
    }
    
    return admin.messaging().sendToTopic("my_topic", payload, options)
        .then(response => {
            console.log(`Log some stuff`)
        })
        .catch(error => {
            console.log(error);
        });
    

    以上内容似乎始终适用于 iOS,有时也适用于 Android。 我得出的结论是,我的后端需要在发送推送通知之前确定平台才能最有效。

    【讨论】:

    • 感谢示例 URL 命令!为我节省了大量测试消息传递问题的时间!
    猜你喜欢
    • 2017-12-08
    • 2018-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-23
    • 2019-05-06
    • 2016-11-03
    相关资源
    最近更新 更多