美文网首页
快速实现APNS

快速实现APNS

作者: 75dfd512edfa | 来源:发表于2016-11-28 22:25 被阅读62次

本文为新手提供快速集成APNS,不为底层实现做赘诉
此集成总结来说分为三步:
1.注册通知
2.接收token
3.接收通知
一.注册通知的方法

//注册远程通知
    if ([[UIDevice currentDevice].systemVersion floatValue] < 8.0)
    {
        UIRemoteNotificationType type = UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound;
        
        [application registerForRemoteNotificationTypes:type];
        
    }
    else
    {
        [[UIApplication sharedApplication] registerForRemoteNotifications];
        UIUserNotificationType type = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;
        
        UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:type categories:nil];
        
        [application registerUserNotificationSettings:setting];
        
    }
#ifdef __IPHONE_8_0
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
    //register to receive notifications
    [application registerForRemoteNotifications];
}
#endif

二.接收token


- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    NSString* newToken = [[[NSString stringWithFormat:@"%@",deviceToken]
                           stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]] stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSLog(@"nsdata:%@\n 字符串token: %@",deviceToken, newToken);// 获取device token
    //将token发送给服务器
    
    //将token存到本地
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:newToken forKey:@"deviceToken"];
    [defaults synchronize];
}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
    NSLog(@"RegistFail %@",error);
}

三.接收消息

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    NSLog(@"userInfo===%@",userInfo);
}

总结:个人认为在开发中需要注意的是:需要ios给服务器提供相应的推送证书(在开发者中心创建,本文不作详细说明)。Xcode8之后尤其注意此处应打开,否则会接收不到token


相关文章

  • 快速实现APNS

    本文为新手提供快速集成APNS,不为底层实现做赘诉此集成总结来说分为三步:1.注册通知2.接收token3.接收通...

  • nodejs APNS

    node-apnnodejs + apn 推送给apns服务器的问题用NODEJS实现APNS

  • TalkingData集成

    TalkingData基于苹果的APNS服务,集成前实现isRegisteredForRemoteNotifica...

  • APNS 相关

    显示实现APNS 的方式有两种,一种是之前的push 证书的管理,另一种就是apns auth key。推送证书有...

  • iOS推送记录

    APNSiOS端代码实现 APNS推送工具 推送工具: https://github.com/shaojianku...

  • APNS与VoIP

    APNS 一、简述APNS APNS全称是Apple Push Notification service(苹果推送...

  • APNS消息推送的实现(完整步骤)

    1. 原理及代码实现 iOS远程推送原理及实现过程 苹果远程推送通知 APNs 详解,官方,iOS | Swift...

  • iOS远程推送(Objective-C & Swift)

    iOS远程推送 APNS远程推送的流程: 1、app 注册到 APNS。2、APNS 下发 devicetoken...

  • iOS APNS

    APNS推送机制 APNS注意事项 1、APNS免费,但需要开发者账号2、APNS不稳定,Apple对消息推送的可...

  • 极光推送(Swift)

    1、使用cocoapod集成 2、注册APNs 3、向苹果发送deviceToken 4、实现极光代理方法 可以在...

网友评论

      本文标题:快速实现APNS

      本文链接:https://www.haomeiwen.com/subject/zitapttx.html