{"id":5419,"date":"2023-05-18T12:05:28","date_gmt":"2023-05-18T04:05:28","guid":{"rendered":"https:\/\/app.applebyme.cn\/cloud\/?p=5419"},"modified":"2023-05-18T12:05:28","modified_gmt":"2023-05-18T04:05:28","slug":"ios-%e6%8e%a8%e9%80%81%e5%bc%80%e5%8f%91%e9%9c%80%e8%a6%81%e6%b3%a8%e6%84%8f%e5%93%aa%e4%ba%9b%e5%9d%91%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/app.applebyme.cn\/cloud\/appshangjia\/5419.html","title":{"rendered":"ios \u63a8\u9001\u5f00\u53d1\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u5751\uff1f"},"content":{"rendered":"<p>iOS \u63a8\u9001\u5f00\u53d1\u662f\u6307\u5728 iOS \u5e94\u7528\u7a0b\u5e8f\u4e2d\u4f7f\u7528 Apple \u63a8\u9001\u901a\u77e5\u670d\u52a1\uff08APNs\uff09\u5411\u8bbe\u5907\u53d1\u9001\u901a\u77e5\u3002APNs \u662f\u4e00\u79cd\u5728 iOS \u8bbe\u5907\u4e0a\u5f39\u51fa\u901a\u77e5\u7684\u6280\u672f\uff0c\u5b83\u4f7f\u5f00\u53d1\u4eba\u5458\u53ef\u4ee5\u5411\u5e94\u7528\u7a0b\u5e8f\u7684\u7528\u6237\u53d1\u9001\u901a\u77e5\uff0c\u5373\u4f7f\u5e94\u7528\u7a0b\u5e8f\u672a\u5728\u4f7f\u7528\u4e2d\u4e5f\u53ef\u4ee5\u63a5\u6536\u5230\u901a\u77e5\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u6df1\u5165\u4e86\u89e3 APNs \u7684\u5de5\u4f5c\u539f\u7406\uff0c\u4ee5\u53ca\u5982\u4f55\u5728 iOS \u5e94\u7528\u7a0b\u5e8f\u4e2d\u5b9e\u73b0\u63a8\u9001\u901a\u77e5\u529f\u80fd\u3002<\/p>\n<p>\u4e00\u3001APNs \u7684\u5de5\u4f5c\u539f\u7406<\/p>\n<p>APNs \u662f\u4e00\u79cd\u5ba2\u6237\u7aef-\u670d\u52a1\u5668\u901a\u4fe1\u534f\u8bae\uff0c\u5b83\u7531 Apple \u63d0\u4f9b\uff0c\u7528\u4e8e\u5728 iOS \u8bbe\u5907\u4e0a\u5f39\u51fa\u901a\u77e5\u3002APNs \u7684\u5de5\u4f5c\u539f\u7406\u5982\u4e0b\uff1a<\/p>\n<p>1. \u5e94\u7528\u7a0b\u5e8f\u5411 APNs \u6ce8\u518c\u4ee5\u63a5\u6536\u901a\u77e5\u3002<\/p>\n<p>2. \u5e94\u7528\u7a0b\u5e8f\u5728\u8bbe\u5907\u4e0a\u5b89\u88c5\u65f6\u4f1a\u751f\u6210\u4e00\u4e2a\u552f\u4e00\u7684\u8bbe\u5907\u4ee4\u724c\uff08Device Token\uff09\u5e76\u53d1\u9001\u7ed9 APNs\u3002<\/p>\n<p>3. \u5f53\u5e94\u7528\u7a0b\u5e8f\u9700\u8981\u53d1\u9001\u901a\u77e5\u65f6\uff0c\u5b83\u4f1a\u5c06\u901a\u77e5\u53d1\u9001\u7ed9 APNs\u3002<\/p>\n<p>4. APNs \u5c06\u901a\u77e5\u4f20\u9012\u7ed9\u8bbe\u5907\uff0c\u8bbe\u5907\u4f7f\u7528\u552f\u4e00\u8bbe\u5907\u4ee4\u724c\u6765\u786e\u5b9a\u54ea\u4e2a\u5e94\u7528\u7a0b\u5e8f\u5e94\u8be5\u63a5\u6536\u901a\u77e5\u3002<\/p>\n<p>5. \u8bbe\u5907\u5f39\u51fa\u901a\u77e5\u3002<\/p>\n<p>\u4e8c\u3001\u5b9e\u73b0 iOS \u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684\u63a8\u9001\u901a\u77e5\u529f\u80fd<\/p>\n<p>\u5728 iOS \u5e94\u7528\u7a0b\u5e8f\u4e2d\u5b9e\u73b0\u63a8\u9001\u901a\u77e5\u529f\u80fd\uff0c\u9700\u8981\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u8fdb\u884c\uff1a<\/p>\n<p>1. \u5728 Apple \u5f00\u53d1\u8005\u7f51\u7ad9\u4e0a\u521b\u5efa\u4e00\u4e2a\u8bc1\u4e66\u3002<\/p>\n<p>2. \u5c06\u8bc1\u4e66\u6dfb\u52a0\u5230 Xcode \u4e2d\u3002<\/p>\n<p>3. \u5728\u5e94\u7528\u7a0b\u5e8f\u4e2d\u542f\u7528\u63a8\u9001\u901a\u77e5\u529f\u80fd\u3002<\/p>\n<p>4. \u7f16\u5199\u4ee3\u7801\u6765\u5904\u7406\u63a8\u9001\u901a\u77e5\u3002<\/p>\n<p>1. \u5728 Apple \u5f00\u53d1\u8005\u7f51\u7ad9\u4e0a\u521b\u5efa\u4e00\u4e2a\u8bc1\u4e66<\/p>\n<p>\u9996\u5148\uff0c\u60a8\u9700\u8981\u5728 Apple \u5f00\u53d1\u8005\u7f51\u7ad9\u4e0a\u6ce8\u518c\u5e76\u521b\u5efa\u4e00\u4e2a\u8bc1\u4e66\uff0c\u8fd9\u5c06\u4f7f\u60a8\u80fd\u591f\u4f7f\u7528 APNs\u3002\u5728 Apple \u5f00\u53d1\u8005\u7f51\u7ad9\u4e0a\u521b\u5efa\u8bc1\u4e66\u7684\u8fc7\u7a0b\u76f8\u5bf9\u7b80\u5355\uff0c\u53ea\u9700\u8981\u6309\u7167\u7f51\u7ad9\u4e0a\u7684\u8bf4\u660e\u8fdb\u884c\u64cd\u4f5c\u5373\u53ef\u3002<\/p>\n<p>2. \u5c06\u8bc1\u4e66\u6dfb\u52a0\u5230 Xcode \u4e2d<\/p>\n<p>\u4e00\u65e6\u60a8\u5728 Apple \u5f00\u53d1\u8005\u7f51\u7ad9\u4e0a\u521b\u5efa\u4e86\u8bc1\u4e66\uff0c\u60a8\u9700\u8981\u5c06\u5b83\u6dfb\u52a0\u5230 Xcode \u4e2d\u3002\u6253\u5f00 Xcode\uff0c\u5355\u51fb\u201cFile\u201d&gt;\u201cNew\u201d&gt;\u201cTarget\u201d\uff0c\u7136\u540e\u9009\u62e9\u201cNotification Extension\u201d\u6216\u201cNotification Service Extension\u201d\u3002\u5728\u5f39\u51fa\u7684\u5bf9\u8bdd\u6846\u4e2d\uff0c\u9009\u62e9\u201cCertificate\u201d\uff0c\u7136\u540e\u9009\u62e9\u60a8\u5728 Apple \u5f00\u53d1\u8005\u7f51\u7ad9\u4e0a\u521b\u5efa\u7684\u8bc1\u4e66\u3002<\/p>\n<p>3. \u5728\u5e94\u7528\u7a0b\u5e8f\u4e2d\u542f\u7528\u63a8\u9001\u901a\u77e5\u529f\u80fd<\/p>\n<p>\u4e3a\u4e86\u5728\u5e94\u7528\u7a0b\u5e8f\u4e2d\u542f\u7528\u63a8\u9001\u901a\u77e5\u529f\u80fd\uff0c\u60a8\u9700\u8981\u5728\u5e94\u7528\u7a0b\u5e8f\u7684 AppDelegate.swift \u6587\u4ef6\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<p>&#8220;`<\/p>\n<p>import UIKit<\/p>\n<p>impor<\/p>\n<p><figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/i.cdn.yimenapp.com\/2023-03\/31\/15\/07792804256.jpg\" \/><\/figure>\n<\/p>\n<p>t UserNotifications<\/p>\n<p>@UIApplicationMain<\/p>\n<p>class AppDelegate: UIResponder, UIApplicationDelegate, UN<a href=\"https:\/\/www.yimenapp.net\/dabao-2329.html\">flutter ios\u6253\u5305\u538b\u7f29<\/a>UserNotificationCenterDelegate {<\/p>\n<p>    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApp<a href=\"https:\/\/www.yimenapp.net\/dabao-941.html\">apk \u83b7\u53d6\u7b7e\u540d<\/a>lication.LaunchOptionsKey: Any]?) -&gt; Bool {<\/p>\n<p>        \/\/ \u6ce8\u518c\u901a\u77e5<\/p>\n<p>        UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { (granted, error) in<\/p>\n<p>            print(&#8220;Permission granted: \\(granted)&#8221;)<\/p>\n<p>        }<\/p>\n<p>        UNUserNotificationCenter.current().delegate = self<\/p>\n<p>        application.registerForRemoteNotifications()<\/p>\n<p>        return true<\/p>\n<p>    }<\/p>\n<p>    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {<\/p>\n<p>        let token = deviceToken.map { String(format: &#8220;%02.2hhx&#8221;, $0) }.joined()<\/p>\n<p>        print(&#8220;Device Token: \\(token)&#8221;)<\/p>\n<p>    }<\/p>\n<p>    func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {<\/p>\n<p>        print(&#8220;Failed to register for remote notifications: \\(error.localizedDescription)&#8221;)<\/p>\n<p>    }<\/p>\n<p>    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -&gt; Void) {<\/p>\n<p>        let userInfo = response.notification.request.content.userInfo<\/p>\n<p>        print(&#8220;Received push notification: \\(userInfo)&#8221;)<\/p>\n<p>        completionHandler()<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p>&#8220;`<\/p>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u8c03\u7528\u4e86 UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) \u65b9\u6cd5\u6765\u8bf7\u6c42\u7528\u6237\u6388\u6743\uff0c\u4ee5\u4fbf\u5e94\u7528\u7a0b\u5e8f\u53ef\u4ee5\u63a5\u6536\u901a\u77e5\u3002\u6211\u4eec\u8fd8\u6ce8\u518c\u4e86\u5e94\u7528\u7a0b\u5e8f\uff0c\u4ee5\u4fbf\u5b83\u53ef\u4ee5\u63a5\u6536\u8fdc\u7a0b\u901a\u77e5\uff0c\u5e76\u5728\u5e94\u7528\u7a0b\u5e8f\u542f\u52a8\u65f6\u6253\u5370\u8bbe\u5907\u4ee4\u724c\u3002<\/p>\n<p>4. \u7f16\u5199\u4ee3\u7801\u6765\u5904\u7406\u63a8\u9001\u901a\u77e5<\/p>\n<p>\u6700\u540e\uff0c\u60a8\u9700\u8981\u7f16\u5199\u4ee3\u7801\u6765\u5904\u7406\u63a8\u9001\u901a\u77e5\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u5728 AppDelegate.swift \u6587\u4ef6\u4e2d\u5b9e\u73b0 userNotificationCenter(_:didReceive:withCompletionHandler:) \u65b9\u6cd5\u6765\u5b8c\u6210\u3002\u4f8b\u5982\uff1a<\/p>\n<p>&#8220;`<\/p>\n<p>func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -&gt; Void) {<\/p>\n<p>    let userInfo = response.notification.request.content.userInfo<\/p>\n<p>    print(&#8220;Received push notification: \\(userInfo)&#8221;)<\/p>\n<p>    completionHandler()<\/p>\n<p>}<\/p>\n<p>&#8220;`<\/p>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u5728\u63a5\u6536\u5230\u901a\u77e5\u65f6\u6253\u5370\u901a\u77e5\u7684\u4fe1\u606f\u3002<\/p>\n<p>\u603b\u7ed3<\/p>\n<p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u6df1\u5165\u4e86\u89e3\u4e86 APNs \u7684\u5de5\u4f5c\u539f\u7406\uff0c\u5e76\u4ecb\u7ecd\u4e86\u5982\u4f55\u5728 iOS \u5e94\u7528\u7a0b\u5e8f\u4e2d\u5b9e\u73b0\u63a8\u9001\u901a\u77e5\u529f\u80fd\u3002\u867d\u7136\u5b9e\u73b0\u63a8\u9001\u901a\u77e5\u529f\u80fd\u53ef\u80fd\u6709\u4e9b\u590d\u6742\uff0c\u4f46\u5b83\u53ef\u4ee5\u4f7f\u60a8\u7684\u5e94\u7528\u7a0b\u5e8f\u66f4\u52a0\u4e92\u52a8\u548c\u6709\u8da3\uff0c\u4e3a\u7528\u6237\u63d0\u4f9b\u66f4\u597d\u7684\u4f53\u9a8c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>iOS \u63a8\u9001\u5f00\u53d1\u662f\u6307\u5728 iOS \u5e94\u7528\u7a0b\u5e8f\u4e2d\u4f7f\u7528 Apple \u63a8\u9001\u901a\u77e5\u670d\u52a1\uff08APNs\uff09\u5411\u8bbe\u5907\u53d1\u9001\u901a\u77e5\u3002APNs \u662f\u4e00\u79cd\u5728 iOS \u8bbe\u5907\u4e0a\u5f39\u51fa\u901a\u77e5\u7684\u6280\u672f\uff0c\u5b83\u4f7f\u5f00\u53d1\u4eba\u5458\u53ef\u4ee5\u5411\u5e94\u7528\u7a0b\u5e8f\u7684\u7528\u6237\u53d1\u9001\u901a\u77e5\uff0c\u5373\u4f7f\u5e94<\/p>\n","protected":false},"author":18,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14],"tags":[7713,2950,1775,882,922],"class_list":["post-5419","post","type-post","status-publish","format-standard","hentry","category-appshangjia","tag-android-popupwindow","tag-app","tag-1775","tag-882","tag-922"],"_links":{"self":[{"href":"https:\/\/app.applebyme.cn\/cloud\/wp-json\/wp\/v2\/posts\/5419","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/app.applebyme.cn\/cloud\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/app.applebyme.cn\/cloud\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/app.applebyme.cn\/cloud\/wp-json\/wp\/v2\/users\/18"}],"replies":[{"embeddable":true,"href":"https:\/\/app.applebyme.cn\/cloud\/wp-json\/wp\/v2\/comments?post=5419"}],"version-history":[{"count":0,"href":"https:\/\/app.applebyme.cn\/cloud\/wp-json\/wp\/v2\/posts\/5419\/revisions"}],"wp:attachment":[{"href":"https:\/\/app.applebyme.cn\/cloud\/wp-json\/wp\/v2\/media?parent=5419"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/app.applebyme.cn\/cloud\/wp-json\/wp\/v2\/categories?post=5419"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/app.applebyme.cn\/cloud\/wp-json\/wp\/v2\/tags?post=5419"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}