• 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


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

  • 相关阅读:
    Python读写ini文件
    MySQL 让主键id 从1开始自增
    python 找字符串中所有包含字符的下标
    centos7防火墙命令
    如何将npm升级到最新版本
    将 Npm 的源替换成淘宝的源
    MySQL 时间格式化/ MySQL DATE_FORMAT
    Python中crypto模块进行AES加密和解密
    windows环境下python3安装Crypto
    Nginx+PHPSTORM+Xdebug 配置
  • 原文地址:https://www.cnblogs.com/xiangrikui/p/5280116.html
Copyright © 2020-2023  润新知