Ad

Firebase Cloud Messaging Successful SendToDevice But No Notification

I'm trying to write a cloud function which sends a push notification to an iOS device. The logs say that sendToDevice was successful. But my device isn't receiving any notifications. Neither Xcode nor Cloud Functions are showing any errors. How can I diagnose this problem?

My cloud function takes a registration token from the realtime database. This token is saved to the database during the didRegisterForRemoteNotificationsWithDeviceToken function in the ios app, confirming that the front end is registering for remote notifications. The app has been given permission to show notifications and the push notification capabilities have been enabled in Xcode.

This block of code comes from my cloud function (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;
});

When calling the cloud function above, the logs showed this console log (Id numbers truncated):

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

But my test device (iPhone 7) hasn't received any notifications. My app has the following delegate functions (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)")
}

Neither print statement is appearing in Xcode's output. The only relevant print statements found are the ones I included in didRegisterForRemoteNotificationsWithDeviceToken. My APNs certificate is apparently still valid and has not expired.

Ad

Answer

The issue was simply that my podfile was missing:

pod 'Firebase/Messaging'

That allowed my didReceiveRemoteNotification to receive the notification payloads from Firebase cloud function. Then once I added UNUserNotificationCenter.current().delegate = self to the AppDelegate, the UNUserNotificationCenterDelegate functions worked as intended.

Strange how the missing pod didn't give me any compiler errors.

Ad
source: stackoverflow.com
Ad