Firebase 云消息传递成功 sendToDevice 但没有通知
Firebase Cloud Messaging successful sendToDevice but no notification
我正在尝试编写一个向 iOS 设备发送推送通知的云函数。日志显示 sendToDevice 成功。但是我的设备没有收到任何通知。 Xcode 和 Cloud Functions 都没有显示任何错误。我该如何诊断这个问题?
我的云函数从实时数据库中获取注册令牌。此令牌在 ios 应用程序中的 didRegisterForRemoteNotificationsWithDeviceToken
函数期间保存到数据库,确认前端正在注册远程通知。该应用程序已获得显示通知的权限,并且已在 Xcode.
中启用推送通知功能
这段代码来自我的云函数 (Node.js):
// This snapshot was taken from the realtime database
// Xcode logs confirmed that this function is receiving the correct key
const notificationKey = userSnapshot.child("notificationKey").val();
const payload = {
notification: {
title: 'Test Notification Title',
body: 'Test Notification Body',
sound: 'default',
badge: '1'
}
};
return admin.messaging().sendToDevice(notificationKey, payload).then(function (response) {
console.log("Successfully sent message: ", JSON.stringify(response));
return;
}).catch(function (error) {
console.log("Error sending message: ", error);
return;
});
调用上面的云函数时,日志显示此控制台日志(截断 ID 号):
"Successfully sent message: {"results":[{"messageId":"0:154/* ... */"}],"canonicalRegistrationTokenCount":0,"failureCount":0,"successCount":1,"multicastId":576/* ... */}"
但是我的测试设备 (iPhone 7) 没有收到任何通知。我的应用程序具有以下委托函数 (Swift 4):
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
print("Notification will present: \(notification.request.content.userInfo)")
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
print("Notification received: \(response.notification.request.content.userInfo)")
}
Xcode 的输出中均未出现打印语句。找到的唯一相关打印语句是我在 didRegisterForRemoteNotificationsWithDeviceToken
中包含的语句。我的 APNs 证书显然仍然有效并且没有过期。
问题只是我的播客文件丢失了:
pod 'Firebase/Messaging'
这让我的 didReceiveRemoteNotification
能够从 Firebase 云函数接收通知负载。然后,一旦我将 UNUserNotificationCenter.current().delegate = self
添加到 AppDelegate,UNUserNotificationCenterDelegate
函数就会按预期工作。
奇怪的是丢失的 pod 没有给我任何编译器错误。
我正在尝试编写一个向 iOS 设备发送推送通知的云函数。日志显示 sendToDevice 成功。但是我的设备没有收到任何通知。 Xcode 和 Cloud Functions 都没有显示任何错误。我该如何诊断这个问题?
我的云函数从实时数据库中获取注册令牌。此令牌在 ios 应用程序中的 didRegisterForRemoteNotificationsWithDeviceToken
函数期间保存到数据库,确认前端正在注册远程通知。该应用程序已获得显示通知的权限,并且已在 Xcode.
中启用推送通知功能
这段代码来自我的云函数 (Node.js):
// This snapshot was taken from the realtime database
// Xcode logs confirmed that this function is receiving the correct key
const notificationKey = userSnapshot.child("notificationKey").val();
const payload = {
notification: {
title: 'Test Notification Title',
body: 'Test Notification Body',
sound: 'default',
badge: '1'
}
};
return admin.messaging().sendToDevice(notificationKey, payload).then(function (response) {
console.log("Successfully sent message: ", JSON.stringify(response));
return;
}).catch(function (error) {
console.log("Error sending message: ", error);
return;
});
调用上面的云函数时,日志显示此控制台日志(截断 ID 号):
"Successfully sent message: {"results":[{"messageId":"0:154/* ... */"}],"canonicalRegistrationTokenCount":0,"failureCount":0,"successCount":1,"multicastId":576/* ... */}"
但是我的测试设备 (iPhone 7) 没有收到任何通知。我的应用程序具有以下委托函数 (Swift 4):
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
print("Notification will present: \(notification.request.content.userInfo)")
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
print("Notification received: \(response.notification.request.content.userInfo)")
}
Xcode 的输出中均未出现打印语句。找到的唯一相关打印语句是我在 didRegisterForRemoteNotificationsWithDeviceToken
中包含的语句。我的 APNs 证书显然仍然有效并且没有过期。
问题只是我的播客文件丢失了:
pod 'Firebase/Messaging'
这让我的 didReceiveRemoteNotification
能够从 Firebase 云函数接收通知负载。然后,一旦我将 UNUserNotificationCenter.current().delegate = self
添加到 AppDelegate,UNUserNotificationCenterDelegate
函数就会按预期工作。
奇怪的是丢失的 pod 没有给我任何编译器错误。