ทดสอบ Push Notifications บน iOS Simulator

Thanathip Kumnarai
te<h @TDG
Published in
3 min readMay 10, 2020

สวัสดีชาว iOS Developer ทุกท่าน คงจะเจ็บปวดไม่น้อยเลยสำหรับที่ผ่านมา พวกเราไม่สามารถทดสอบ Push Notification บน iOS Simulator ได้ ต้องหาเครื่องจริงมาทดสอบกัน จึงจะสามารถทดสอบส่วนของ Push Notification ได้

แต่ในวันนี้สิ่งที่พวกเรารอคอยก็มาถึง เมื่อ Xcode 11.4 ได้มี release note เกี่ยวกับ Simulator ดังนี้

Simulator supports simulating remote push notifications, including background content fetch notifications. In Simulator, drag and drop an APNs file onto the target simulator. The file must be a JSON file with a valid Apple Push Notification Service payload, including the “aps” key. It must also contain a top-level “Simulator Target Bundle” with a string value that matches the target application‘s bundle identifier.

ซึ่งจากข้อความด้านบนเรายังสามารถลากไฟล์ APNs ไปยัง iOS Simulator ได้อีกด้วย แล้วมันจะเป็นยังไงหล่ะ เรามาทำไปพร้อมๆกันเลยครับ

  1. ดาวน์โหลด Xcode 11.4 หรือใหม่กว่า และทำการติดตั้ง

ณ ตอนที่เขียนบทความนี้ Apple ได้ปล่อย Xcode 11.4.1 ตัว stable เป็นที่เรียบร้อยแล้ว สามารถเข้าไปโหลดได้ ที่นี่

ที่มา: https://developer.apple.com/news/releases/

2. สร้าง Project ใหม่ เพื่อสำหรับทดสอบ

โดยผู้เขียนได้สร้าง Project ง่ายๆชื่อว่า TestNotification จากนั้นทดสอบ Build and Run บน iPhone 11 จะได้หน้าตาดังนี้

3. สร้าง Grant Permission ใน AppDelegate

อันดับแรกไปที่ Appdelegate.swift แล้ว import UserNotifications framework

import UserNotifications

จากนั้นที่ Method didFinishLaunchingWithOptions ให้ทำการขอสิทธิ์การเข้าถึง Notification ดังนี้

UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) { (granted, error) in   print("permission is \(granted)")}

ทำการ Build and Run จะมีการขอสิทธิ์การเข้าถึง Notification ดังภาพด้านล่างนี้

4. ทำการสร้างไฟล์ Payload

การสร้างไฟล์ Payload จะต้องเป็น Format JSON และต้องถูก Format ของ Apple Push Notification Service ดังตัวอย่างทางด้านล่างนี้

{   "aps": {      "alert": {          "title": "Test Notification",          "body": "Hello",      },   "badge": 1   },  "Simulator Target Bundle": "com.thanathip.TestNotification"}

โดยตรง key “Simulator Target Bundle” จะต้องเป็น Bundle Identifier ของ Project ที่ถูกสร้างดังภาพทางด้านล่าง

จากนั้นทำการ save เป็นสกุล .apns ไว้ที่ Desktop โดยผมจะ save ในชื่อ TestApnsFile.apns

5. ทดสอบ Push Notification

หลังจากที่เราทำการ Save APNs file เป็นที่เรียบร้อยแล้วเราก็จะมาทำการทดสอบกัน โดยก่อนอื่น ให้กลับมาที่หน้า Home ของ iPhone ก่อน จากนั้นลากไฟล์ APNs เข้ามาที่ Simulator ได้เลย

จากการทดสอบจะเห็นได้ว่ามี Push Notification เข้ามาแล้ว 🎉 🎉 🎉

แต่เดี๋ยวก่อน! ถ้าเราเปิดแอปค้างอยู่หล่ะ จะเป็นยังไง?

อ่าว! ไม่เห็นมีอะไรเกิดขึ้นเลย?

สาเหตุนี้เป็นเพราะ เรายังไม่ได้สั่งให้มันทำงานได้เมื่อสถานะของแอปเป็น Foreground หล่ะสิ

5. Push Notification เมื่อ App อยู่ในสถานะ Foreground

ไปที่ AppDelegate.swift จากนั้น Conform protocolUNUserNotificationCenterDelegate

โดยที่ Method didFinishLaunchingWithOptions ให้ใส่ code ดังนี้

UNUserNotificationCenter.current().delegate = self

จากนั้นให้ทำการสร้าง delegate method ดังนี้ userNotificationCenter(_:willPresent:withCompletionHandler:)

จากนั้นทำการเพิ่ม completionHandler([.alert, .sound, .badge]) ดังตัวอย่างทางด้านล่าง

เมื่อเสร็จแล้วเรามาทำการทดสอบกันเลย

จะเห็นได้ว่ามี Banner ทางด้านบนเมื่อ App ของเราอยู่ในสถานะ Foreground แล้ว 🎉

Special…

นอกจากนี้เรายังสามารถสั่ง push notification ที่ Terminal ได้ด้วยนะ โดยใช้คำสังทางด้านล่าง

xcrun simctl push <device> com.example.my-app ExamplePush.apns

โดย <device> คือ Identifier ของ Simulator สามารถดูได้ใน Xcode โดยกดปุ่ม Shift + ⌘ + 2 เพื่อเปิด Devices And Simulators จากนั้นให้เลือก Simulator ที่เรารัน คลิกขวาแล้วเลือก Copy Identifier ดังตัวอย่างภาพด้านล่างนี้

ส่วน com.example.my-app ให้ทำการเปลี่ยนเป็น Bundle Identifier ของ App เราเลย แล้วตามด้วย path ของไฟล์ .apns ที่เราได้สร้างไว้

xcrun simctl push 095409B6-18B1-4408-AC64-52F7B79120D6 /Users/MyName/Desktop/TestApnsFile.apns

จากนั้นกด Enter จะได้ผลลัพธ์ดังนี้

หมายเหตุ จากการทดสอบสิ่งที่ไม่มีเลยคือเสียงของ Notification และเลข Badge Number บน App ไม่แน่ใจว่าเป็นบัคหรือไม่ ต้องคอยติดตามในเวอร์ชั่นถัดไป ซึ่งทุกท่านสามารถเข้าไปอ่านเพิ่มเติมได้ที่ https://developer.apple.com/documentation/xcode_release_notes/xcode_11_4_release_notes

สุดท้ายนี้ต้องขอขอบคุณสำหรับข้อมูลดีๆนะครับ

แล้วพบกันใหม่ในบทความถัดไปนะครับ 🤝

--

--