iOS Coding

iOS开发-远程推送

2016-11-01  本文已影响16人  看我的大白眼

远程通知的原理

配置证书

</div>

远程通知实现流程

// AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    ///  注册通知
    ///  判断当前设备
    if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0) {
        // iOS8 之后的版本
        //1. 配置通知的类型
        /*
         UIUserNotificationTypeBadge
         UIUserNotificationTypeSound
         UIUserNotificationTypeAlert
         */
        UIUserNotificationType type = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
        //2. 配置settings
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type categories:nil];
        //3. 注册settings
        [application registerUserNotificationSettings:settings];
        
        //4. 注册远程通知
        [application registerForRemoteNotifications];
        
    } else {
        //1. 配置通知的类型
        /**
         UIRemoteNotificationTypeBadge
         UIRemoteNotificationTypeSound
         UIRemoteNotificationTypeAlert
         */
        UIRemoteNotificationType type = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert;
        //2. 注册远程通知
        [application registerForRemoteNotificationTypes:type];
        
    }
    return YES;
}
#pragma mark - 当完成注册远程通知时,会调用的方法,在这里返回deviceToken

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    
    //3c48ae8d f6e2c1ff 473c84ea edcfff3d 9dd3beee 30eefbcb e5158680 80da7238
    NSLog(@"%@",deviceToken);
    //deviceToken发送到自己的服务器做存储
}

1.PushMeBaby

我们要借助PushMeBaby模拟服务器,来给我们的APP推送消息, PushMeBaby配置要做如下配置:
<div align = center>

pushMeBaby
</div>
2.代码实现
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //  注册通知
    ///  判断当前设备
    if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0) {
        // iOS8 之后的版本
        //1. 配置通知的类型
        UIUserNotificationType type = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
        //2. 配置settings
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type categories:nil];
        //3. 注册settings
        [application registerUserNotificationSettings:settings];
        
        //4. 注册远程通知
        [application registerForRemoteNotifications];
        
    } else {
        //1. 配置通知的类型
        UIRemoteNotificationType type = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert;
        //2. 注册远程通知
        [application registerForRemoteNotificationTypes:type];
        
    }
    //程序退出的时候 我们可以在这里拿到远程通知的内容
    //launchOptions是一个字典
    if (launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]) {
        NSDictionary *remoteKey = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];
        NSLog(@"%@",remoteKey);
    }

    return YES;
}

#pragma mark - 当完成注册远程通知时,会调用的方法,在这里返回deviceToken
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    
    //3c48ae8d f6e2c1ff 473c84ea edcfff3d 9dd3beee 30eefbcb e5158680 80da7238
    NSLog(@"%@",deviceToken);
    //deviceToken发送到自己的服务器做存储
}

#pragma mark - 当接受到远程推送的值得时候回调用
// 此方法无论前台/后台/退出的情况下都会调用 iOS7 新增的
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler {
    
    // 逻辑处理
    NSLog(@"userInfo: %@",userInfo);
    // 必须调用的
    completionHandler(UIBackgroundFetchResultNewData);
}


3.控制台打印

2016-07-16 15:39:47.611 mall[8118:1494303] userInfo: {
    aps =     {
        alert = "\U4eca\U5929\U505a\U4e2a\U56db\U6709\U9752\U5e74.";
        badge = 1;
    };
}

4.真机效果
<div align = center>


真机

</div>

JPush远程推送设置

请看下一篇博客介绍 iOS开发-极光远程推送

上一篇 下一篇

猜你喜欢

热点阅读