{"id":251,"date":"2020-07-27T18:14:53","date_gmt":"2020-07-27T10:14:53","guid":{"rendered":"http:\/\/123.57.164.21\/?p=251"},"modified":"2020-08-05T12:18:19","modified_gmt":"2020-08-05T04:18:19","slug":"ios-uilocalnotification-%e5%92%8c-uiremotenotification-tips","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=251","title":{"rendered":"iOS LocalNotification \u548c RemoteNotification Tips"},"content":{"rendered":"\n<pre class=\"wp-block-preformatted\">1. \u901a\u77e5\u6743\u9650\u83b7\u5f97\uff0c App \u542f\u52a8\u65f6\u4f1a\u8be2\u95ee\uff0c \u662f\u5426\u5141\u8bb8\u63a5\u6536\u901a\u77e5\u3002<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import UIKit\n\n@UIApplicationMain\nclass AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate{\n    \n    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {\n        \n        UNUserNotificationCenter.current().delegate = self\n        \/\/ \u901a\u77e5\u6a29\u9650\u3092\u53d6\u5f97\n        UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) {\n            granted, error in\n            if granted {\n                DispatchQueue.main.async(execute: {\n                    \/\/ \u901a\u8fc7UIApplication\u6ce8\u518c\u8fdc\u7a0b\u901a\u77e5\u3002 \n                    \/\/ appWatch\u6ca1\u6709\u8fd9\u6bb5\u4ee3\u7801\uff0cappWatch\u6240\u4ee5\u4e0d\u80fd\u6ce8\u518c\u8fdc\u7a0b\u901a\u77e5\u670d\u52a1\u3002\n                    UIApplication.shared.registerForRemoteNotifications()\n                })\n            }\n        }\n        return true\n    }<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">2. RemoteNotification\u6ce8\u518c\u6210\u529f\u540e\uff0c \u4f1a\u6fc0\u6d3bAppDelegate\u4e2d\u4e0b\u9762\u7684\u65b9\u6cd5\u3002\u5f97\u5230 \u4ece\u82f9\u679cApns\u8fd4\u56de\u7684deviceToken\u3002<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">func application(_ application: UIApplication,\ndidRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {\n        let uuid = UIDevice.current.identifierForVendor!.uuidString\n        let deviceTokenStr = deviceToken.hexString\n    }\n\nextension Data {\n    var hexString: String {\n        let hexString = map { String(format: \"%02.2hhx\", $0) }.joined()\n        return hexString\n    }\n}<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">3. \u5728AppDelegate\u5b9e\u88c5\u4e0b\u9762\u7684\u4e24\u4e2a\u65b9\u6cd5\uff0c  App\u63a5\u6536\u901a\u77e5\u5230\u4ee5\u540e\uff0c \u4f1a\u6fc0\u6d3b\u4e0b\u9762\u7684\u65b9\u6cd5\u3002<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">    func userNotificationCenter(_ center: UNUserNotificationCenter,\n        willPresent notification: UNNotification,\n        withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)\n    {\n        completionHandler([.badge, .alert, .sound])\n    }\n\n    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {\n        completionHandler()\n    }<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">4. \u53d1\u9001LocalNotification\u4f8b\u5b50\u3002<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">            \/\/ \u30ed\u30fc\u30ab\u30eb\u901a\u77e5\n            let content = UNMutableNotificationContent()\n            content.title = \"\u672c\u5730\u901a\u77e5\"\n            \/\/ \u901a\u77e5\u6d88\u606f\u4f53\n            content.body = \"\u901a\u77e5\"\n            \/\/ \u901a\u77e5\u58f0\u97f3\uff0c \u53ef\u4ee5\u81ea\u5b9a\u4e49\n            content.sound = UNNotificationSound.default\n            \n            \/\/ \u30c8\u30ea\u30ac\u30fc\n            let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 0.1, repeats: false)\n            let requestIdentifier = \"com.localNotification.\" + String(Date().timeIntervalSince1970)\n            \n            \/\/ \u901a\u77e5\u30ea\u30af\u30a8\u30b9\u30c8\n            let request = UNNotificationRequest(identifier: requestIdentifier,\n                                                content: content, trigger: trigger)\n            \n            \/\/ \u901a\u77e5\u30e1\u30c3\u30bb\u30fc\u30b8\u3092\u9001\u4fe1\n            UNUserNotificationCenter.current().add(request) { error in\n                if error == nil {\n                    print(\"Time Interval Notification scheduled: \\(requestIdentifier)\")\n                }else{\n                    print(\"Time error: \\(String(describing: error))\")\n                }\n            }<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">5. \u901a\u8fc7NodeJs \u53d1\u9001RemoteNotification\u3002<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ \u9700\u8981npm install apn\npackage.json\n{\n  \"name\": \"RemoteNotificationSample\",\n  \"version\": \"1.0.0\",\n  \"description\": \"RemoteNotificationSample\",\n  \"main\": \"server.js\",\n  \"scripts\": {\n    \"start\": \"node server.js\"\n  },\n  \"dependencies\": {\n    \"apn\": \"^2.2.0\"\n  }\n}<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ \u4e00\u90e8\u5206nodejs\u4ee3\u7801\n\nconst apn = require(\"apn\");\n\nvar options = {\n  \/\/ \u53d1\u9001RemoteNotification\uff0c\u9700\u8981\u6709P12\u8bc1\u4e66\u3002\n  pfx: \".\/xxxxx.p12\",\n  \/\/ P12\u8bc1\u4e66 \u5bc6\u7801\n  passphrase: \"xxxxxx\",\n  production: true\n};\n\nvar apnProvider = new apn.Provider(options);\nvar note = new apn.Notification();\n\nnote.alert = \"\u8fd9\u662f\u4e00\u4e2aRemoteNotification\";\nnote.payload = { 'messageFrom': '\u30ea\u30e2\u30fc\u30c8\u901a\u77e5' };\n\/\/ \u548cApp \u7684Bundle Id\u9700\u8981\u4e00\u81f4\nnote.topic = \"com.xxx.xxx.xxx\";\nnote.sound = \"ping.aiff\";\n\n\/\/  \u901a\u8fc7apnProvider\u53d1\u9001RemoteNotification\n apnProvider.send(note, deviceToken).then((result) => {\n    console.log(result.failed)\n  });<\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<pre class=\"wp-block-preformatted\">6.LocalNotification \u548c RemoteNotification\u603b\u7ed3<\/pre>\n\n\n\n<p>[table id=1 \/]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. \u901a\u77e5\u6743\u9650\u83b7\u5f97\uff0c App \u542f\u52a8\u65f6\u4f1a\u8be2\u95ee\uff0c \u662f\u5426\u5141\u8bb8\u63a5\u6536\u901a\u77e5\u3002 2. RemoteNotification\u6ce8 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/251"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=251"}],"version-history":[{"count":13,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/251\/revisions"}],"predecessor-version":[{"id":267,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/251\/revisions\/267"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=251"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=251"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=251"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}