![](/img/trans.png)
[英]How can I implement Apple Push Notification Service on iOS Application?
[英]How can I test Apple Push Notification Service without an iPhone?
是否可以在沒有 iPhone 應用程序的情況下測試 Apple 推送通知服務? (在 windows 上創建模擬器?)
如果不是,我怎么能測試呢? 是否有編譯的免費示例應用程序來執行此操作?
我創建了服務器提供程序,但我需要測試功能。
這個答案已經過時了。 從 2020 / Xcode 11.4 開始,現在可以在模擬器中測試推送通知
在下面的答案中看到這個完整的解釋
抱歉,您需要找到一些硬件來測試此功能。
推送通知在模擬器中不可用。 它們需要來自 iTunes Connect 的配置文件,因此需要安裝在設備上。 這也意味着您可能必須被 Apple iPhone 開發者計划接受並支付 99 美元。
好的一面是,隨着 iPhone OS 3.0 更新,您可以在任何設備上測試此功能,包括第一代 iPhone。
您無法測試真正的推送通知。 但是,您可以通過以編程方式創建一個並手動觸發您的 AppDelegate 的- application:application didReceiveRemoteNotification:notification
方法來測試您的應用程序對模擬推送通知的響應。
要從不同的類(如 UIViewController)觸發它:
[[[UIApplication sharedApplication] delegate]
application:[UIApplication sharedApplication]
didReceiveRemoteNotification:testNotification];
testNotification 應該具有與真實通知相同的格式,即包含屬性列表對象和 NSNull 的 NSDictionary。
以下是如何提供上述testNotification
的示例:
NSMutableDictionary *notification = [[NSMutableDictionary alloc] init];
[notification setValue:@"Test" forKey:@"alert"];
[notification setValue:@"default" forKey:@"sound"];
NSMutableDictionary *testNotification = [[NSMutableDictionary alloc] init];
[testNotification setValue:notification forKey:@"aps"];
這應該創建一個合理的通知 NSDictionary 來使用。
現在,我們可以使用這個庫測試推送通知。
通過終端發送推送非常簡單:
echo -n '{"message":"message"}' | nc -4u -w1 localhost 9930
echo -n '{"aps":{"alert" : "message","badge" : 99,"sound" : "default"}, "myField" : 54758}' | nc -4u -w1 localhost 9930
從Xcode 11.4 開始,現在可以通過將.apns
文件拖放到 iOS 模擬器來模擬推送通知。 Xcode 11.4 發行說明對新功能有以下說明:
Simulator 支持模擬遠程推送通知,包括后台內容獲取通知。 在模擬器中,將 APNs 文件拖放到目標模擬器上。 該文件必須是具有有效 Apple 推送通知服務有效負載的 JSON 文件,包括“aps”鍵。 它還必須包含一個頂級“模擬器目標捆綁包” ,其字符串值與目標應用程序的捆綁包標識符相匹配。
simctl
還支持發送模擬推送通知。 如果文件包含“Simulator Target Bundle”,則不需要包標識符,否則您必須將其作為參數提供 (8164566):
xcrun simctl push <device> com.example.my-app ExamplePush.apns
這是此類.apns
文件的示例,針對系統設置應用程序:
{
"Simulator Target Bundle": "com.apple.Preferences",
"aps": {
"alert": "This is a simulated notification!",
"badge": 3,
"sound": "default"
}
}
將其拖放到目標模擬器上將顯示通知並設置徽章:
現在,要將其用於調試目的,您只需將Simulator Target Bundle
更改為您自己的應用程序標識符並根據您的調試需要調整有效負載!
Apple 支持模擬器的推送通知。 iOS 13.4 及更高版本或 Xcode 11.4 及更高版本。
通常創建Xcode項目並實現用戶通知和授權權限。
在模擬器 iOS 13.4 及更高版本中運行您的應用程序。
將您的應用程序置於后台。
{
"aps": {
"alert": {
"title": "Test Push",
"body": "Success! Push notification in simulator! 🎉",
"sound": "default"
},
"badge": 10
},
"Simulator Target Bundle": "com.company.app"
}
現在您的推送通知將出現在模擬器上。
您也可以通過終端模擬推送通知
通過打開Window->Devices and Simulators並選擇您的目標模擬器並右鍵單擊並復制您的標識符來獲取您的模擬器標識符。
現在構建一個終端命令,如
xcrun simctl push <simulator-identifier> <path-to-payload-file>
前任:
xcrun simctl push 27A23727-45A9-4C12-BE29-8C0E6D1E5360 payload.apns
運行此命令並在模擬器中模擬推送通知
模擬器不執行推送通知。
要從服務器推送,您必須擁有要推送到的設備以及該設備上的應用程序。
令牌包含應用程序標識和設備 ID。
你必須使用
NSString *notificationString = @"{\"aps\":{\"alert\":\"Test alert\",\"sound\":\"default\"}}";
NSData *notificationData = [notificationString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *testNotification = [NSJSONSerialization JSONObjectWithData:notificationData options:0 error:&error];
[[[UIApplication sharedApplication] delegate] application:[UIApplication sharedApplication] didReceiveRemoteNotification:testNotification fetchCompletionHandler:nil];
Xcode 11.4 Beta 開始支持模擬器中的推送通知
Simulator 支持模擬遠程推送通知,包括后台內容獲取通知。 在模擬器中,將 APNs 文件拖放到目標模擬器上。 該文件必須是具有有效 Apple 推送通知服務負載的 JSON 文件,包括“aps”鍵。 它還必須包含一個頂級“模擬器目標捆綁包”,其字符串值與目標應用程序的捆綁包標識符相匹配。
simctl 還支持發送模擬推送通知。 如果文件包含“Simulator Target Bundle”,則不需要包標識符,否則您必須將其作為參數提供 (8164566):
$ xcrun simctl push <device> com.example.my-app ExamplePush.apns
發行說明: https : //developer.apple.com/documentation/xcode_release_notes/xcode_11_4_beta_release_notes
除了@fredpi 的回答,您還可以使用Poes命令行工具。 它允許我們在 iOS 模擬器上快速測試遠程推送通知,而無需創建 JSON 有效負載文件。
Poes --help
OVERVIEW: A Swift command-line tool to easily send push notifications to the iOS simulator
USAGE: Poes <options>
OPTIONS:
--body, -b The body of the Push Notification
--bundle-identifier The bundle identifier to push to
--mutable, -m Adds the mutable-content key to the payload
--title, -t The title of the Push Notification
--verbose Show extra logging for debugging purposes
--help Display available options
以下命令足以發送帶有默認標題和正文的簡單推送通知:
$ Poes --bundle-identifier com.wetransfer.app --verbose
Generated payload:
{
"aps" : {
"alert" : {
"title" : "Default title",
"body" : "Default body"
},
"mutable-content" : false
}
}
Sending push notification...
Push notification sent successfully
是的,您可以在模擬器上檢查推送通知,但您必須在您的應用程序中使用名為SimulatorRemoteNotifications的庫。 通過它,只需使用 4-5 個步驟,您就可以在模擬器上測試推送通知。
他們也提供 POD:
pod 'SimulatorRemoteNotifications', '~> 0.0.3'
看起來現在我們有非常強大的庫https://github.com/ctreffs/SwiftSimctl
至少它比這里提到的 lib SimulatorRemoteNotifications
強大得多。 這也已經過時了。
對於自 Xcode 11.4 和 Xcode 12.4 起更完整的答案:
Simulator 支持模擬遠程推送通知,包括后台內容獲取通知。 在模擬器中,將 APNs 文件拖放到目標模擬器上。 該文件必須是具有有效 Apple 推送通知服務負載的 JSON 文件,包括“aps”鍵。 它還必須包含一個頂級“模擬器目標捆綁包”,其字符串值與目標應用程序的捆綁包標識符相匹配。 simctl 還支持發送模擬推送通知。
通知服務擴展在模擬推送通知中不起作用。 不遵守可變內容鍵。 (55822721)
參考: Xcode 11.4 發行說明
在 macOS 13 和 Xcode 14 中,在配備 Apple 芯片或 T2 處理器的 Mac 計算機上,現在可以接收真實推送通知,因為didRegisterForRemoteNotificationsWithDeviceToken
將返回真實令牌。
請參閱 Xcode 14 發行說明:
模擬器現在支持 iOS 16 在具有 Apple 硅或 T2 處理器的 Mac 計算機上的 macOS 13 中運行時的遠程通知。 模擬器支持 Apple Push Notification Service Sandbox 環境。 您的服務器可以通過連接到 APNS 沙箱 (api.sandbox.push.apple.com) 向在該模擬器中運行的應用程序發送遠程通知。 每個模擬器都會生成該模擬器和運行它的 Mac 硬件組合所獨有的注冊令牌。
https://developer.apple.com/documentation/xcode-release-notes/xcode-14-release-notes
大多數答案建議使用第三方 Cocoapods/SPM 依賴項。 但是在新的 iOS 或 Xcode 版本發布后的很長一段時間內,這些依賴項很容易變得不受支持。
這只是一個適用於任何情況和任何天氣的輕量級解決方案:
xcrun simctl push
命令如需更多詳細信息,請隨時查看此博客文章。 希望這可以幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.