XCode Version: 13.2.1 iOS Version: 15.3.1
I have an apple server setup for sending push notifications, that sends to both Production and Development environments.
It is using certificate-based push, the type of certificate selected is this:
Other than that the code is successfully registering for remote notification returning a unique device token.
Here is the code for registering for notification.
let center = UNUserNotificationCenter.current()
center.delegate = self as UNUserNotificationCenterDelegate
center.requestAuthorization(options: [.alert, .badge, .sound]) { granted, error in
print("Granted \(granted)")
print("Granted \(granted.description)")
if error != nil {
print("Error in permission for notificaiton")
}
if(granted){
print("Permission is granted")
DispatchQueue.main.async {
let settings = UIUserNotificationSettings(types: [], categories: .none)
UIApplication.shared.registerUserNotificationSettings(settings)
UIApplication.shared.registerForRemoteNotifications()
}
} else {
self.deviceToken = "PermissionDenied"
}
}
Here is the registration callback
override func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken tokenData: Data) {
print("This is unnotification token \(tokenData.hexString)")
deviceToken = tokenData.hexString
}
override func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (_ options: UNNotificationPresentationOptions) -> Void) {
print("Handle push from foreground")
// custom code to handle push while app is in the foreground
print("\(notification.request.content.userInfo)")
completionHandler(.alert)
}
override func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
print("Handle push from background or closed")
// if you set a member variable in didReceiveRemoteNotification, you will know if this is from closed or background
print("\(response.notification.request.content.userInfo)")
completionHandler()
}
override func application(_ application: UIApplication,
didReceiveRemoteNotification userInfo: [AnyHashable : Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
print("This is push notification data \(userInfo)")
// Inform the system after the background operation is completed.
completionHandler(.newData)
}
Here is the server response for sending the notification to the device token provided to it.
None of the callback is receiving the notification payload.
Here is the notification payload:
{
"aps" : {
"alert" : {
"title" : "Game Request",
"body" : "Bob wants to play poker",
},
},
}