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);
?>

  

 

 

原文地址:https://www.cnblogs.com/dongfangchun/p/5549637.html