为什么我在使用 Firebase FCM 时无法折叠我的推送通知? [英] Why can't I collapse my push notifications when I use Firebase FCM?

查看:25
本文介绍了为什么我在使用 Firebase FCM 时无法折叠我的推送通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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);
     });
});

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

I have a collapse-Key field in my options.

当运行此代码时,iPhone 会收到多个通知,所有通知都在彼此之上.我希望用最新的通知替换以前的通知.

When this code is ran, the iPhone receives multiple notifications, all on top of each other. I'd like to have most recent notification replace the previous ones.

推荐答案

对于 iOS:

使用 apns-collapse-id 参见 文档.

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

if you use collapsible messages, remember that FCM only allows a maximum of four different collapse keys to be used by the FCM connection server per registration token at any given time. You must not exceed this number, or it could cause unpredictable consequences.

使用场景

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

When there is a newer message that renders an older, related message irrelevant to the client app, FCM replaces the older message. For example: messages used to initiate a data sync from the server, or outdated notification messages.

如何发送

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

Set the appropriate parameter in your message request:

  • collapseKey 在 Android 上
  • apns-collapse-id 在 iOS 上
  • 网络主题
  • collapse_key 在旧协议(所有平台)中
  • collapseKey on Android
  • apns-collapse-id on iOS
  • Topic on Web
  • collapse_key in legacy protocols (all platforms)

apns-collapse-id 的实现="nofollow noreferrer">文章:

See the implementation of apns-collapse-id in the article:

# 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

对于安卓:

在您的通知负载中使用tag变量.

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

这篇关于为什么我在使用 Firebase FCM 时无法折叠我的推送通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆