• iOS 远程推送


    前期准备工作:到苹果开发者网站上注册AppID、推送证书、描述性证书

    代码段实现:

     if ([application respondsToSelector:@selector(isRegisteredForRemoteNotifications)]) {
            //IOS8
            //创建UIUserNotificationSettings,并设置消息的显示类类型
            UIUserNotificationSettings *notiSettings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound) categories:nil];
            
            [application registerUserNotificationSettings:notiSettings];
        }else{
           
            //ios7.0及以下
            [application registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge                                       |UIRemoteNotificationTypeSound    |UIRemoteNotificationTypeAlert)];
        
        }

    代理方法实现

    //会接收来自苹果服务器给你返回的deviceToken,然后你需要将它添加到你本地的推送服务器上。(很重要,决定你的设备能不能接收到推送消息)。
    - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)pToken{
        //注册成功,将deviceToken保存到应用服务器数据库中,在应用服务器端需要
        NSLog(@"---Token--%@", pToken);
       
    }
    //当注册失败时,触发此函数
    - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{
        
        NSLog(@"Regist fail%@",error);
    }
    
    
    //
    - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
        //处理推送消息
        NSLog(@"userInfo == %@",userInfo);
        NSString *message = [[userInfo objectForKey:@"aps"]objectForKey:@"alert"];
        
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定",nil];
        
        [alert show];
    }

    2.PHP服务端

    将simplepush.php这个推送脚本也放在push文件夹中

        <?php  
          
        // ??????????deviceToken???????????????  
        $deviceToken = 'c95f661371b085e2517b4c12cc76293522775e5fd9bb1dea17dd80fe85583b41';  
          
        // Put your private key's passphrase here:  
        $passphrase = 'abc123';  
          
        // Put your alert message here:  
        $message = 'My first push test!';  
          
        ////////////////////////////////////////////////////////////////////////////////  
          
        $ctx = stream_context_create();  
        stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');  
        stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);  
          
        // Open a connection to the APNS server  
        //??????????  
         //$fp = stream_socket_client(?ssl://gateway.push.apple.com:2195?, $err, $errstr, 60, //STREAM_CLIENT_CONNECT, $ctx);  
        //?????????????appstore??????  
        $fp = stream_socket_client(  
        'ssl://gateway.sandbox.push.apple.com:2195', $err,  
        $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);  
          
        if (!$fp)  
        exit("Failed to connect: $err $errstr" . PHP_EOL);  
          
        echo 'Connected to APNS' . PHP_EOL;  
          
        // Create the payload body  
        $body['aps'] = array(  
        'alert' => $message,  
        'sound' => 'default'  
        );  
          
        // Encode the payload as JSON  
        $payload = json_encode($body);  
          
        // Build the binary notification  
        $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;  
          
        // Send it to the server  
        $result = fwrite($fp, $msg, strlen($msg));  
          
        if (!$result)  
        echo 'Message not delivered' . PHP_EOL;  
        else  
        echo 'Message successfully delivered' . PHP_EOL;  
          
        // Close the connection to the server  
        fclose($fp);  
        ?>  

    deviceToken填写你接收到的token,passPhrase则填写你的ck.pem设置的密码。

    此刻就是见证奇迹的时候了

    使用终端进入到push文件夹,在终端输入 php simplepush.php


    若显示以上提示则表示推送成功了。

  • 相关阅读:
    window+Apache+php+mysql注意事项
    【转】QRCode二维码的应用心得
    如何创建和配置服务器证书进行SSL Relay
    Citrix 实践中的问题及解决
    web.config中httpRunTime的属性
    [原]IE9 DOM的自定义属性问题
    添加图片到数据库
    Windows server 2008安装企业CA证书服务
    EXTJS Date 的转换格式化字符
    Extjs 4.1 学习笔记(一)(proxy,loader,treestore)
  • 原文地址:https://www.cnblogs.com/xiangrikui/p/5280116.html
Copyright © 2020-2023  润新知