iOS - 注册远程推送通知

本文分享了作者在iOS应用中实现推送通知的经历,包括解决证书问题、注册通知监听、获取并发送deviceToken到服务端的过程。

那就来讲讲我做推送的辛酸历程吧!


报错:

 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>


②在didRegisterForRemoteNotificationsWithDeviceToken注册通知

<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>


后续再更新吧 ╮(╯▽╰)╭ 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值