4

I am writing a reminder app in iOS 10; my local notifications are send using the UserNotifications framework. Sending the notification works fine already; my problem is rather the background handling of the notification. Earlier days, you could use didReceiveRemoteNotification in the app delegate for handling stuff like userInfo; now, UserNotification has apparently it's own methods.

I want to detect, generally, if in my absence a notification has arrived. Case: I received it, I tap open the app icon, bam: alert controller that says: you've received a notification. I am using these both functions for it:

func userNotificationCenter(_ center: UNUserNotificationCenter,  willPresent notification: UNNotification, withCompletionHandler   completionHandler: @escaping (_ options:   UNNotificationPresentationOptions) -> Void) {
    print("Handle push from foreground")

    UserDefaults.standard.set(true, forKey: "didReceiveRemoteNotification")
    UserDefaults.standard.synchronize()

    print("\(notification.request.content.userInfo)")
}

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    print("Handle push from background or closed")

    UserDefaults.standard.set(true, forKey: "didReceiveRemoteNotification")

    print("\(response.notification.request.content.userInfo)")
}

But they ONLY work, if I access the app by tapping on the notification in Notification Center. So, how do I detect if I've received a notification in the scenario that I don't enter the app through the notification, but through the app itself?

Carl Henretti
  • 457
  • 2
  • 7
  • 17
  • You need an API in order to get the all received Notification list like facebook. Because Apple doesn't allow you to check whether you have received notification or not if you have open the app by tapping on your app icon not on the notification. – Syed Qamar Abbas May 28 '17 at 15:48
  • are you sure? and what would that look like? I honestly don't know what an API is... – Carl Henretti May 28 '17 at 15:51
  • Your Server Guy will store all the notifications in a separate table and when you will open the app you have to make an HTTP request to server that will give you all notification sent to this device. Thats how you can get the notification list. – Syed Qamar Abbas May 28 '17 at 15:54
  • yeah no but that can't be the only solution; there is NO reason why apple would not make it possible to detect if the app has send a push notification. I mean, what would be the readon, they DID allow it to detect it if you're accessing the app through the notification... – Carl Henretti May 28 '17 at 15:56
  • didReceiveRemoteNotification only available for the remote notifications – Mannopson May 28 '17 at 22:04
  • you could work with silent remote notifications. Please see https://stackoverflow.com/a/36695473/1113632 – christopher.online May 31 '17 at 09:26
  • see [here](https://stackoverflow.com/questions/44425744/alternative-to-usernotificationcenterdelegates-willpresent-when-app-is-in-backg/44705892#44705892) – mfaani Jun 22 '17 at 17:31

0 Answers0