andrepaulo
andrepaulo

Reputation: 816

APNS Silent Remote Notification not triggering - SWIFT 3

Scenarios:

1) Silent or Normal payload when App's in Foreground:

2) Silent payload when App's in Background:

3) Normal payload when App's in Background:

  1. If User click the notification to open the App.

    • triggers the application:didReceiveRemoteNotification:fetchCompletionHandler
  2. If user open the App clicking the App icon:

    • Nothing happens

These are the payloads I'm using for the APNs:

Normal payload: .

{  
  "aps":{  
    "alert":"andre test",
    "badge":0,
    "sound":"default",
    "content-available":1
  },
  "acme-syncalarm":"true"
} 

Silent payload: .

{  
  "aps":{  
    "content-available":1
  },
  "acme-syncalarm":"true"
}

I've implemented the Remote Push Notification using this code:

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {

    print("Receeeeeeeeived: \(userInfo)")
    UIApplication.shared.applicationIconBadgeNumber = 11

    completionHandler(.newData)
}

I also implemented this to check if the App is recovering from a kill state (as I've read in some Questions too), but the code never enters the print(rn)line.

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

        if let rn = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] {
            print(rn)
        }
 }

EDIT 1 - I have also enabled Remote Notifications in background mode for the App.

What do I have to do to cover the "Nothing Happens" Scenarios? 1 , 2 and 3.2 ?

Upvotes: 0

Views: 1933

Answers (1)

mfaani
mfaani

Reputation: 36277

Some notes:

  • "If user open the App clicking the App icon: Nothing happens" <-- That's expected, because you didn't interact with any notification directly. Imagine if you had 5 notifications arrived. How would you know which notification you should process...

  • normal payload won't have any key named content-available. So that again is a silent notification. Can you first see my answer here?

Some suggestions:

  • Make sure you've enabled Remote Notifications in background mode. Like this:

enter image description here

  • Additionally See here. iOS 11 initial releases were buggy for silent notifications. Make sure you have the latest version for your testing, otherwise it won't work. If you have an iOS 10 device, then first try testing with that...

  • Make sure you have Background App refresh and notifications available on your device. To see how to do it, refer to my linked answer.

  • Are you creating the payload yourself or you're using FireBase? If you're using Firebase then some of the keys change...and you must adjust accordingly.

  • make sure you've set some object as the delegate of UNUserNotificationCenterDelegate e.g.:

UNUserNotificationCenter.current().delegate = delegateObject

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    let content = notification.request.content
    // Process notification content

    completionHandler([.alert, .sound, .badge]) // Display notification as regular alert and play sound
}

Code copied from here.

If you don't do such then you won't be showing any notification when the app is in the foreground. This should resolve the issue of when app is in foreground and you've received a normal remote notification.

Upvotes: 1

Related Questions