0

I recently set up notifications on my application, I can send via php and no worries, everything works fine.

Since last week, I am trying to find out if a push notification is received by the phone or not..

I can know when someone clicks on it but if the person does not click on it and prefer to open the application, it does not work..

Is there not a simple function that can tell me if a notification has just been received by the application ?

In my AppDelegate.swift :

import UIKit
import UserNotifications

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {

...

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

     // Check if launched from notification
     if let notification = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? [String: AnyObject] {
       //print(notification)
       window?.rootViewController?.present(ViewController(), animated: true, completion: nil)
   } 
   else{
       //print("ici ?")
       registerForRemoteNotification()
   }

return true
}

...

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

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

...

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {

        **print("here ?")**

        switch((application.applicationState)){

        case UIApplicationState.inactive:
            print("Inactive")
            //Show the view with the content of the push
            completionHandler(.newData)

        case UIApplicationState.background:
            print("Background")
            //Refresh the local model
            completionHandler(.newData)

        default:
            print("Active")
            //Show an in-app banner
            completionHandler(.newData)
            break
        }
    }

}

Problem : I never pass in didReceiveRemoteNotification :/ The "print here" is never displayed.

I have a mistake on this line :

Instance method 'application(application:didReceiveRemoteNotification:fetchCompletionHandler:)' nearly matches optional requirement 'application(_:didReceiveRemoteNotification:fetchCompletionHandler:)' of protocol 'UIApplicationDelegate'

But I do not understand :/

Do you have an idea ?

Thx for your help ^^

Insou
  • 1,303
  • 1
  • 13
  • 17
  • whatever you said in your third explanation , only we can check user tap the notification. when user swipe the notification cant check, This feature not available in apple document. – Pavankumar Dec 07 '16 at 12:52
  • it's not possible to check if my app receive a push ? O_o – Insou Dec 07 '16 at 12:58
  • http://stackoverflow.com/questions/25830597/how-to-know-push-notification-delivery-status , go with link and i studied also no guarantee to say delivered or not. In our app also we tried this one , that time i came to know not possible, only when user tap that one then only. – Pavankumar Dec 07 '16 at 13:03
  • I read the link but I have the opposite example : How does WhatsApp detect if a message is delivered without opening the app on iOS ? It should be possible :p – Insou Dec 07 '16 at 13:12

1 Answers1

2

It's rather common mistake since the conversion to Swift 3 - you're using Swift 2 method.

The right signature of this method in Swift 3 is:

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
    print("here?")
} 
Elena
  • 829
  • 8
  • 20
  • Oh it's just a bad copy/paste... in my code, it's the right signature, for Swift 3.. My bad ^^ But it's still doesn't work :/ – Insou Dec 07 '16 at 12:55
  • are you running it in iOS 10? – Elena Dec 07 '16 at 13:04
  • Do you physically get pushes for this app? With sound etc. – Elena Dec 07 '16 at 14:59
  • Yes, the pushes work fine but i don't know how to "catch" when it delivered on the phone :/ – Insou Dec 07 '16 at 16:22
  • What for do you need to "catch"? If user taps on push the app should know this either in `didFinishLaunchingWithOptions` if app was terminated or in `didReceiveRemoteNotification` if app was in background – Elena Dec 08 '16 at 06:35
  • When i say "catch", it's when the phone receive the push.. without action of user. Check my code, is it good for you ? Because it didn't work in didReceiveRemoteNotification and i don't know how fix it :/ (thx for your time ^^) – Insou Dec 08 '16 at 09:08
  • Have you implemented the methods: `func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {}` and `func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {}`? We should know for sure that if the app registered for remote notifications or not – Elena Dec 08 '16 at 09:13
  • Yes, the methods are implemented and the pushs work fine when i send via my php script.. I receive notifications. but i never pass in "didReceiveRemoteNotification" :/ – Insou Dec 08 '16 at 10:25
  • `didReceiveRemoteNotification` is called in two cases: 1. app is in background and user taps on push 2. app is active and 'silent push' is received – Elena Dec 08 '16 at 13:37