苹果APNS前及后端搭建流程记录

APNS,全称为苹果推送通知服务Apple Push Notification Service)

在iOS开发中消息的推送是利用自身的服务器,通知苹果的apns服务,然后在推送到具体的机器上。网上已有很多的具体配置过程,但是每个方法都不尽相同。以下方法经过自己的测试,完全可行。

首先在appid中申请开通apns的服务,申请过程中需要上传CSR文件,完成后下载push SLL的证书,并导入到本机电脑

紧接着申请provisioning,如果之前已经开通过provisioning,需要重新申请provisioning。并导入电脑以及测试真机。

第三步,也是最重要的就是制作pem文件具体过程如下

1)在Keychain Access.app里选定这个新证书(Apple Development Push Services*),导出到桌面,保存为Certificates.p12.过程中需要输入密码,记录下该密码,后面会用到

  2)然后运行如下命令:

   1.     openssl pkcs12 -clcerts -nokeys -out cert.pem -in Certificates.p12
   2.     openssl pkcs12 -nocerts -out key.pem -in Certificates.p12
   3.     openssl rsa -in key.pem -out key.unencrypted.pem
   4.     cat cert.pem key.unencrypted.pem > ck.pem

<?php

$deviceToken = '46c1015f 0d11cb7a 93680da6 4dfd4f96 a76beb0a 35bdf012 a123d96e 78338dd9'; // 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 Robert.si';
$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 $errstrn";
return;
}
else {
print "Connection OKn";
}
$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);
?>

在app 的delegate中添加如下代码

#import "ApplePushNotificationAppDelegate.h"

#import "ApplePushNotificationViewController.h"

 

@implementation ApplePushNotificationAppDelegate

 

@synthesize window;

@synthesize viewController;

 

- (void)applicationDidFinishLaunching:(UIApplication*)application {   

   [window addSubview:viewController.view];

   [window makeKeyAndVisible];

 

    NSLog(@"Registeringfor push notifications...");   

    [[UIApplication sharedApplication]

       registerForRemoteNotificationTypes:

        (UIRemoteNotificationTypeAlert |

        UIRemoteNotificationTypeBadge |

        UIRemoteNotificationTypeSound)];

}

 

- (void)application:(UIApplication*)appdidRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {

    NSString *str = [NSString

       stringWithFormat:@"Device Token=%@",deviceToken];

    NSLog(str);

}

 

- (void)application:(UIApplication*)appdidFailToRegisterForRemoteNotificationsWithError:(NSError *)err {

    NSString *str = [NSStringstringWithFormat: @"Error: %@", err];

    NSLog(str);   

 

}

 

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

    for (id key in userInfo) {

       NSLog(@"key: %@, value: %@", key, [userInfo objectForKey:key]);

    }   

}

- (void)dealloc {

   [viewController release];

   [window release];

   [super dealloc];

}

@end

苹果APNS前及后端搭建流程记录》上有11条评论

  1. DreamyChrome64z

    http://www.rcdrift.pl/wyposazenie-samochodu
    Looking for a new or used vehicle might be a difficult method unless you know what you are actually undertaking. By teaching yourself about automobile purchasing prior to deciding to head to the dealer, you possibly can make points less difficult yourself. The following advice may help your next purchasing trip become more satisfying.

    Always bring a auto technician along when searching for a fresh automobile. Automobile retailers are notorious for selling lemons and you do not wish to be their up coming target. When you can not have a auto mechanic to consider vehicles along with you, at least ensure that you have him evaluate your closing decision before you purchase it.

    Know your restrictions. Before you start buying for your upcoming automobile or van, choose how much you can manage to shell out, and adhere to it. Don’t forget to add curiosity about your estimations. You will definitely pay all around 20 percent as a payment in advance as well, so prepare yourself.

    Before going to a dealership, know what kind of motor vehicle you need. Research all of you options ahead of shopping to help you figure out what works well with your budget and loved ones requirements. Seek information to determine just how much you should pay for the potential car.

    Before you sign any commitment spend some time to study every single line, including the fine print. When there is nearly anything detailed you do not comprehend, will not sign till you get an respond to that you just fully grasp. Unsavory salesmen can make use of an agreement to put several fees that had been not discussed.

    Should you maintain the previous advice at heart the very next time that you simply go looking for a car, you may be more likely to get a good offer. Investing in a automobile does not have to become a headache. Simply use the ideas using this article and you will have the auto you would like in a good price.

    回复

发表评论

电子邮件地址不会被公开。


*