zoukankan      html  css  js  c++  java
  • 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


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

  • 相关阅读:
    CSS3:三个矩形,一个宽200px,其余宽相等且自适应满铺
    pidera安装node.js(树莓派)
    深入JavaScript模块化编程
    c# 多显示器设置主屏幕(Set primary screen for multiple monitors)
    c# 获取移动硬盘信息、监听移动设备的弹出与插入事件
    C# 弹出USB外接硬盘(U盘)
    log4net 自定义Appender
    Scrum 思考
    监控浏览器关闭事件
    判断地图的点是否在面内 腾讯地图
  • 原文地址:https://www.cnblogs.com/xiangrikui/p/5280116.html
Copyright © 2011-2022 走看看