Firebase Cloud Messaging成功发送sendToDevice,但没有通知

时间:2018-11-14 18:03:27

标签: node.js swift firebase apple-push-notifications firebase-cloud-messaging

我正在尝试编写一个云功能,该功能将推送通知发送到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;
});

调用上面的cloud函数时,日志显示此控制台日志(标识号被截断):

"Successfully sent message:  {"results":[{"messageId":"0:154/* ... */"}],"canonicalRegistrationTokenCount":0,"failureCount":0,"successCount":1,"multicastId":576/* ... */}"

但是我的测试设备(iPhone 7)尚未收到任何通知。我的应用程序具有以下委托函数(快捷键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证书显然仍然有效,并且尚未过期。

1 个答案:

答案 0 :(得分:0)

问题仅仅是我的podfile丢失了:

pod 'Firebase/Messaging'

这使我的didReceiveRemoteNotification从Firebase云功能接收通知有效负载。然后,一旦我将UNUserNotificationCenter.current().delegate = self添加到AppDelegate中,UNUserNotificationCenterDelegate函数就会按预期工作。

奇怪的是,缺少的Pod如何没有给我任何编译器错误。

相关问题