{"id":6757,"date":"2023-10-13T12:33:30","date_gmt":"2023-10-13T04:33:30","guid":{"rendered":"https:\/\/www.yimenyun.cn\/emen\/?p=6757"},"modified":"2023-10-13T12:33:30","modified_gmt":"2023-10-13T04:33:30","slug":"ios-%e6%89%93%e5%8c%85%e6%97%b6%e6%9c%aa%e6%b7%bb%e5%8a%a0push%e6%a8%a1%e5%9d%97%e6%9c%89%e4%bb%80%e4%b9%88%e5%bd%b1%e5%93%8d%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/www.yimenyun.cn\/emen\/6757\/","title":{"rendered":"ios \u6253\u5305\u65f6\u672a\u6dfb\u52a0push\u6a21\u5757\u6709\u4ec0\u4e48\u5f71\u54cd\uff1f"},"content":{"rendered":"

iOS\u6253\u5305\u65f6\u5982\u679c\u672a\u6dfb\u52a0push\u6a21\u5757\uff0c\u5373\u672a\u8fdb\u884c\u8fdc\u7a0b\u63a8\u9001\u529f\u80fd\u7684\u96c6\u6210\uff0c\u90a3\u4e48\u5728\u5e94\u7528\u7a0b\u5e8f\u4e2d\u5c31\u65e0\u6cd5\u4f7f\u7528\u82f9\u679c\u63d0\u4f9b\u7684\u8fdc\u7a0b\u901a\u77e5\u670d\u52a1\u3002\u672c\u6587\u5c06\u4e3a\u60a8\u8be6\u7ec6\u4ecb\u7ecdiOS\u63a8\u9001\u6a21\u5757\u7684\u539f\u7406\u53ca\u5176\u96c6\u6210\u6b65\u9aa4\u3002<\/p>\n

### \u4e00\u3001Push\u6a21\u5757\u7b80\u4ecb<\/p>\n

\u63a8\u9001\u901a\u77e5\uff08Push Notification\uff09\u662f\u4e00\u79cd\u5728\u8bbe\u5907\u4e0a\u663e\u793a\u5f39\u51fa\u5f0f\u901a\u77e5\u7684\u6280\u672f\uff0c\u7528\u6237\u53ef\u4ee5\u968f\u65f6\u63a5\u6536\u5230\u5e94\u7528\u7a0b\u5e8f\u63a8\u9001\u7684\u6d88\u606f\uff0c\u5373\u4f7f\u5e94\u7528\u7a0b\u5e8f\u672a\u5728\u524d\u53f0\u8fd0\u884c\u4e5f\u80fd\u6536\u5230\u901a\u77e5\u3002\u5728iOS\u7cfb\u7edf\u4e2d\uff0c\u82f9\u679c\u63d0\u4f9b\u4e86APNs\uff08Apple Push Notification service\uff09\u63a8\u9001\u670d\u52a1\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u901a\u8fc7\u96c6\u6210APNs\u6a21\u5757\u5b9e\u73b0\u6d88\u606f\u63a8\u9001\u3002<\/p>\n

### \u4e8c\u3001\u63a8\u9001\u6a21\u5757\u539f\u7406<\/p>\n

APNs\u662f\u82f9\u679c\u63d0\u4f9b\u7684\u670d\u52a1\uff0c\u5b83\u8d1f\u8d23\u63a8\u9001\u63a8\u9001\u6d88\u606f\u7ed9iOS\u8bbe\u5907\u3002\u63a8\u9001\u6d88\u606f\u7684\u6574\u4e2a\u6d41\u7a0b\u5982\u4e0b\uff1a<\/p>\n

1. \u5f00\u53d1\u8005\u5c06\u8bbe\u5907\u7684Push token\uff08\u63a8\u9001\u6807\u8bc6\u7b26\uff09\u53d1\u9001\u7ed9\u81ea\u5df1\u7684\u670d\u52a1\u5668\u3002<\/p>\n

2. \u5f00\u53d1\u8005\u5c06\u8981\u53d1\u9001\u7684\u6d88\u606f\u548c\u8bbe\u5907\u7684Push token\u53d1\u9001\u7ed9APNs\u3002<\/p>\n

3. APNs\u5c06\u6d88\u606f\u53d1\u9001\u7ed9\u62e5\u6709\u8be5token\u7684\u8bbe\u5907\u3002<\/p>\n

4. \u8bbe\u5907\u63a5\u6536\u5230\u63a8\u9001\u6d88\u606f\u5e76\u663e\u793a\u5728\u901a\u77e5\u4e2d\u5fc3\u6216\u9501\u5c4f\u4e0a\u3002<\/p>\n

### \u4e09\u3001\u63a8\u9001\u6a21\u5757\u96c6\u6210\u6b65\u9aa4<\/p>\n

\u5728\u6dfb\u52a0Push\u6a21\u5757\u524d\uff0c\u60a8\u9700\u8981\u5148\u6ce8\u518c\u4e00\u4e2a\u60a8\u7684\u5e94\u7528\u7a0b\u5e8f\u7684App ID\uff0c\u5e76\u5f00\u542f\u63a8\u9001\u529f\u80fd\u3002\u63a5\u4e0b\u6765\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u63a8\u9001\u6a21\u5757\u7684\u96c6\u6210\u6b65\u9aa4\uff1a<\/p>\n

#### 1. \u521b\u5efa\u63a8\u9001\u8bc1\u4e66<\/p>\n

1. \u6253\u5f00Apple Developer\u7f51\u7ad9\uff0c\u9009\u62e9\u201cCertificates, Identifiers & Profiles\u201d\u3002<\/p>\n

2. \u9009\u62e9\u201cApp IDs\u201d\u5e76\u521b\u5efa\u4e00\u4e2a\u65b0\u7684App ID\uff0c\u786e\u4fdd\u5df2\u5f00\u542fPush Notifications\u529f\u80fd\u3002<\/p>\n

3. \u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u63a8\u9001\u8bc1\u4e66\uff0c\u4e0b\u8f7d.p12\u683c\u5f0f\u7684\u63a8\u9001\u8bc1\u4e66\u5230\u672c\u5730\u3002<\/p>\n

#### 2. \u914d\u7f6e\u63a8\u9001\u6a21\u5757<\/p>\n

1. \u6253\u5f00Xcode\uff0c\u9009\u62e9\u9879\u76ee\u7684target\uff0c\u5728Capabilities\u9009\u9879\u5361\u4e2d\u5f00\u542f\u201cPush Notifications\u201d\u3002<\/p>\n

2. \u5bfc\u5165\u63a8\u9001\u8bc1\u4e66\uff0c\u5728\u201cSigning & Capabilities\u201d\u9009\u9879\u5361\u4e2d\u70b9\u51fb\u201c+\u201d\u6309\u94ae\uff0c\u9009\u62e9\u201cApple Push Notifications service\u201d\u3002<\/p>\n

#### 3. \u8bbe\u7f6e\u63a8\u9001\u529f\u80fd<\/p>\n

1. \u5728AppDelegate\u6587\u4ef6\u4e2d\u6dfb\u52a0\u63a8\u9001\u529f\u80fd\u76f8\u5173\u7684\u4ee3\u7801\u3002<\/p>\n

“`swift<\/p>\n

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {<\/p>\n

\/\/ \u6ce8\u518c\u8fdc\u7a0b\u63a8\u9001\u670d\u52a1<\/p>\n

UNUserNotificationCenter.current().delegate = self<\/p>\n

UNUserNotificationCenter.current().requestAuthorization(optios\u7ec4\u4ef6\u5316\u80fd\u52a0\u5feb\u6253\u5305\u5417<\/a>ions: [.alert, .sound, .badge]) { (granted, error) in<\/p>\n

if granted {<\/p>\n

DispatchQueue.main.async(execute: {<\/p>\n

application.registerForRemoteNotifications()<\/p>\n

})<\/p>\n

}<\/p>\n

}<\/p>\n

return true<\/p>\n

}<\/p>\n

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {<\/p>\n

let token = deviceToken.map { String(format: “%02.2hhx”, $0) }.joined()<\/p>\n

\/\/ \u5c06\u8bbe\u5907token\u53d1\u9001\u7ed9\u670d\u52a1\u5668\u4fdd\u5b58<\/p>\n

}<\/p>\n

func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {<\/p>\n

ios\u4e91\u6253\u5305\u8bbe\u7f6e\u6559\u7a0b<\/a> print(“Failed to register for remote notifications: \\(error.localizedDescription)”)<\/p>\n

}<\/p>\n

“`<\/p>\n

2. \u5c06\u8bbe\u5907\u7684Push token\u53d1\u9001\u7ed9\u670d\u52a1\u5668\u4fdd\u5b58\uff0c\u4ee5\u4fbf\u540e\u7eed\u5411\u8be5\u8bbe\u5907\u63a8\u9001\u6d88\u606f\u3002<\/p>\n

#### 4. \u63a8\u9001\u6d88\u606f\u5904\u7406<\/p>\n

1. \u5728AppDelegate\u6587\u4ef6\u4e2d\u6dfb\u52a0\u5904\u7406\u63a8\u9001\u6d88\u606f\u7684\u4ee3\u7801\u3002<\/p>\n

“`swift<\/p>\n

\/\/ \u5904\u7406App\u5728\u524d\u53f0\u8fd0\u884c\u65f6\u63a5\u6536\u5230\u7684\u63a8\u9001\u6d88\u606f<\/p>\n

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {<\/p>\n

completionHandler([.alert, .badge, .sound])<\/p>\n

}<\/p>\n

\/\/ \u5904\u7406App\u5728\u540e\u53f0\u6216\u672a\u8fd0\u884c\u65f6\u63a5\u6536\u5230\u7684\u63a8\u9001\u6d88\u606f<\/p>\n

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {<\/p>\n

completionHandler()<\/p>\n

}<\/p>\n

“`<\/p>\n

### \u56db\u3001\u603b\u7ed3<\/p>\n

\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86iOS\u63a8\u9001\u6a21\u5757\u7684\u539f\u7406\u53ca\u5176\u96c6\u6210\u6b65\u9aa4\u3002\u901a\u8fc7\u6dfb\u52a0Push\u6a21\u5757\uff0c\u60a8\u53ef\u4ee5\u5b9e\u73b0\u5e94\u7528\u7a0b\u5e8f\u7684\u6d88\u606f\u63a8\u9001\u529f\u80fd\uff0c\u4f7f\u7528\u6237\u80fd\u591f\u53ca\u65f6\u6536\u5230\u91cd\u8981\u7684\u5e94\u7528\u901a\u77e5\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u60a8\u6709\u6240\u5e2e\u52a9\uff0c\u5982\u679c\u6709\u4efb\u4f55\u7591<\/p>\n

<\/figure>\n<\/p>\n

\u95ee\uff0c\u8bf7\u968f\u65f6\u5411\u6211\u63d0\u95ee\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

iOS\u6253\u5305\u65f6\u5982\u679c\u672a\u6dfb\u52a0push\u6a21\u5757\uff0c\u5373\u672a\u8fdb\u884c\u8fdc\u7a0b\u63a8\u9001\u529f\u80fd\u7684\u96c6\u6210\uff0c\u90a3\u4e48\u5728\u5e94\u7528\u7a0b\u5e8f\u4e2d\u5c31\u65e0\u6cd5\u4f7f\u7528\u82f9\u679c\u63d0\u4f9b\u7684\u8fdc\u7a0b\u901a\u77e5\u670d\u52a1\u3002\u672c\u6587\u5c06\u4e3a\u60a8\u8be6\u7ec6\u4ecb\u7ecdiOS\u63a8\u9001\u6a21\u5757\u7684\u539f\u7406\u53ca\u5176\u96c6\u6210\u6b65\u9aa4\u3002### \u4e00\u3001Push\u6a21\u5757\u7b80\u4ecb\u63a8\u9001<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[10703,12174,662,226,56],"topic":[],"class_list":["post-6757","post","type-post","status-publish","format-standard","hentry","category-appkf","tag-iosipa","tag-ios","tag-662","tag-226","tag-56"],"_links":{"self":[{"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/posts\/6757","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/comments?post=6757"}],"version-history":[{"count":0,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/posts\/6757\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/media?parent=6757"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/categories?post=6757"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/tags?post=6757"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/topic?post=6757"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}