iOS原生APNS推送

现在好多人都在用极光推送,但是本人感觉APNS的代码比极光还要简单。既然是推送就必须要有证书,我在这里就只说APNS的代码吧,如果有需要证书申请的过程那就看我推送证书的那篇文章吧。

(1)首先注册通知(else里会报黄,可以不要else里的)

<span style="font-family:Times New Roman;font-size:14px;">- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    // 注册远程通知
    if(MySystemVersion >= 8.0){
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert categories:nil];
        [application registerUserNotificationSettings:settings];
    }else{
        UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
        [application registerForRemoteNotificationTypes:myTypes];
    }
    return YES;
}
</span>
(2)注册通知代理方法
<span style="font-family:Times New Roman;font-size:14px;">// 注册通知成功
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
    [application registerForRemoteNotifications];
}

// 注册通知失败
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *) error {
    NSLog(@"RegisterForRemoteNotificationsError = %@",error);
}
</span>

(3)获取deviceToken(把deviceToken传给后台,后台是根据deviceToken推送到具体人的)

<span style="font-family:Times New Roman;font-size:14px;">// 获取deviceToken
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    NSLog(@"deviceToken = %@",deviceToken);
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:deviceToken forKey:@"deviceToken"];
}</span>
(4)获取通知消息(userInfo是一个字典,里面有消息内容,推送声音和角标数。当然这些都是后台传给你的,你需要什么参数你跟后台说,他会传给你的)
<span style="font-family:Times New Roman;font-size:14px;">// 获取通知消息
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
    NSLog(@"didReceiveRemoteNotificationUserInfo = %@",userInfo);
    if (userInfo[@"type"]) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"ChangeTabbarBadge" object:nil userInfo:userInfo];
    } else {
        NSLog(@"接受通知失败");
    }
}</span>
(4.1)如果写这个代理方法,上边的(4)就会不走(我也不知道这两个有什么不同)
<span style="font-family:Times New Roman;font-size:14px;">- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    NSLog(@"userInfoBlock = %@",userInfo);
    if (userInfo[@"type"]) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"ChangeTabbarBadge" object:nil userInfo:userInfo];
    } else {
        NSLog(@"接受通知失败");
    }
}</span>
大家注意!!!!!!!!

如果是点击通知栏进入应用程序的话会走(4)和(4.1)的代理方法。如果要是点图标(icon)进应用程序的话这两个代理方法是不会走的!

下面是解决方法:

(5)点击图标(icon)进入应用程序只能是调一下接口,然后用通知把字典传给你UI需要的地方

<span style="font-family:Times New Roman;font-size:14px;">// 应用程序进入前台的时候调用
-(void)applicationWillEnterForeground:(UIApplication *)application{
    if (application.applicationIconBadgeNumber != 0) {
        NSDictionary *aps = @{@"alert" : @"U6",
                              @"badge" : @"7",
                              @"sound" : @"default"};
        
        NSDictionary *userInfo = @{@"aps" : aps, @"type" : @"Tools"};
        
        NSLog(@"applicationWillEnterForegroundUserInfo = %@",userInfo);
        
        if (userInfo[@"type"]) {
            [[NSNotificationCenter defaultCenter] postNotificationName:@"ChangeTabbarBadge" object:nil userInfo:userInfo];
        } else {
            NSLog(@"接受通知失败");
        }
    }
}</span>

(6)在你的UITabBarController获取通知,执行方法(你可以在你需要的Controller获取通知,执行方法)

<span style="font-family:Times New Roman;font-size:14px;">- (void)viewDidLoad {
    [super viewDidLoad];
    // 接受ChangeTabbarBadge通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ChangeTabbarBadge:) name:@"ChangeTabbarBadge" object:nil];
}
</span>
(7)执行方法修改icon的角标和tabbarItem的角标
<span style="font-family:Times New Roman;font-size:14px;">- (void)ChangeTabbarBadge:(NSNotification *)notification {
    // 还原icon角标个数
    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
    // 修改你的tabbar的角标
    NSDictionary *userInfo = notification.userInfo;
    if ([userInfo[@"type"] isEqualToString:@"News"]) {
        NSString *badge = [NSString stringWithFormat:@"%@",notification.userInfo[@"aps"][@"badge"]];
        [self.newsNC.tabBarItem setBadgeValue:badge];
    } else if ([userInfo[@"type"] isEqualToString:@"Account"]) {
        NSString *badge = [NSString stringWithFormat:@"%@",notification.userInfo[@"aps"][@"badge"]];
        [self.accountNC.tabBarItem setBadgeValue:badge];
    } else if ([userInfo[@"type"] isEqualToString:@"Tools"]) {
        NSString *badge = [NSString stringWithFormat:@"%@",notification.userInfo[@"aps"][@"badge"]];
        [self.toolsNC.tabBarItem setBadgeValue:badge];
    } else if ([userInfo[@"type"] isEqualToString:@"We"]) {
        NSString *badge = [NSString stringWithFormat:@"%@",notification.userInfo[@"aps"][@"badge"]];
        [self.aboutNC.tabBarItem setBadgeValue:badge];
    }
}</span>




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值