3

We have some apps where we need to check push notifications in regular interval. Can we check push notifications via Xcode UI test?

Confused
  • 3,846
  • 7
  • 45
  • 72

1 Answers1

-3

Disclaimer: I haven't done this...

You can't generate a token...so I guess the answer would be No! What can do is a mock your notificationManager with stub data.

With the stub data you test:

  1. Does your callback function download what want it to?
  2. Or maybe assuming you have alerts when app is in foreground: are you showing an alert?

Otherwise if you're purely up to testing to see if the app throws the callback when it receives a notification then you're testing the OS and that's wrong. You should only test your own code.

By testing your notificationManager I roughly mean something like:

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

        NotificationManager.shared.handle(userInfo)
}

Your NotificationManager class

func handleAPNSMessage(_ message:[AnyHashable: Any]) {

if let aps = message["aps"] as? NSDictionary {
    if let alert = aps["alert"] as? NSDictionary {
        if let message = alert["message"] as? NSString {
           //Do stuff
        }
    } else if let alert = aps["alert"] as? NSString {
        //Do stuff
        }
    }
}

Derived from here

Now what you do is, you mock calling handleAPNSMessage with a JSON object that looks same as the one your server would send you,

If you want to go crazy and really duplicate the remote notifications then you could use your own stub data and create local notifications... and see if the app works as you expect...

EDIT:

Make sure you see How can I test Apple Push Notification Service without an iPhone?

mfaani
  • 33,269
  • 19
  • 164
  • 293