iOS 模擬器上測試推送

Xcode 11.4 beta 版本開始支持在 iOS 模擬器上測試推送通知。

準(zhǔn)備工作

打開項(xiàng)目的推送通知開關(guān):

截屏2021-10-08 下午4.32.45.png

注冊推送通知:

// MARK: - Push Notification
    
    private func setupPushNotification() {
        UNUserNotificationCenter.current().delegate = self
    }
    
    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        PushService.instance.receiveDeviceToken(deviceToken: deviceToken)
    }
    
    // MARK: - UNUserNotificationCenterDelegate
    
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        if UIApplication.shared.applicationState == UIApplication.State.active {
            if let isContract = PushService.instance.isContract(userInfo: notification.request.content.userInfo), !isContract {
                if #available(iOS 14.0, *) {
                    completionHandler([.banner, .list])
                } else {
                    completionHandler([.alert])
                }
            }
        }
        completionHandler([])
    }

    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        PushService.instance.check(userInfo: response.notification.request.content.userInfo)
        completionHandler()
    }

生成 playload 文件:xxx.apns

{
    "Simulator Target Bundle": "jp.co.sbisec.sbikabu2spUAT",
    "aps": {
        "alert": {
            "title": "通知 title",
            "subtitle": "通知 subtitle",
            "body": "通知 message content"
        },
        "badge": 1,
        "sound": "default"
    },
    "extras": {
        "notification_type": "4",
        "product_code": "1401",
        "index_code": "00"
    }
}

注意:xxx.apns文件里extras的格式要與注冊通知,接收通知的代碼里格式保持一致

使用命令行方式

xcrun simctl push booted xxx.apns

如果出現(xiàn)下面錯(cuò)誤:

xcrun: error: unable to find utility "simctl", not a developer tool or in PATH ...

可以通過 Xcode > Preferences > Locations 下的指定命令行工具來解決:


截屏2021-10-08 下午4.50.07.png

如果不想在 playload 文件中指定 app 的 bundle identifier,則可以使用下面命令:

xcrun simctl push booted <app-bundle-identifier> xxx.apns

使用拖拽的方式

直接將 .apns 文件推拽到模擬器中即可,這個(gè)是最簡單的測試方式

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容