【问题标题】:Firebase cloud messaging android priority propertyFirebase 云消息传递 android 优先级属性
【发布时间】:2020-02-27 03:04:08
【问题描述】:

我正在尝试将通知优先级设置为HIGH,如文档here 和定义的特定参数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
            }
        };

【问题讨论】:

    标签: node.js firebase-cloud-messaging firebase-admin


    【解决方案1】:

    看来您需要在其他位置设置优先级,才能正确设置并且不会抛出错误。如果您查看此文档here,它似乎需要以不同的格式编写。

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

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

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

    【讨论】:

    • 我实际上找到了解决方案,即在构建消息后添加return 我的admin.messaging().sendMulticast(message)... 行,而不是仅仅发送消息,但由于您的链接也很有帮助,因此投票并标记为答案!第一个链接有助于设置优先级
    • 您好@Amin,感谢您并确认您如何修复它!
    • @lukecross 刚刚看到您的问题 - 我必须专门查看 heads up notification documentation for android。这些要求是不同的,您将在收到 firebase 消息后在客户端上指定。如果您检查抬头通知下的部分,它将告诉您要求
    • 我已经解决了,需要在后端使用有效载荷(数据)通知。
    【解决方案2】:

    由于感知到的类型不匹配,IDE 不满意。

    一种解决方案是显式键入 android 配置:

    import * as admin from 'firebase-admin';
    
    const androidConfig: admin.messaging.AndroidConfig = {
        priority: 'high', 
        ...,
    };
    
    const message = {
        ...,
        android: androidConfig,
    };
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-27
      • 1970-01-01
      相关资源
      最近更新 更多