|
| 1 | +iOS如何绑定deviceToken? |
| 2 | + |
| 3 | +```objectivec |
| 4 | + |
| 5 | +#import "AppDelegate.h" |
| 6 | +// 引入环信SDK |
| 7 | +#import <Hyphenate/Hyphenate.h> |
| 8 | + |
| 9 | +@interface AppDelegate () |
| 10 | + |
| 11 | +@end |
| 12 | + |
| 13 | +@implementation AppDelegate |
| 14 | + |
| 15 | + |
| 16 | +- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { |
| 17 | + |
| 18 | + |
| 19 | + // ... |
| 20 | + |
| 21 | + |
| 22 | + // // 获取UNUserNotificationCenter并申请[badge, alert, sound]权限。 |
| 23 | + UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; |
| 24 | + [center requestAuthorizationWithOptions: |
| 25 | + UNAuthorizationOptionAlert | UNAuthorizationOptionBadge | UNAuthorizationOptionSound |
| 26 | + completionHandler:^(BOOL granted, NSError * _Nullable error) { |
| 27 | + if (granted) { |
| 28 | + dispatch_async(dispatch_get_main_queue(), ^{ |
| 29 | + // 注册远程推送 |
| 30 | + [application registerForRemoteNotifications]; |
| 31 | + }); |
| 32 | + } |
| 33 | + }]; |
| 34 | + |
| 35 | + |
| 36 | + return YES; |
| 37 | +} |
| 38 | + |
| 39 | +// 收到系统deviceToken获取成功回调 |
| 40 | +-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{ |
| 41 | + // 将deviceToken传给环信SDK |
| 42 | + [EMClient.sharedClient registerForRemoteNotificationsWithDeviceToken:deviceToken completion:nil]; |
| 43 | +} |
| 44 | + |
| 45 | + |
| 46 | + |
| 47 | +@end |
| 48 | + |
| 49 | +``` |
| 50 | +
|
| 51 | +```swift |
| 52 | +
|
| 53 | +import UIKit |
| 54 | +import Flutter |
| 55 | +// 引入环信SDK |
| 56 | +import HyphenateChat |
| 57 | +
|
| 58 | +@UIApplicationMain |
| 59 | +@objc class AppDelegate: FlutterAppDelegate { |
| 60 | + override func application( |
| 61 | + _ application: UIApplication, |
| 62 | + didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? |
| 63 | + ) -> Bool { |
| 64 | + GeneratedPluginRegistrant.register(with: self); |
| 65 | + |
| 66 | + // 获取UNUserNotificationCenter并申请[badge, alert, sound]权限。 |
| 67 | + let center = UNUserNotificationCenter.current(); |
| 68 | + center.requestAuthorization(options: [.badge, .alert, .sound]) { granted, error in |
| 69 | + if(granted){ |
| 70 | + DispatchQueue.main.async { |
| 71 | + // 注册远程推送 |
| 72 | + application.registerForRemoteNotifications(); |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + return super.application(application, didFinishLaunchingWithOptions: launchOptions) |
| 78 | + } |
| 79 | + // 收到系统deviceToken获取成功回调 |
| 80 | + override func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { |
| 81 | + // 将deviceToken传给环信SDK |
| 82 | + EMClient.share.registerForRemoteNotifications(withDeviceToken: deviceToken, completion: nil) |
| 83 | + } |
| 84 | +} |
| 85 | +
|
| 86 | +
|
| 87 | +
|
| 88 | +
|
| 89 | +``` |
0 commit comments