zoukankan      html  css  js  c++  java
  • iOS推送流程

    1. 在apple开发者帐号上创建一个BundleID,创建证书或者Xcode上都是用这个BundleID(例如com.mycompany.pushDemo)

    2. 代码层面:

        在capability里面将pushNotification设置为ON。

        在Appdelegate里面的didfinishLaunching。。。添加代码

        //推送Notification

        if ([application respondsToSelector:@selector(isRegisteredForRemoteNotifications)])

        {

            UIUserNotificationSettings *notiSettings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIRemoteNotificationTypeSound) categories:nil];

            [application registerUserNotificationSettings:notiSettings];

        } else{ // ios7

            [application registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |UIRemoteNotificationTypeSound |UIRemoteNotificationTypeAlert)];

        }

        [application registerForRemoteNotifications];

        

     

    再添加回调函数

     

    - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)pToken{

        const unsigned char *pBytes = pToken.bytes;

        NSMutableString *strToken = [[NSMutableString alloc]init];

        for (int iloop = 0; iloop < pToken.length; iloop++) {

            [strToken stringByAppendingFormat:@"%02x",pBytes[iloop]];

        }

        NSLog(@"token is : %@",[strToken copy]);

    }

     

    - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{

        

        NSLog(@"userInfo == %@",userInfo);

    }

     

    - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{

        

        NSLog(@"Regist fail%@",error);

    }

     

    3. 在“钥匙串访问”-》“证书助理”-》“从证书颁发机构请求证书”  将其保存到磁盘为CertificateSigningRequest.certSigningRequest

    4. 在官网appid中edit中,勾选上PushNotification,然后在下一步中点击“create certificate”,选择上面的CertificateSigningRequest.certSigningRequest文件。生成一个aps_dev.cer

    5. 双击aps_dev.cer,此时要是串会多处一个证书,将这个证书保存为*.p12文件。

    6. 命令行中: openssl pkcs12 -in push_dev.p12 -out push_dev.pem -nodes

       这个push_dev.pem就是关键的文件。

    通过php脚本运行,就可以发送了。

     

    <?php
    
    // ??????????deviceToken???????????????
    $deviceToken = '????????';
    
    // Put your private key's passphrase here:
    $passphrase = '*****';
    
    // 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);
    ?>
    

      

     

     

  • 相关阅读:
    网页加速的14条优化法则 网站开发与优化
    .NET在后置代码中输入JS提示语句(背景不会变白)
    C语言变量声明内存分配
    SQL Server Hosting Toolkit
    An established connection was aborted by the software in your host machine
    C语言程序设计 2009春季考试时间和地点
    C语言程序设计 函数递归调用示例
    让.Net 程序脱离.net framework框架运行
    C语言程序设计 答疑安排(2009春季 110周) 有变动
    软件测试技术,软件项目管理 实验时间安排 2009春季
  • 原文地址:https://www.cnblogs.com/dongfangchun/p/5549637.html
Copyright © 2011-2022 走看看