On March 9 or so, I was receiving both push and silent notifications, but on March 22 or so, I stopped receiving tokens and silent notifications. When I created a new project and implemented FCM, I am still not receiving either push or silent notifications, although I can get the tokens.
Has anyone else experienced the same symptoms and solved the problem?
version iPad 15.4.1 Xamarin.Firebase.iOS.CloudMessaging 8.10.0
The library in 4.7.1 was receiving push notifications but not silent notifications; after updating to 8.10.0, both push and silent notifications are no longer being received.
public override void ReceivedRemoteNotification(UIApplication application, NSDictionary userInfo)
{
Console.WriteLine(userInfo);
}
public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler)
{
Console.WriteLine(userInfo);
}
public void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)
{
Console.WriteLine(userInfo);
}
Any advice is welcome. Best regards.
CodePudding user response:
The Javascript registered with Functions was bad.
Bad code:
const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp(functions.config().firebase);
exports.BucketChanged=functions.region("asia-northeast1").storage.object()
.onFinalize(async (object)=>{
const topic = "TopicName";
const message = {
topic: topic,
data: {
body: object.name,
contentAvailable: "true",
},
};
admin.messaging().send(message)
.then((response) => {
console.log("Successfully sent message:", response);
})
.catch((error) => {
console.log("Error sending message", error);
});
}
);
Good code:
"use strict";
const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp(functions.config().firebase);
exports.sendSilentNotificationWithTopic = functions.region("asia-northeast1")
.region("asia-northeast1").storage.object().onFinalize(async (object) => {
const topic = "TopicName";
const payload = {
data: {
imageName: object.name,
},
};
const options = {
contentAvailable: true,
};
return admin.messaging().sendToTopic(topic, payload, options)
.then(function(response) {
return console.log("Successfully sent message:", response);
})
.catch(function(error) {
return console.log("Error sending message:", error);
});
});
Mainly the difference between send and sendToTopic, but can anyone explain why this is happening? At least I'm getting it right for now....
CodePudding user response:
See this docs,
send(message, dryRun) means Sends the given message via FCM
.
sendToTopic(topic, payload, options) means Sends an FCM message to a topic
.See Send to a topic for code samples and detailed documentation.