首页 > 代码库 > 在mac上配置push notification的问题

在mac上配置push notification的问题

在代码的deletegater中写:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge
                                                                           | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
    return YES;
}

- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    NSLog(@"my device token=%@",deviceToken);
}

- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err
{
    NSString *str = [NSString stringWithFormat: @"Error: %@", err];
    NSLog(@"%@",str);
}

然后运行在真机上,得到机器的deviceToken


然后在生成的证书上双击Certificates.p12,点击添加,输入密码



在xcode中配置:



然后在mac上用终端到证书所在的文件夹,先看看mac是否能连上苹果服务器:

$ telnet gateway.sandbox.push.apple.com 2195
Trying 17.172.232.226...
Connected to gateway.sandbox.push-apple.com.akadns.net.
Escape character is '^]'.

如果出现上面的内容,说明没问题,如果不是,看看是不是防火墙什么的问题,然后接着把p12和aps_development.cer文件转为pem文件,把转完的两个pem文件合并为ck.pem

$ openssl x509 -in aps_development.cer -inform der -out PushChatCert.pem
$ openssl pkcs12 -nocerts -out PushChatKey.pem -in PushChatKey.p12
Enter Import Password: 
MAC verified OK
Enter PEM pass phrase: 
Verifying - Enter PEM pass phrase: 
$ cat PushChatCert.pem PushChatKey.pem > ck.pem

<img src=http://www.mamicode.com/"http://img.blog.csdn.net/20140628153305687" alt="" />
看看是否返回成功,如果返回一大堆字符串说明是成功的

最后到http://www.raywenderlich.com/32960/apple-push-notification-services-in-ios-6-tutorial-part-1

下载那个simplepush.php文件,注意

<?php

// Put your device token here (without spaces):
$deviceToken ='d4bb74c6f54d5ff6a2fbb45ea9b083738bdba936a690138f58b4a8b5dc4c65e84';

// Put your private key's passphrase here:
$passphrase = '111111';

// Put your alert message here:
$message = 'My first push notification!';
这个$passphrace的mac会自动转成中文的引号,导致连接不成功,报下面的错误,
kouyuukenmatoMacBook-Pro:Archive2 kouyuuken$ php simplepush.php 

Warning: stream_socket_client(): Unable to set private key file `/Users/kouyuuken/Desktop/ios项目/Archive2/ck.pem' in /Users/kouyuuken/Desktop/ios项目/Archive2/simplepush.php on line 21

Warning: stream_socket_client(): failed to create an SSL handle in /Users/kouyuuken/Desktop/ios项目/Archive2/simplepush.php on line 21

Warning: stream_socket_client(): Failed to enable crypto in /Users/kouyuuken/Desktop/ios项目/Archive2/simplepush.php on line 21

Warning: stream_socket_client(): unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Unknown error) in /Users/kouyuuken/Desktop/ios项目/Archive2/simplepush.php on line 21
Failed to connect: 0 

所以必须自己改成英文的引号

如果是成功,会出现以下信息,device也会收到notification

kouyuukenmatoMacBook-Pro:Archive2 kouyuuken$ php simplepush.php 
Connected to APNS
Message successfully delivered