FCM handbook
Umang Sharma
Building Against the Odds - RISE with SAP automation | X-Doubtnut/Allen | Product, Back-End Dev, SEO Technical Expert-| SAP
Firebase Messaging
Options for interacting with FCM servers include the following:
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:
The Admin Node.js SDK provides methods for sending messages to device groups.
Topic is for broadcast events which are not time critical .
Some things to keep in mind about topics:
Stop Abusing Priority
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
Measuring message deprioritization on Android
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