ios: APNS 配置

AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil] autorelease];
    } else {
        self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController_iPad" bundle:nil] autorelease];
    }
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    
    NSLog(@"Registerng For push notifications");
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
    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);
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    
    NSLog(@" 收到推送消息 : %@",[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]);
    if ([[userInfo objectForKey:@"aps"] objectForKey:@"alert"]!=NULL) {
        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"推送通知"
                                                        message:[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]
                                                       delegate:self
                                              cancelButtonTitle:@" 关闭"
                                              otherButtonTitles:@" 更新状态",nil];
        [alert show];
        [alert release];
    }
}

1: 创建AppId,设置develop ssl  configure
2: 上传request文件,下载配置好的cer文件,双击安装
3: 创建provisioning profile,下载.mobileprovision
4:配置xcode:info-》budle identifier->com.xxxx.xxxx
5:  修改code signing 
可以运行。调试文件:
meBaby,引入cer文件。更改.m的deviceToken
php 服务器配置:
二、生成PHP端推送消息到Apple服务器时需要的证书文件
1.选定推送服务证书(Apple Development Push Services*),导出到桌面,保存为Certificates.p12。

2.在终端中运行如下命令:

[xhtml] view plaincopy
openssl pkcs12 -clcerts -nokeys -out cert.pem -in Certificates.p12 
openssl pkcs12 -nocerts -out key.pem -in Certificates.p12 
openssl rsa -in key.pem -out key.unencrypted.pem 
cat cert.pem key.unencrypted.pem > ck.pem 

ck.pem文件则是PHP推送消息时所使用的证书文件。

3:服务器端代码:
<?php
$deviceToken = '6974ac11 870e09fa 00e2238e 8cfafc7d 2052e342 182f5b57 fabca445 42b72e1b';
$pass = '123456';   // Passphrase for the private key (ck.pem file)
 
// Get the parameters from http get or from command line
$message = $_GET['message'] or $message = $argv[1] or $message = 'A test message from worldcup';
$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);
 
// connect to apns
$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<br/>";
}
 
// send message
$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);
?>

请 求一次 http://127.0.0.1/apns/apns.php?message=A%20test%20message%20from%20localhost&badge=2&sound=received5.caf就 会向APNS进行一次推送。我的请求结果如下:
Connection OK
Sending message :{"aps":{"alert":"A test message from localhost","badge":2,"sound":"received5.caf"}}

你可能感兴趣的:(ios)