FCM handbook

Firebase Messaging

Options for interacting with FCM servers include the following:

  • The Firebase Admin SDK, which has support for?Node,?Java,?Python,?C#, and?Go.
  • The?FCM HTTP v1 API, which is the most up to date of the protocol options, with more secure authorization and flexible?cross-platform messaging capabilities?(the Firebase Admin SDK is based on this protocol and provides all of its inherent advantages). Because new features typically are added to the HTTP v1 API only, we recommend using this API for most use cases.
  • The?legacy HTTP?protocol. New projects are strongly recommended to adopt the FCM v1 HTTP API instead of the legacy protocol.
  • The?legacy XMPP?server protocol. New projects are strongly recommended to adopt the FCM v1 HTTP API instead of the legacy protocol.

Firebase Admin SDK for FCM

The Admin FCM API handles authenticating with the backendand facilitates sending messages and managing topic subscriptions. With the Firebase Admin SDK, you can:

  • Send messages to individual devices
  • Send messages to topics and condition statements that match one or more topics.
  • Subscribe and unsubscribe devices to and from topics
  • Construct message payloads tailored to different target platforms

The Admin Node.js SDK provides methods for sending messages to device groups.

  • Migrate from legacy HTTP to HTTP v1

Topic is for broadcast events which are not time critical .

Some things to keep in mind about topics:

  • Topic messaging is best suited for content such as weather, or other publicly available information.
  • Topic messages are?optimized for throughput rather than latency. For fast, secure delivery to single devices or small groups of devices,?target messages to registration tokens, not topics.
  • If you need to send messages to multiple devices?per user, consider?device group messaging?for those use cases.
  • Topic messaging supports unlimited subscriptions for each topic. However, FCM enforces limits in these areas:One app instance can be subscribed to no more than 2000 topics.If you are using?batch import?to subscribe app instances, each request is limited to 1000 app instances.The frequency of new subscriptions is rate-limited per project. If you send too many subscription requests in a short period of time, FCM servers will respond with a?429 RESOURCE_EXHAUSTED?("quota exceeded") response. Retry with exponential backoff.

Stop Abusing Priority

Set and manage message priority ?|? Firebase Cloud Messaging

High priority messages on Android are meant for time sensitive, user visible content, and should result in user-facing notifications. If FCM detects a pattern in which messages do not result in user-facing notifications, your messages may be deprioritized to normal priority. FCM uses 7 days of message behavior when determining whether to deprioritize messages; it makes this determination independently for every instance of your application. If, in response to high priority messages, notifications are displayed in a way that is visible to the user, then your future high-priority messages will not be deprioritized. This applies whether the notification is displayed by the FCM SDK via a?notification message, or a developer-generated notification via a?data message.

Measurements

https://firebase.google.com/docs/cloud-messaging/android/message-priority?hl=en&authuser=0#measuring_message_deprioritization_on_android

Measuring message deprioritization on Android

  • Individual Messages.?On delivery, you can determine whether an individual message was deprioritized or not by comparing its delivered priority, from?getPriority(), with its original priority, from?getOriginalPriority()
  • All Messages.?The?FCM Aggregate Delivery Data API?can report what percentage of all your messages to Android are being deprioritized. Some messages may be omitted from the aggregate data reports, but overall they should give a global view of message deprioritization rates. See our article on?aggregated delivery data?for more information and sample code for querying the API; it can also be explored from the?API explorer.

Options For Sending Message

Multicast

// Create a list containing up to 500 registration tokens.

// These registration tokens come from the client FCM SDKs.

const registrationTokens = [

'YOUR_REGISTRATION_TOKEN_1',

// …

'YOUR_REGISTRATION_TOKEN_N',

];

const message = {

data: {score: '850', time: '2:45'},

tokens: registrationTokens,

};

getMessaging().sendMulticast(message)

.then((response) => {

console.log(response.successCount + ' messages were sent successfully');

});
        

Single Device

// This registration token comes from the client FCM SDKs.

const registrationToken = 'YOUR_REGISTRATION_TOKEN';

const message = {

data: {

score: '850',

time: '2:45'

},

token: registrationToken

};

// Send a message to the device corresponding to the provided

// registration token.

getMessaging().send(message)

.then((response) => {

// Response is a message ID string.

console.log('Successfully sent message:', response);

})

.catch((error) => {

console.log('Error sending message:', error);

});

        

Topic


// The topic name can be optionally prefixed with "/topics/".

const topic = 'highScores';

const message = {

data: {

score: '850',

time: '2:45'

},

topic: topic

};

// Send a message to devices subscribed to the provided topic.

getMessaging().send(message)

.then((response) => {

// Response is a message ID string.

console.log('Successfully sent message:', response);

})

.catch((error) => {

console.log('Error sending message:', error);

});
        

Ternary Operations on Topic

?? "'TopicA' in topics && ('TopicB' in topics || 'TopicC' in topics)"

Firebase Topics

These are pub.sub based channel which can be subscribed and unsubscribed by user id and can be used to broadcast messages

Example

Firebase.getMessaging().subscribeTopic([User1,User2],”TG1”).then(response => {

if(response){

console.log(response) \\\\Topic is Successfully Subscribed

}

})
        

Above Snippet will subscribe users in TG1

Now for reusing same TG1 for different messaging nothing is required on server end as topics are maintained at firebase end

Simple add topic in variable with message payload


	   {
            ? notification: {
            ? ? title: 'DN Noti',
            ? ? body: 'DN noti Body'
            ? },
            ? Topic: “TG1”
            };
        

Ternary Operations on Topics

// Define a condition which will send to devices which are subscribed

// to either the TG1 or TG2 topics.

const condition = '\\'TG1\\' in topics || \\'TG2\\' in topics';

// See documentation on defining a message payload.

const message = {

notification: {

title: 'DN noti',

body: 'DN noti body'

},

condition: condition

};

// Send a message to devices subscribed to the combination of topics

// specified by the provided condition.

getMessaging().send(message)

.then((response) => {

// Response is a message ID string.

console.log('Successfully sent message:', response);

})

.catch((error) => {

console.log('Error sending message:', error);

});
        

Now OR XOR AND etc can be used to send messages or notification

to mix of TGs without considering distinct check? and Just By Keeping Changes in TG in a topic? we can easily remove

those from TG so that we don’t have to parse the whole data again

要查看或添加评论,请登录

Umang Sharma的更多文章

社区洞察

其他会员也浏览了