【问题标题】:Why can't I collapse my push notifications when I use Firebase FCM?为什么我在使用 Firebase FCM 时无法折叠我的推送通知?
【发布时间】:2018-01-02 01:24:15
【问题描述】:
const options = {
    priority: 'high',
    collapseKey: user_id
};
const deviceTokensPromise = db.ref('/users-fcm-tokens/' + user_id).once('value');
deviceTokensPromise.then(tokensSnapshot => {
    if (!tokensSnapshot.hasChildren()) {
        return console.log('There are no device tokens to send to.');
    }
    const tokens = Object.keys(tokensSnapshot.val());
    console.log(tokens);
    console.log(payload);
     return admin.messaging().sendToDevice(tokens, payload, options).then(response => {
         console.log(response);
         return removeInvalidFCMTokens(tokensSnapshot, response);
     });
});

我的选项中有一个折叠键字段。

运行此代码时,iPhone 会收到多个通知,所有通知相互叠加。我想让最近的通知替换以前的通知。

【问题讨论】:

  • 我已经更新了我的答案,请看下文。

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


【解决方案1】:

@Timex 你可以为所有具有相同collapse_id 的通知传递same notification id。为此,您需要实现自己的SendNotification method

【讨论】:

    【解决方案2】:

    查看Firebase's FCM Messages documentation 中的“交付选项”部分。

    在 Android 上通过 FCM 的 collapse_key 支持“可折叠”消息行为,在 iOS 上通过 apns-collapse-id 支持,在 JavaScript/Web 上通过 Topic 支持。

    直觉上,您可能认为apns-collapse-id 设置可能会进入您正在使用的sendToMessage 方法中传递的options 参数。然而,这种情况并非如此。而是尝试将其修补到 payload 对象中,如下所示:

    const patchedPayload = Object.assign({}, payload, {
        apns: {
            headers: {
                'apns-collapse-id': user_id
            }
        }
    });
    

    这遵循上面链接的文档中提供的payload 格式。

    一旦您构建了这个修补的有效负载,请不要忘记将sendToDevice(tokens, payload, options) 更新为sendToDevice(tokens, patchedPayload, options)

    希望这对你有用!

    【讨论】:

    • 嗨我有一个类似的问题described here。我尝试了您的解决方案,但收到以下错误消息:错误:消息负载包含无效的“apns”属性。有效属性是“数据”和“通知”。你看出什么问题了吗?
    【解决方案3】:

    对于 iOS:

    使用apns-collapse-id 请参阅docs

    如果您使用可折叠消息,请记住 FCM 仅允许 FCM 连接服务器在任何给定时间为每个注册令牌使用最多四个不同的折叠密钥。不得超过此数量,否则可能导致无法预料的后果。

    可折叠:

    使用场景

    当有新消息呈现与客户端应用无关的旧消息时,FCM 会替换旧消息。例如:用于从服务器启动数据同步的消息,或过时的通知消息。

    如何发送

    在您的消息请求中设置适当的参数:

    • collapseKey 在 Android 上
    • apns-collapse-id 在 iOS 上
    • 主题在网络上
    • 旧协议中的 collapse_key(所有平台)

    article中查看apns-collapse-id的实现:

    # Script to send push notifications for each song in a Phish Setlist via an updateable Push Notification.
    # Place a config.yml in the same directory as the script and your push notification PEM file.  
    #
    # Config Format:
    
    # push_token: XXXXXXXXXXXXXX
    # phish_api_key: XXXXXXXXXXXXXX
    # push_mode: XXXXXXXXXXXXXX # development or production
    
    require 'apnotic'
    require 'phish_dot_net_client'
    require 'awesome_print'
    require 'yaml'
    
    show_date = ARGV[0]
    
    if show_date 
    
        script_config = YAML.load(File.read(File.expand_path('../config.yml', __FILE__)))
    
        PhishDotNetClient.apikey = script_config["phish_api_key"]
    
        the_show = PhishDotNetClient.shows_setlists_get :showdate => show_date
    
        push_body = ""
    
        if script_config["push_mode"] == "development"
            connection = Apnotic::Connection.new(cert_path: "pushcert.pem", url: "https://api.development.push.apple.com:443")
        else
            connection = Apnotic::Connection.new(cert_path: "pushcert.pem")
        end
    
        token = script_config["push_token"] 
    
        notification       = Apnotic::Notification.new(token)
    
        notification.apns_id = SecureRandom.uuid
    
        notification.apns_collapse_id = "Phish " + the_show[0]["showdate"] + ": "
    
        notification.mutable_content = true
    
        the_show[0]["setlistdata"].sets.each do |set_data|
            set_name = set_data.name + ": "
            set_data.songs.each do |song|
                song_str = set_name + song.title
                push_body = push_body + set_name + song.title + "\n"
                set_name = ""
                push_content = {'title' => song_str, 'body' => push_body} 
                puts push_content
    
                notification.alert = push_content
    
                response = connection.push(notification)
    
                # read the response
                puts ""
                puts response.ok?      # => true
                puts response.status   # => '200'
                puts response.headers  # => {":status"=>"200", "apns-id"=>"XXXX"}
                puts response.body     # => ""
                puts ""
    
                sleep(5)
    
            end
    
        end
    
        connection.close
    else
        puts "Usage ruby send_push.rb SHOWDATE(Format:YYYY-MM-DD)"
    end
    

    对于安卓:

    在您的notification payload 中使用tag 变量。

    "notification":{
      "title":"Huawei",
      "body":"21 Notification received", 
      "sound":"default",
      "badge":4,
      "tag":"1",
      "click_action":"Your_Activity"
       "icon":"Push_Icon" 
    }
    

    【讨论】:

    • 标签似乎只影响安卓设备,但 apns-collapse-id 似乎是 IOS 的方式
    【解决方案4】:

    我建议使用firebase-admin 中最新的send 函数,用法描述为here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-22
      • 1970-01-01
      • 2016-10-23
      相关资源
      最近更新 更多