0

So when my app is not currently open and it receives a push notification, the "didrecieveremotenotification" works perfectly fine when the banner appears and you press it. But when the app is open, no banner at all appears. I want the push notification to work when the app is open aswell.

I tried using UNNotificationCenter but lost push notifications in general.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    UNUserNotificationCenter.current().delegate = self
    UNUserNotificationCenter.current().requestAuthorization(options: [.badge, .sound, .alert], completionHandler: {(granted, error) in
        if (granted) {
            UIApplication.shared.registerForRemoteNotifications()
        } else{
            print("Notification permissions not granted")
        }
    })
 }


//Called when a notification is delivered to a foreground app.
public func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Swift.Void) {
    completionHandler([.sound, .alert, .badge])
}

//Called to let your app know which action was selected by the user for a given notification.
public func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Swift.Void) {
    completionHandler()
}

Update: UNUserNotificationCenter.current().requestAuthorization runs

func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error)

when permission is granted.

however when I don't use UNNotificationCenter and use old method it doesn't fail.

Why would one method fail to register for remote notifications but the other not?

Update 2: Was working in simulator lol, that's why it was failing. However now when I run the UNUserNotificationCenterDelegate methods only the

public func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive method works and not the

public func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent

Update 3: I've ran out of possible things to try. What would be stopping the code from firing willPresent but not didReceive. This is brutal. Has anyone ever had this issue before?

Update 4: SOLVED. The service I'm using for the push notifications requires the user to be disconnected from the service to send the push notification. It automatically disconnects you when you're in the background, which is why that was working and it wasn't working in the foreground.

Weird way for them to configure that, I'll probably send an email.

4
  • Everything looks okay.. try deleting the app. Commented Jul 17, 2017 at 3:41
  • tried that already Commented Jul 17, 2017 at 3:46
  • can you provide the error from didFailToRegisterForRemoteNotificationsWithError ? Commented Jul 17, 2017 at 3:53
  • ok sorry, the error was for when I was running it in the simulator. Now it runs "didReceive" but not "willpresent" same issue as the start Commented Jul 17, 2017 at 4:07

1 Answer 1

1

Implement UNUserNotificationCenterDelegate delegate methods to get notification (tray at top) while app is in foreground. But it will only work with IOS 10.

In your didFinishLaunchingWithOptions method set UNUserNotificationCenterDelegate delegate like this.

UNUserNotificationCenter.current().delegate = self

Implement Delegate methods...

//Called when a notification is delivered to a foreground app.
public func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Swift.Void) {
    completionHandler([.sound, .alert, .badge])
}

//Called to let your app know which action was selected by the user for a given notification.
public func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Swift.Void) {
    completionHandler()
}
Sign up to request clarification or add additional context in comments.

2 Comments

I tried using that exact code before asking this question and my push notifications just stopped working completely.
Hi @NickPali, reviving an old thread... But facing the exact problem now. I have background notifications working, but any reason why the didReceive foreground notification method never calls?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.