Firebase cloud messaging android priority property
Asked Answered
R

2

5

I am attempting to set a notification priority to HIGH, as noted in the docs here and specific parameter defined here. However, when I set this property in my cloud function, I get the following error when attempting to deploy:

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)
                                        ~~~~~~~

I understand this means I shouldn't be entering a string. But according to the docs, that's the expected type. Not only that, but I'm very confused on what type it's referring to in the quotations marks '"normal | "high" | undefined'. What is that type?

Here is the full message container being set:

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
            }
        };
Roux answered 27/2, 2020 at 3:4 Comment(0)
N
1

It seems that you need to set the priority in other locations, for it to be set correctly and no error be thrown. If you check this documentation here, it seems that it would need to be written in a different format.

I did further investigation and I could find these two below posts on the Community, that might help you achieve your goal of setting the priority to high.

This other article - Working easily with FCM push notifications in Android - might provide some additional ideas on how to achieve that.

Let me know if the information helped you!

Newspaper answered 27/2, 2020 at 9:57 Comment(4)
I actually found the solution to be to add a return my admin.messaging().sendMulticast(message)... line after building the message rather than just sending the message, but upvoted and marked as answer since your links were also helpful! First link helped with setting priorityRoux
Hi @Roux thank for that and for providing the confirmation on how you fixed it!Newspaper
@lukecross just saw your question - I had to look specifically at the heads up notification documentation for android. Those requirements are different, which you will specify on the client after the firebase message is received. If you check the section under Heads Up Notification, it will tell you the requirementsRoux
I already resolve it, is needed use payload(data) notifications on backend.Premonitory
B
8

The IDE is unhappy due to a perceived type mismatch.

A solution is to explicitly type the android config:

import * as admin from 'firebase-admin';

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

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

This is nice cause you get to see what other config options you can set. On that note you can also explicitly type your entire message, in this case that would be admin.messaging.MulticastMessage

Bouchard answered 25/5, 2021 at 20:44 Comment(0)
N
1

It seems that you need to set the priority in other locations, for it to be set correctly and no error be thrown. If you check this documentation here, it seems that it would need to be written in a different format.

I did further investigation and I could find these two below posts on the Community, that might help you achieve your goal of setting the priority to high.

This other article - Working easily with FCM push notifications in Android - might provide some additional ideas on how to achieve that.

Let me know if the information helped you!

Newspaper answered 27/2, 2020 at 9:57 Comment(4)
I actually found the solution to be to add a return my admin.messaging().sendMulticast(message)... line after building the message rather than just sending the message, but upvoted and marked as answer since your links were also helpful! First link helped with setting priorityRoux
Hi @Roux thank for that and for providing the confirmation on how you fixed it!Newspaper
@lukecross just saw your question - I had to look specifically at the heads up notification documentation for android. Those requirements are different, which you will specify on the client after the firebase message is received. If you check the section under Heads Up Notification, it will tell you the requirementsRoux
I already resolve it, is needed use payload(data) notifications on backend.Premonitory

© 2022 - 2024 — McMap. All rights reserved.