Я боролся с этой проблемой в течение прошлой недели и не смог найти исправление. /> Проблема: < /h3>
Когда я отправляю уведомления FCM из моего бэкэнда, они получены на устройстве, а постукивание их открывает приложение. Триггер. Слушатели событий правильно зарегистрированы. < /li>
Проверяли, сохраняется ли вопрос как на переднем плане, так и на фоновом плане. Любая помощь ценится! < /P>
public static async Task SendNotificationToFCM(string fcmToken, string title, string message, string imageUrl, string amazonAffiliateUrl)
{
try
{
Dictionary data = new Dictionary
{
{ "Why mobile development is hard?", "hello world!" },
};
var fcmMessage = new Message()
{
Notification = new Notification()
{
Title = title,
Body = message,
ImageUrl = imageUrl,
},
Data = data,
Token = fcmToken,
Android = new AndroidConfig()
{
Notification = new AndroidNotification()
{
Title = title,
Body = message,
ImageUrl = imageUrl,
},
// Data = data,
Priority = Priority.High // Set high priority
}
};
string serializedMsg = JsonConvert.SerializeObject(fcmMessage);
if (FirebaseApp.DefaultInstance == null)
{
FirebaseApp.Create(new AppOptions()
{
Credential = GoogleCredential.FromFile(firebase-config.json")
});
}
// Send a message to the device corresponding to the provided
// registration token.
string response = await FirebaseMessaging.DefaultInstance.SendAsync(fcmMessage);
// Response is a message ID string.
Console.WriteLine("Successfully sent message: " + response);
return true;
}
catch (Exception ex)
{
return false;
}
}
< /code>
Теперь нативный код React < /p>
useEffect(() => {
const handleNotification = async (remoteMessage) => {
console.log("
};
console.log("
//
messaging()
.getInitialNotification()
.then((remoteMessage) => {
if (remoteMessage) {
console.log("
handleNotification(remoteMessage);
} else {
console.log("
}
})
.catch((error) => console.error("
//
const unsubscribe = messaging().onNotificationOpenedApp((remoteMessage) => {
console.log("
handleNotification(remoteMessage);
});
//
const foregroundUnsubscribe = messaging().onMessage((remoteMessage) => {
console.log("
handleNotification(remoteMessage);
});
return () => {
console.log("
unsubscribe();
foregroundUnsubscribe();
};
}, []);
Подробнее здесь: https://stackoverflow.com/questions/794 ... n-sent-via