zoukankan      html  css  js  c++  java
  • 远程通知

    消息推送

     
    /*
     要开发测试消息机制的程序,必须用真机测试
      
     推送消息的类型
     UIRemoteNotificationTypeNone    不接收推送消息
     UIRemoteNotificationTypeBadge   接收图标数字
     UIRemoteNotificationTypeSound   接收音频
     UIRemoteNotificationTypeAlert   接收消息文字
     UIRemoteNotificationTypeNewsstandContentAvailability 接收订阅消息
      
     要想监听到注册的deviceToken需要在苹果的开发者中心,进行一些设置工作才可以。
     */
     
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        // 设置应用程序能够接收APNS推送的消息
        [application registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound];
         
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        self.window.backgroundColor = [UIColor whiteColor];
        [self.window makeKeyAndVisible];
         
        return YES;
    }
     
    #pragma mark - 获取DeviceToken
    - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
    {
        NSLog(@"%@", deviceToken);
        // 1. 从系统偏好取之前的token
        NSData *oldToken = [[NSUserDefaults standardUserDefaults]objectForKey:@"deviceToken"];
        // 2. 新旧token进行比较
        if (![oldToken isEqualToData:deviceToken]) {
            // 3. 如果不一致,保存token到系统偏好
            [[NSUserDefaults standardUserDefaults]setObject:deviceToken forKey:@"deviceToken"];
             
            // 4. 使用post请求传输新旧token至服务器
            // 1) url
            // 具体的URL地址以及POST请求中的参数和格式,是由公司的后端程序员提供的
            // 2) request POST body(包含新旧token的数据)
            // 3) connection 的异步
        }
    }
     
    远程通知
     
    /**
     远程消息推送必须在真机上运行!
     */
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        // 需要告诉苹果的服务器,当前应用程序需要接收远程通知
        [application registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound];
         
        return YES;
    }
     
    #pragma mark - 获取到设备的代号(令牌)
    // 接收到苹果返回的设备代号
    - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
    {
        // 第一次运行获取到DeviceToken时间会比较长!
        NSLog(@"%@", deviceToken);
         
        // 将deviceToken转换成字符串,以便后续使用
        NSString *token = [deviceToken description];
        NSLog(@"description %@", token);
         
        // =======================================================
        // 如果DeviceToken发生变化,需要通知服务器
        // 每次都记录住从服务器获取到得DeviceToken
        // 再次获取时进行比对
        // 从偏好设置取出当前保存的Token
        NSString *oldToken = [[NSUserDefaults standardUserDefaults] objectForKey:@"DeviceToken"];
         
        // 当Token发生变化时,提交给服务器保存新的Token
    //    if (![oldToken isEqualToString:token]) {
    //        
    //        // 将deviceToken通过Post请求,提交给自己的服务器即可!
    //        // 发送Post请求
    //        NSURL *url = [NSURL URLWithString:@"公司后台服务器的网址"];
    //        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.f];
    //        
    //        request.HTTPMethod = @"POST";
    //        request.HTTPBody = @"转换后的设备ID以及其他信息[之前的Token]";
    //        
    //        // SQL: update t_deviceTable set token = newToken where token = oldToken;
    //        
    //        // 同步:必须执行完才能继续
    //        // 异步:直接交给其他线程工作,不干扰主线程工作,用户也感觉不到延迟
    //        [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
    //            // 偷偷的将用户信息传送到公司的服务器
    //        }];
    //    }
         
        // 将Token保存至系统偏好
        [[NSUserDefaults standardUserDefaults] setObject:token forKey:@"DeviceToken"];
    }
  • 相关阅读:
    PHP基础学习笔记(一)
    安装wampserver之后,浏览器中输入localhost页面显示IIS7解决办法
    HTML5常识总结(一)
    AngularJs中的服务
    AngularJs中的directives(指令part1)
    Happy Number——LeetCode
    Binary Tree Zigzag Level Order Traversal——LeetCode
    Construct Binary Tree from Preorder and Inorder Traversal——LeetCode
    Construct Binary Tree from Inorder and Postorder Traversal——LeetCode
    Convert Sorted Array to Binary Search Tree——LeetCode
  • 原文地址:https://www.cnblogs.com/OIMM/p/4889684.html
Copyright © 2011-2022 走看看