APNS的Notes集合

apple 提供了详细的代码文档为iphone推送通知服务做示范,但提示不是很具体;倒是如何实现和处理alert在设备app上讲述比较详细. 作为service,需要和apple的服务器交互,device只要维护和apns的一个连接,节省电池

前提条件:

使用SSL证书,这个就涉及不同的服务器不同的配置了,例如windows/linux的SSL连接socket的如何实现?

逻辑条件判断需要发送的消息,简单一点就是每10min发送一条

组合每条message的payload/ 按照一定的格式,其实格式也不是很多种

断开与apns的链接

Device token

定位符号,APNS首先为每一个程序分配了一token,然后你的服务器要记录所有安装此app的token。token看起来是这样的:

c9d4c07c fbbc26d6 ef87a44d 53e16983 1096a5d5 fd825475 56659ddd f715defc

HTTP API call,在app启动的时候,我们调用必要的方法,并且注册到我们的服务service上面,这样用户/设备和token就关联了/

====客户端的测试工作,当没有服务器端业务,甚至服务器没有,也可以进行以下测试,单也要真机  

//when the application successfully registers with Apple Push Service (APS)....

-(void) application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken

{

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

    /*

     token:<2d0938f4 f6feb164 a5ca07d6 1e8c043c fc7ee6df 49eb9afd 47d3515f 4b27ab19>

     */

    

}

-(void) application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error

{

   NSLog(@"failed to get token, error:%@",error);

 

}

 

if([launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]){
        //if we're here, apps was launched due to Remote Notification

    }

 

 

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

{

   self.window = [[[UIWindowalloc]initWithFrame:[[UIScreenmainScreen]bounds]]autorelease];

   // Override point for customization after application launch.

   self.viewController = [[[flyViewControlleralloc]initWithNibName:@"flyViewController"bundle:nil]autorelease];

   self.window.rootViewController =self.viewController;

    [self.windowmakeKeyAndVisible];

   //let the device know we want to recieve push notifications

    [[UIApplicationsharedApplication]registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeSound) ];

    //你的程序可以用以下代码了解什么类型的推送通知,我们知道:

    /*

    UIRemoteNotificationType enabledTypes=[[UIApplication sharedApplication] enabledRemoteNotificationTypes];

    NSLog(@"%@",enabledTypes);

    

    */

   //模拟器不支持push notification

   returnYES;

}

===简单测试 通过以下的php脚本(运行在服务器端,通过pem文件建立ssl链接,通过 token发给特定的device《未来服务器端,一定是能识别该给哪个device token发消息的,如果是业务系统的话,不适合多用户合用一个设备一个app的场景》)
<?php
// Put your device token here (without spaces):
$deviceToken = '2d0938f4f6fXXXXXXXXXXe8c043cfc7ee6df49eb9afd47d3515f4b27ab19';
// Put your private key's passphrase here:
$passphrase = 'XXXX!';
// Put your alert message here:
$message = 'My first push notification!';


stream_context 以及stream_socket,以及 json_encode($body);, php包装json很方便的json——encode


$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');//私钥,证书信息所在文件
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase); //密码


// Open a connection to the APNS server
$fp = stream_socket_client(
'ssl://gateway.sandbox.push.apple.com:2195', $err,   //这个sandbox是用来做测试的,通过ping/telnet可以判断你的网络链接问题
$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);


if (!$fp)
exit("Failed to connect: $err $errstr" . PHP_EOL);


echo 'Connected to APNS' . PHP_EOL;


// Create the payload body
$body['aps'] = array(
'alert' => $message,
'sound' => 'default'
);


// Encode the payload as JSON
$payload = json_encode($body);


// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload; //二进制数据发送


// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));


if (!$result)
echo 'Message not delivered' . PHP_EOL;
else
echo 'Message successfully delivered' . PHP_EOL;


// Close the connection to the server
fclose($fp);
 
作为java的,最可靠快速的方式是参考:http://code.google.com/p/javapns/
JavaPNS is a Java library to send notifications through the Apple Push Notification Service

Welcome to the JavaPNS project, home of the most user-friendly, powerful and fine-tuned Java library for APNS! This fully-featured Java library allows developers to push notifications to iOS devices (iPhone, iPod, iPad, etc.) through the Apple Push Notification Service using a simple yet powerful set of tools. First-time users will find the library to be extremely easy to use (start with a single line of code!), while advanced users will benefit from the library's flexible design and scalability features.

Notification sevice tutoral:

How to build an Apple Push Notification provider server (tutorial)

http://blog.serverdensity.com/2009/07/10/how-to-build-an-apple-push-notification-provider-server-tutorial/

Notficiation server Opensource:

http://maniacdev.com/2010/02/feature-rich-push-notification-server/

转A::::

自設Notification Server?煩…

網上已有不少網頁介紹自設Notification Server的詳細方法,然而,筆者認為以下書本的介紹更詳盡:

iPhone Advanced Projects by Dylan Bruzenak

書本第10章有講述相關之詳細方法。

然而,筆者認為架設Notification Server其實是頗麻煩的,因為:

  • 須自設數據庫,以管理用家的Token。
  • 須自行管理inactive token(假如用家移除了你的App,他的token會變成inactive)
  • 大部份Developer對Socket Programming都不熟悉
  • Notification Server必須連線至Apple server的port 2195,但很多hosting都會限制port2195的使用

以上的東東,筆者曾做過,做是做得到,但有一定的Development及Maintenance成本。

更快捷的方法:Urban Airship

Urban Airship公司提供了AirMail Push服務,大大簡化了自設Notification server的程序,你可以使用其server作為你自已的server與Apple server之中間人:用Urban Airship server去管理用家的Token,用其server去發出Notification。因此你可以不用自設數據庫去管理用家的Token。

AirMail Push提供一系列簡易的API(比起自已架設Server去發Notification,AirMail API是簡易多了)供你使用,使你免去架設Socket connection之苦。API是HTTP Request,絕大部份Hosting都容許HTTP request。

然而,這是一項收費服務,Urban Airship提供了幾個收費模式。假如使用其Indie收費模式,每月首250,000個Notification是免費的(其後$0.0005/message)(筆者於2010-10-15更新:Indie收費已由「每月首250,000Notification免費改為每月首1,000,000Notification免費,詳情請參考其收費模式)。

該怎麼選擇?

如果你有大量用戶,又或者要經常發出大量push notification予用家(例如新聞App),不妨自設Notification server。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值