在SAE使用Apple Push Notification Service服务开发iOS应用

来源:互联网 发布:重庆市知行卫校 编辑:程序博客网 时间:2024/06/07 11:59

1,在iOS开发者中心: iOS ProvisioningPortal 创建一个AppID,如图:

在SAE使用Apple <wbr>Push <wbr>Notification <wbr>Service服务开发iOS应用

 

在SAE使用Apple <wbr>Push <wbr>Notification <wbr>Service服务开发iOS应用

2,生成iOS Push Service证书,如图:

在SAE使用Apple <wbr>Push <wbr>Notification <wbr>Service服务开发iOS应用

在SAE使用Apple <wbr>Push <wbr>Notification <wbr>Service服务开发iOS应用

 

在SAE使用Apple <wbr>Push <wbr>Notification <wbr>Service服务开发iOS应用

按照上述提示操作:

在SAE使用Apple <wbr>Push <wbr>Notification <wbr>Service服务开发iOS应用

在SAE使用Apple <wbr>Push <wbr>Notification <wbr>Service服务开发iOS应用

在SAE使用Apple <wbr>Push <wbr>Notification <wbr>Service服务开发iOS应用

 

在SAE使用Apple <wbr>Push <wbr>Notification <wbr>Service服务开发iOS应用


回到网站,


在SAE使用Apple <wbr>Push <wbr>Notification <wbr>Service服务开发iOS应用

在SAE使用Apple <wbr>Push <wbr>Notification <wbr>Service服务开发iOS应用

 

选择刚才生成的文件,并上传:

在SAE使用Apple <wbr>Push <wbr>Notification <wbr>Service服务开发iOS应用





在SAE使用Apple <wbr>Push <wbr>Notification <wbr>Service服务开发iOS应用

点击“Download”下载iOS Push Service证书文件


在SAE使用Apple <wbr>Push <wbr>Notification <wbr>Service服务开发iOS应用

3,导入证书文件到keychain:双击即可


在SAE使用Apple <wbr>Push <wbr>Notification <wbr>Service服务开发iOS应用

4,生成ck.pem

导出cert.p12
在SAE使用Apple <wbr>Push <wbr>Notification <wbr>Service服务开发iOS应用

在SAE使用Apple <wbr>Push <wbr>Notification <wbr>Service服务开发iOS应用

导出key.p12:


在SAE使用Apple <wbr>Push <wbr>Notification <wbr>Service服务开发iOS应用

在SAE使用Apple <wbr>Push <wbr>Notification <wbr>Service服务开发iOS应用

得到这样两个文件:


在SAE使用Apple <wbr>Push <wbr>Notification <wbr>Service服务开发iOS应用

 

接下来打开终端:

输入命令:openssl pkcs12 -clcerts -nokeys -out cert.pem -incert.p12

 

在SAE使用Apple <wbr>Push <wbr>Notification <wbr>Service服务开发iOS应用

 

然后输入命令:openssl pkcs12 -nocerts -out key.pem-in key.p12


在SAE使用Apple <wbr>Push <wbr>Notification <wbr>Service服务开发iOS应用

 

最后合并成一个ck.pem文件:

输入命令:cat cert.pem key.pem > ck.pem


在SAE使用Apple <wbr>Push <wbr>Notification <wbr>Service服务开发iOS应用

得到一个ck.pem文件:


在SAE使用Apple <wbr>Push <wbr>Notification <wbr>Service服务开发iOS应用

5, 生成并安装Profile文件:


在SAE使用Apple <wbr>Push <wbr>Notification <wbr>Service服务开发iOS应用

在SAE使用Apple <wbr>Push <wbr>Notification <wbr>Service服务开发iOS应用

在SAE使用Apple <wbr>Push <wbr>Notification <wbr>Service服务开发iOS应用

6, 上传ck.pem到SAE,如图


在SAE使用Apple <wbr>Push <wbr>Notification <wbr>Service服务开发iOS应用

在SAE使用Apple <wbr>Push <wbr>Notification <wbr>Service服务开发iOS应用

7, 客户端程序:

设置profile:


在SAE使用Apple <wbr>Push <wbr>Notification <wbr>Service服务开发iOS应用

info.plist中设置Bundleidentifier


在SAE使用Apple <wbr>Push <wbr>Notification <wbr>Service服务开发iOS应用

将app注册notification里面, 并从APNS上获取测试机的deviceToken, 代码如下:

- (BOOL)application:(UIApplication *)applicationdidFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

 ......  

   

   //注册到消息中心:   

   [[UIApplication sharedApplication]

    registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge|

UIRemoteNotificationTypeSound |

UIRemoteNotificationTypeAlert |

                                        UIRemoteNotificationTypeNewsstandContentAvailability)];

       

    returnYES;

}

#pragma mark -

#pragma mark APNS



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

 

    //获得device token

   NSLog(@"deviceToken: %@", deviceToken);

   

    //获得唯一标示

NSLog(@"uniqueIdentifier: %@", [[UIDevice currentDevice]uniqueIdentifier]);

   

  

}



- (void)application:(UIApplication *)applicationdidFailToRegisterForRemoteNotificationsWithError:(NSError *)error{

 

   NSLog(@"Error in registration. Error: %@", error);

}





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

 

 

   

    if([[userInfo objectForKey:@"aps"] objectForKey:@"alert"] != nil){

       UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"通知"

                                                       message:[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]

                                                      delegate:self

                                             cancelButtonTitle:@"确定"

                                             otherButtonTitles:nil];

       [alert show];

       [alert release];

    }

}



8, 服务器端程序:



<?php



include_once("saeapns.class.php");



//许可证id

$cert_id = 1;



//设备令牌

$device_token = "xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxxxxxxxxxx xxxxxxxx xxxxxxxx";

 

$message = "测试消息 from SAE: " . date('Y-m-d H:i:s');



//消息体,格式详见iOS官方文档

$body = array(

'aps' => array('alert' => $message, 'badge' => 1, 'sound' =>'in.caf')

);



$apns = new SaeAPNS();

$result = $apns->push($cert_id , $body ,$device_token);

 

if ($result != false) {



echo '发送成功';

} else {

 

echo '发送失败';

var_dump($apns->errno(),$apns->errmsg());

}

?>





9, 手机收到通知效果:


在SAE使用Apple <wbr>Push <wbr>Notification <wbr>Service服务开发iOS应用

实例代码下载地址:

http://vdisk.weibo.com/s/SbY2



原创粉丝点击