Issue
Hi so I've implemented a method based on this answer: How to get notification in xamarin forms on firebase data change? But I'm having trouble getting it working.
Here's my code:
void DoFirebaseObserve()
{
var firebase = new FirebaseClient(Constants.FirebaseProjectUrl);
firebase.Child("RegisterUserTable").AsObservable<RegisterUser>().Subscribe(obs =>
{
switch (obs.EventType)
{
case Firebase.Database.Streaming.FirebaseEventType.InsertOrUpdate:
Console.WriteLine("InsertOrUpdate");
break;
case Firebase.Database.Streaming.FirebaseEventType.Delete:
Console.WriteLine("Delete");
break;
default:
break;
}
});
}
I call this code in the OnAppearing method of my app's home page. I've tried with and without the while (true) {} condition.
When I add, update or delete a record in firebase, nothing happens in my application. The writelines are never called.
Any help would be appreciated. Thanks.
Solution
So I worked out how to do it through using Google Cloud Functions in the Firebase console.
Step 1. Set up FCM in your .Net code
Follow this tutorial: https://docs.microsoft.com/en-us/xamarin/android/data-cloud/google-messaging/remote-notifications-with-fcm?tabs=macos
Once you have got your FCM token from that device, you can add it to your realtime database (I did this myself in the firebase console manually for testing).
Step 2. Set up Cloud Functions
Follow this tutorial: https://www.youtube.com/watch?v=bpI3Bbhlcas&t=1104s&ab_channel=uNicoDev
Step 3. Create a function to send the message
You can use this node.js code in your cloud functions to send up to 500 devices:
// 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,
};
admin.messaging().sendMulticast(message)
.then((response) => {
console.log(response.successCount + ' messages were sent successfully');
});
Source: https://firebase.google.com/docs/cloud-messaging/send-message#node.js_1
Code Example
Some of my code to help you get an idea of how to go about doing something like this. Can't guarantee this is the optimal method but it works.
const reference = "JourneyTbl/{journeyId}";
// newJourneyAdded | listener method | Send push notif when new journey created:
exports.newJourneyAdded = functions.database.ref(reference)
.onCreate((event, context) => {
// where fcm tokens are stored:
const refNotifications = "NotificationTbl";
// get ref to RegNotTable:
const refFcm = admin.database().ref(refNotifications);
// array to hold all fcms from table:
const allFcms = [];
// get value of "NotificationTbl":
refFcm.on("value", (snapshot) => {
// check if there's any children:
if (snapshot.hasChildren()) {
// loop through children:
snapshot.forEach((element) => {
// stringify the data:
const asString = JSON.stringify(element);
// parse that as a JSON object:
const asJson = JSON.parse(asString);
// add fcm token to array:
allFcms.push(asJson.fcm);
});
}
// if array contains something:
if (allFcms.length > 0) {
// construct message to send:
const msg = {
notification: {
title: "Notification Title goes here",
body: "Notification Body goes here ",
},
tokens: allFcms, // pass the tokens
};
// send that message:
admin.messaging().sendMulticast(msg)
.then((response) => {
console.log(response.successCount + " mgs were sent sfly");
});
} else {
console.log("No devices in FCM tokens list");
}
});
return "ok";
});
Answered By - PJ.
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.