未收到通过iOS应用中的Firebase Cloud函数发送的推送通知,尽管我从消息传递控制台获得了
我以正确的方式设置了项目设置(我相信)IE:
- apns身份验证密钥在Firebase控制台的云消息传递部分中添加。
- 删除通知应用程序委托函数:
应用程序委托函数:
extension AppDelegate: UNUserNotificationCenterDelegate {
func registerForPushNotifications() {
if !Device.isSimulator {
UNUserNotificationCenter.current().delegate = self
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(options: authOptions,
completionHandler: {_, _ in
dispatchOnMainThread {
Messaging.messaging().delegate = self
UIApplication.shared.registerForRemoteNotifications()
}
})
}
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
print("
I have my project setup in the correct way (I believe) i.e:
- The APNs Authentication Key is added in the cloud messaging section of the firebase console.
- The remove notification app delegate functions:
App Delegate functions:
extension AppDelegate: UNUserNotificationCenterDelegate {
func registerForPushNotifications() {
if !Device.isSimulator {
UNUserNotificationCenter.current().delegate = self
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(options: authOptions,
completionHandler: {_, _ in
dispatchOnMainThread {
Messaging.messaging().delegate = self
UIApplication.shared.registerForRemoteNotifications()
}
})
}
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
print("???? Failed to register for remote notifications with error: \(error)")
}
private func processNotification(_ notification: UNNotification) {
let userInfo = notification.request.content.userInfo
UIApplication.shared.applicationIconBadgeNumber = 0
print("???? Notification Content Received: \(userInfo)")
if let resourcePath = Bundle.main.path(forResource: "general_notification", ofType: "m4a") {
let url = URL(fileURLWithPath: resourcePath)
audioPlayer = try? AVAudioPlayer(contentsOf: url)
audioPlayer?.prepareToPlay()
audioPlayer?.play()
}
}
}
extension AppDelegate {
func application(_ application: UIApplication,
didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
print("???? Notification received in an state: \(application.applicationState)")
}
// MARK: Handles Silent Push Notifications
func application(_ application: UIApplication,
didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
print("???? Notification Content Received in Background State: \(userInfo)")
completionHandler(UIBackgroundFetchResult.newData)
}
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: () -> Void) {
processNotification(response.notification)
completionHandler()
}
// MARK: Receive displayed notifications for iOS 10 devices.
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
processNotification(notification)
completionHandler([.badge, .banner, .sound])
}
}
In my cloud functions this is what I have:
admin.initializeApp(functions.config().firebase);
function sendNotification(userID){
let notificationToken = db.collection('notification_tokens').doc(userID);
notificationToken.get().then(doc => {
if (!doc.exists) {
console.log(`⛔️ Cant find notification token for user ${userID}`);
} else {
const tokenData = doc.data();
const tokenID = Object.keys(tokenData)[0];
console.log(`???? Device Token Found with tokenID: ${tokenID}`);
const messagePayload = {
title: `${userName} just joined`,
body: `Your friend ${userName} is now on Your App`,
sound: 'general_notification.m4a',
badge: `1`
};
const notificationMessageObject = {
token: tokenID,
data: messagePayload
};
admin.messaging().send(notificationMessageObject).then((response) => {
console.log(`✅ Successfully sent message: ${response}`);
return true;
}).catch((error) => {
console.log('❌ Error sending message:', error);
return false;
});
}
}).catch(err => {
console.log('Error getting tokenID document', err);
return false;
});
}
When I test the push notification functionality by sending a push notification from the Messaging Panel on Firebase Console to my physical device token captured from the app delegate, I get the notification on my phone,
When I run a cloud function trigger to get there notification, I get a success response in the logs but the notification does not get received on my device.
It was working on a staging database I had but when I setup a new database and replicate the old one, the above occurs.
Might there be something I missed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最终找到了解决方案 - 位于node.js云函数中。
而不是这样做:
我必须使用:
最终的云功能看起来像这样:
希望它能帮助某人。
Eventually found the solution - which lay in the node.js cloud function.
Instead of doing this:
I had to use:
The final cloud function looks like this:
Hope it helps someone.