Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS best practice to test push notifcations (UI Test)

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! :)

like image 589
Godlike Avatar asked Nov 23 '25 11:11

Godlike


2 Answers

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)")
           }
 }
like image 169
Dhruvin Thumar Avatar answered Nov 26 '25 00:11

Dhruvin Thumar


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

https://github.com/noodlewerk/NWPusher

This library is very helpful.

like image 32
Dhaval Patoliya Avatar answered Nov 26 '25 00:11

Dhaval Patoliya



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!