• iphone 推送服务Apple Push Notification Service


    转载iphone 推送服务--Apple Push Notification Service
    (1) You need to create an App ID without .* in the Program Portal (that means one cert for one app)
    (2) Generate a certificate signing request from your Mac's keychain and save to disk
    (3) Upload the CertificateSigningRequest.certSigningRequest to the Program Portal
    (4) Wait for the generation of cert (about 1 min). Download the certificate (aps_developer_identity.cer) from the Program Portal
    (5) Keep (or rename them if you want) these 2 files (steps 2 and 4) in a safe place. You might need the CertificateSigningRequest.certSigningRequest file to request a production cert in the future or renew it again.
    (6) Suppose you have imported the aps_developer_identity.cer to the keychain. Then you have to export these new cert and the private key of this cert (not the public key) and saved as .p12 files.
    (7) Then you use these commands to generate the cert and key in Mac's Terminal for PEM format (Privacy Enhanced Mail Security Certificate)
    openssl pkcs12 -clcerts -nokeys -out cert.pem -in cert.p12
    openssl pkcs12 -nocerts -out key.pem -in key.p12
    (8) The cert.pem and key.pem files will be used by your own program communicating with APNS.
    (9) If you want to remove the passphase of private key in key.pem, do this
    openssl rsa -in key.pem -out key.unencrypted.pem
    Then combine the certificate and key
    cat cert.pem key.unencrypted.pem > ck.pem
    But please set the file permission of this unencrypted key by using chmod 400 and is only readable by root in a sever configuration.
    (10) The testing APNS is at ssl://gateway.sandbox.push.apple.com:2195
    (11) For the source codes to push payload message to the APNS, you can find them in the Developer Forum. This is the one that I used, for php. Run this (after obtaining the device token from the testing device and with iPhone Client program setup)
    php -f apns.php "My Message" 2
    or if you put this php script and the ck.pem in a local web server, you can use this to test
    http://127.0.0.1/apns/apns.php?message=test%20from%20javacom&badge=2&sound=received5.caf
    apns.php Select all
    < ?php $deviceToken = '02da851dXXXXXXXXb4f2b5bfXXXXXXXXce198270XXXXXXXX0d3dac72bc87cd60'; // masked for security reason // Passphrase for the private key (ck.pem file) // $pass = ''; // Get the parameters from http get or from command line $message = $_GET['message'] or $message = $argv[1] or $message = 'Message received from javacom'; $badge = (int)$_GET['badge'] or $badge = (int)$argv[2]; $sound = $_GET['sound'] or $sound = $argv[3]; // Construct the notification payload $body = array(); $body['aps'] = array('alert' => $message);
    if ($badge)
    $body['aps']['badge'] = $badge;
    if ($sound)
    $body['aps']['sound'] = $sound;
    /* End of Configurable Items */
    $ctx = stream_context_create();
    stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
    // assume the private key passphase was removed.
    // stream_context_set_option($ctx, 'ssl', 'passphrase', $pass);
    $fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
    if (!$fp) {
    print "Failed to connect $err $errstr\n";
    return;
    }
    else {
    print "Connection OK\n";
    }
    $payload = json_encode($body);
    $msg = chr(0) . pack("n",32) . pack('H*', str_replace(' ', '', $deviceToken)) . pack("n",strlen($payload)) . $payload;
    print "sending message :" . $payload . "\n";
    fwrite($fp, $msg);
    fclose($fp);
    ?>
    (12) For iPhone Client Program, you need to edit the bundle identifier to the App ID that you created and imported the new provisioning profile for that APP ID to the XCode and iPhone. Then implement the following methods in AppDelegate to Build & Go
    AppDelegate.m Select all
    - (void)applicationDidFinishLaunching:(UIApplication *)application {
    NSLog(@"Registering Remote Notications");
    // For beta 2
    // [[UIApplication sharedApplication] registerForRemoteNotifications];
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)]; // For beta 3
    // other codes here
    }
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    NSLog(@"%@",[[[launchOptions objectForKey:@"UIApplicationLaunchOptionsRemoteNotificationKey"] objectForKey:@"aps"] objectForKey:@"alert"]);
    return YES;
    }
    - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    NSLog(@"deviceToken: %@", deviceToken);
    }
    - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    NSLog(@"Error in registration. Error: %@", error);
    }
    (13) Additional tips
    - The feedback service is currently unavailable.
    - Send your messages to gateway.sandbox.push.apple.com:2195 during the beta period.
    - Devices must be set up as new iPhones in iTunes in order to generate device tokens. Restoring from backup is not currently supported.


    这是我看到得推送服务 包括证书制作 比较详细的开发过程 好东西分享一下

  • 相关阅读:
    GridView“gv_Info”激发了未处理的事件“RowEditing” “RowEditing”
    VS aspx页面在 设计视图 状态时 才可选用 工具 菜单下的 生成本地资源
    愿能与诸位关心的人及时保持互联
    [转]NOD32 與 無法將工作階段狀態要求送至工作階段狀態伺服器 NOD32与asp.net 状态服务
    [转]JavaScript:只能输入数字(IE、FF)
    勿以恶小而为之>致 被烟所包的程序员
    婚姻 一辈子的幸福厮守 请不要多拿彩礼和父母说事
    [文摘20090601]美国和中国老师讲灰姑娘的故事(差距啊~体现得淋漓尽致)
    多语言开发 之 通过基页类及Session 动态响应用户对语言的选择
    javascript的拖放(第1部分)
  • 原文地址:https://www.cnblogs.com/chen1987lei/p/1895213.html
Copyright © 2020-2023  润新知