Firebase 云消息传递 android 优先级 属性

Firebase cloud messaging android priority property

我正在尝试将通知优先级设置为 HIGH,如文档 here and specific parameter defined here 中所述。但是,当我在我的云函数中设置此 属性 时,我在尝试部署时收到以下错误:

src/index.ts:107:35 - error TS2345: Argument of type '{ tokens: string[]; notification: { title: string; body: any; }; data: { title: any; subtitle: any; body: any; id: any; }; android: { notification: { icon: string; channel_id: string; tag: any; }; priority: string; }; }' is not assignable to parameter of type 'MulticastMessage'.
  The types of 'android.priority' are incompatible between these types.
    Type 'string' is not assignable to type '"normal" | "high" | undefined'.

107     admin.messaging().sendMulticast(message)
                                        ~~~~~~~

我明白这意味着我不应该输入字符串。但根据文档,这是预期的类型。不仅如此,我对它在引号 '"normal | "high" | undefined' 中指的是什么类型感到很困惑。那是什么类型?

这是正在设置的完整消息容器:

const message = {
            tokens: tokens,
            notification: {
                title: snapshot.data().sender_username + " - " + snapshot.data().group_name,
                body: snapshot.data().content
            },
            data: {
                title: snapshot.data().group_name,
                subtitle: snapshot.data().sender_username,
                body: snapshot.data().content,
                id: message_topic
            },
            android: {
                notification: {
                    icon: 'add_circle_outline',
                    channel_id: 'exampleChannelId',
                    tag: message_topic

                },
                priority: "high" // <-- This is where the error is thrown
            }
        };

看来你需要在其他地方设置优先级,才能设置正确,不报错。如果您查看此文档here,它似乎需要以不同的格式编写。

我做了进一步的调查,我在社区上找到了下面这两个帖子,它们可能会帮助您实现将优先级设置为高的目标。

另一篇文章 - Working easily with FCM push notifications in Android - 可能会提供一些关于如何实现该目标的额外想法。

如果这些信息对您有帮助,请告诉我!

由于类型不匹配,IDE 不高兴。

一个解决方案是显式输入 android 配置:

import * as admin from 'firebase-admin';

const androidConfig: admin.messaging.AndroidConfig = {
    priority: 'high', 
    ...,
};

const message = {
    ...,
    android: androidConfig,
};

这很好,因为您可以看到可以设置的其他配置选项。在该注释上,您还可以明确键入整个消息,在本例中为 admin.messaging.MulticastMessage