那就来讲讲我做推送的辛酸历程吧!
报错:
Register Remote Notifications error:{Error Domain=NSCocoaErrorDomain Code=3000 "未找到应用程序的“aps-environment”的授权字符串" UserInfo=0x17146fdc0 {NSLocalizedDescription=未找到应用程序的“aps-environment”的授权字符串}}
报错翻译: 找不到相关证书
报错原因: 之前的证书为发布证书
解决方案: 重新申请一个带Push属性的调试证书
现在来讲讲注册通知,获取deviceToken的步骤
1⃣️ 去开发者账号申请带有Push属性的证书,网上步骤很多,在这里就不写出来了。
2⃣️来到AppDelegate.m
①在didFinishLaunchingWithOptions注册通知监听
<span style="font-size:18px;">- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
[application registerUserNotificationSettings:settings];
[application registerForRemoteNotifications];
return YES;
}
</span>
<span style="font-size:18px;">//获取DeviceToken成功
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
NSLog(@"DeviceToken: {%@}",deviceToken);
//这里进行的操作,是将Device Token发送到服务端
NSString * str = @"登陆";
UIAlertView * alert = [[UIAlertView alloc]initWithTitle:nil message:[NSString stringWithFormat:@"DeviceToken:%@",deviceToken] delegate:self cancelButtonTitle:nil otherButtonTitles:str.L, nil];
[alert show];
}</span>③注册通知失败的方法 didFailToRegisterForRemoteNotificationsWithError
<span style="font-size:18px;">//注册DeviceToken消息推送失败
-(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{
NSLog(@"Register Remote Notifications error:{%@}",error);
}
</span>后续再更新吧 ╮(╯▽╰)╭
本文分享了作者在iOS应用中实现推送通知的经历,包括解决证书问题、注册通知监听、获取并发送deviceToken到服务端的过程。

2956

被折叠的 条评论
为什么被折叠?



