实现:获取devicetoken(ios10 xcode8)
一、开发者AppDeveloper 注册证书:
开发证书,发布证书(此处步骤大家应该都会比较熟悉,点击进入开发者开发证书以及发布证书创建)
二、应用内部设置:
1、在Targets-->Capabilities-->Push Notifications开关设置成 ON
打开Push Notifications开关,自动生成.entitlements文件
2、在Targets-->Capabilities-->Background Modes开关设置成 ON
Modes:勾选 Remote notifications
3、在Targets-->Build Phases-->Link Binary With Libraries
导入UserNotification.framework系统库文件
libstdc++6.0.9
三、在AppDelegate.m文件中设置
1、导入系统库文件
#import
2、设置代理
@interfaceAppDelegate()
3、注册方法,应用注册设置代理
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
[[UIApplication
sharedApplication]registerForRemoteNotifications];
[UNUserNotificationCenter
currentNotificationCenter].delegate=self;;
[self.windowmakeKeyAndVisible];
returnYES;
}
4、
#pragma mark ------------------------------推送处理--------------------------------------------
//app允许接受远程通知以后利用该回调回取token
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken {
NSString*deviceTokenString = [[[[deviceTokendescription]
stringByReplacingOccurrencesOfString:@"<"withString:@""]
stringByReplacingOccurrencesOfString:@">"withString:@""]stringByReplacingOccurrencesOfString:@" "withString:@""];
if(![AppDelegateAPP].deviceToken) {
[AppDelegateAPP].deviceToken= deviceTokenString;
}
CLog(@"本地存储deviceToken = %@", deviceTokenString);
}
//app允许接受远程通知以后token获取失败
- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error {
NSLog(@"DeviceToken获取失败,原因:%@",error);
[AppDelegateAPP].deviceToken=@"c7ef3cb5e31a40a084672d55f46cfd964b9b32ddf62d3b4b0609c4a52837aa41";
}
以上完成应用注册的相关操作,可以用真机获取到设备的deviceToken。
欢迎大家评论交流~
网友评论
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
[center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionCarPlay) completionHandler:^(BOOL granted, NSError *_Nullable error) {
if (!error) {
NSLog(@"request authorization succeeded!");
}
}];
[[UIApplication sharedApplication] registerForRemoteNotifications];
你可以改成这个试试,证书申请的时候要搞清楚该开发环境还是生产环境,如果是安装手机测试包,就是Xcode直接运行这种,用开发环境,如果是上架或者TestFlight测试,用生产环境