3

I want to UI test my app with push notifications and maybe some of you have found a best practice. There are a lot tutorials and questions but I wasn't able to find a satisfying answer.

  1. Start test
  2. Send push notifcation
  3. Tap push notifcation
  4. Do stuff

But what would be the best practice to send the push notification?

  • Rest Call that triggers the push notification
  • Get access to the terminal (but how?) and run: xcrun simctl push <device> com.example.my-app ExamplePush.apns
  • Send local push notifications?
  • Mock Server
  • use a Framework like Mussel

Thanks! :)

Godlike
  • 1,621
  • 2
  • 20
  • 33

2 Answers2

2

Use terminal to test push notification with single line command

Install Houston in your Mac, run below command in terminal.

  1. gem install houston

    If you are facing error like this,

    Fetching houston-2.4.0.gem ERROR: While executing gem ... (Gem::FilePermissionError) You don't have write permissions for the /Library/Ruby/Gems/2.6.0 directory.

    First run below commands in terminal to install Ruby

    brew install ruby

    export GEM_HOME="$HOME/.gem"

    gem install rails

    after successful installation run again

    gem install houston

  2. Go to the pem files folder and open terminal from that folder.

  3. Run below command like

    apn push "Device Token" -c PEM_FILE_NAME -m "MESSAGE"

    Like:

    apn push "5a4b74d5e5fc325b14d2f2641aa11bfb9744d1f88922822a5ed3512376d5f5b9" -c myapp_apns_dev.pem -m "Testing"

after successful run of above command it will ask for PEM pass phrase which is password of your pem file.

If your app is lived then use production pem file name

like this,

apn push "5a4b74d5e5fc325b14d2f2641aa11bfb9744d1f88922822a5ed3512376d5f5b9" -c myapp_apns_pro.pem -m "Testing"

Done.

For UI Testing you can use Local notification,

You have to set categoryIdentifier for local notification also make sure that same categoryIdentifier set into UNNotificationExtensionCategory in your AppExtension Info.plist file

For more in detail kindly refer below link

https://levelup.gitconnected.com/custom-push-notification-in-ios-swift-5-210552643e86#:~:text=Go%20to%20xcode%20select%20File,have%20its%20unique%20category%20Identifier.

Below is the sample code to fire local notification with Category Identifier

let content = UNMutableNotificationContent()
content.title = title
content.body = body
content.categoryIdentifier = "LOCAL_NOTIFICATION"
    
if let info = userInfo {
       let dic = ["body" : "Custom Data"]
       content.userInfo = dic as [AnyHashable : Any]
 }
    
content.sound = UNNotificationSound.init(named: UNNotificationSoundName(rawValue: sound))
            
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false)
            
let request = UNNotificationRequest(identifier: "LOCAL_NOTIFICATION", content: content, trigger: trigger)
    
let notificationCenter = UNUserNotificationCenter.current()
     notificationCenter.add(request) { (error) in
          if let error = error {
                print("Error \(error.localizedDescription)")
           }
 }
Dhruvin Thumar
  • 190
  • 2
  • 10
  • But I can't trigger the Push Notification from a UI Test with this approach, do I? – Godlike Feb 24 '21 at 13:03
  • 1
    I think above information is sufficient for Push notification custom UI testing. – Dhruvin Thumar Feb 24 '21 at 13:33
  • 1
    Thats actually a pretty good solution. Your code doesn't work in the UI Tests but you could put it in the app delegate and work with launchArguments. Thank you! I will post the code later :) – Godlike Feb 24 '21 at 15:15
1

For testing purpose if you want to create testing push notification then

https://github.com/noodlewerk/NWPusher

This library is very helpful.