当我使用Firebase FCM时为什么无法删除推送通知?

TIM*_*MEX 9 push-notification apple-push-notifications firebase firebase-cloud-messaging

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);
     });
});
Run Code Online (Sandbox Code Playgroud)

我的选项中有一个collapse-Key字段.

运行此代码时,iPhone会收到多个通知,这些通知全部相互叠加.我想将最近的通知替换为以前的通知.

小智 3

对于 iOS:

使用apns-collapse-id请参阅文档

如果您使用可折叠消息,请记住,FCM 在任何给定时间仅允许每个注册令牌的 FCM 连接服务器最多使用四个不同的折叠键。绝对不能超过这个数字,否则可能会造成不可预测的后果。

可折叠:

使用场景

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

如何发送

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

  • Android 上的折叠键
  • iOS 上的apns-collapse-id
  • 网络主题
  • 遗留协议中的collapse_key(所有平台)

参见文章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
Run Code Online (Sandbox Code Playgroud)

对于安卓:

tag通知负载中使用变量。

"notification":{
  "title":"Huawei",
  "body":"21 Notification received", 
  "sound":"default",
  "badge":4,
  "tag":"1",
  "click_action":"Your_Activity"
   "icon":"Push_Icon" 
}
Run Code Online (Sandbox Code Playgroud)

  • 标签似乎只影响 Android 设备,但 apns-collapse-id 似乎是 IOS 的最佳选择 (2认同)