When I send a push notification, and the app is in the foreground willPresent
is called. didReceive
is never called. When the application is in the background, and a push notification is received, the alert is shown, but the application never calls didReceive
, or willPresent
.
In Project > Capabilities, I have Background Modes Location updates
, and Remote notifications
checked. Location updates is for unrelated code.
I have also enabled push notifications. This is working fine, as they are being received.
I have implemented the UNUserNotificationCenter
notification stuff in my AppDelegate
, see below:
import UserNotifications
...
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
...
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
...
registerForPushNotifications(application: application)
...
}
// MARK: - Push Notifications
func registerForPushNotifications(application: UIApplication) {
let notificationCenter = UNUserNotificationCenter.current()
notificationCenter.delegate = self
notificationCenter.requestAuthorization(options: [.badge, .sound, .alert], completionHandler: {(granted, error) in
if (granted)
{
UIApplication.shared.registerForRemoteNotifications()
}
else{
//Do stuff if unsuccessful...
print("Unsuccessful in registering for push notifications")
}
})
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
//convert the deviceToken to a string
let deviceTokenString = deviceToken.map { String(format: "%02.2hhx", arguments: [$0]) }.joined()
let ud = UserDefaults.standard
ud.set(deviceTokenString, forKey: "deviceToken")
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
//Handle the notification
print("User Info = ",notification.request.content.userInfo)
completionHandler([.alert, .badge, .sound])
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
//handle the notification
print("Push notification: ",response.notification.request.content.userInfo)
... code to import the notification data into Core Data.
completionHandler()
}
...
}
Any ideas why didReceive
is not being called?
This is the received push notification:
[AnyHashable("aps"): {
alert = "[name] has added you as a friend.";
category = "NEWS_CATEGORY";
"related_user_id" = 55;
sound = default;
type = "friend_request";
}]
iOS 10.1, Swift 3.
I found that I need to set the delegate after I get the permission granted.
Otherwise when you lunch the app first time ever you register the delegate before you get the permissions and the didReceive function is not triggered - this happened only on the first time you run app, after kill and relaunch it was called just fine.
See below:
UNUserNotificationCenter.current().requestAuthorization(options: authOptions, completionHandler: { (granted, error) in
if granted {
// Register after we get the permissions.
UNUserNotificationCenter.current().delegate = self
}
})
didReceive is not executed when you receive a notification, it's executed when the user performs an action on the received notification
From the Apple documentation at https://developer.apple.com/documentation/usernotifications/unusernotificationcenterdelegate
Called to let your app know which action was selected by the user for a given notification.
Meaning this method will be called when the user performs an action on the received notification (push the notification, swipe down, etc)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With