关于推送通知,除了苹果的APNS之外,我们还有很多其他的选择,Urban Airship就是其中之一。
Urban Airship公司是位于俄勒冈州波特兰地区的一家“推送”功能提供商。每月的推送数量达到5.2亿次,平均每分钟的信息发送量约为1.3万次。
开始教你怎么用Urban Airship快速进行推送,省去自己搭建服务器又省事的好方法。
Urban Airship不仅仅做普通的推送通知,他们还发明了一种Rich Push,可以推送更丰富的内容,例如html、音频、视频等。
先进入网站,https://www.urbanairship.com/ 第一步,注册一个自己的帐号是必须的。关于帐号类型,对于推送量不大的应用来说,免费的足矣。(降低开发成本)
注册好了免费帐号之后要做的就是添加自己的应用了。如下图,填好各项信息。
这里需要提一下的是,1. Application Mode,分为Development和Production。分别是测试用和正式的产品。建议分别建立两个应用一个测试用,一个做正式推送。
2. 需要把下面的“Push Notifications Surpport”选项打勾才能出现页面下方的内容。
3. 再往下的“Apple push certificate”一项就是那个从证书助手里导出的p12文件,当然也分为Development和Production的,需要注意。
这里还可以为BlackBerry和Android系统的应用添加推送,具体就不细说了。在一切信息填写OK之后点击页面最下方的【create your application】按钮,完成应用的添加。
下面需要在你的应用里加入一些代码,来让IOS设备在打开应用的时候发送自己的device token到Urban Airship的服务器中,告诉他们可以向你的手机发送推送信息。
[application registerForRemoteNotificationTypes:
UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeAlert |
UIRemoteNotificationTypeSound];
这段代码会在用户第一次安装应用的时候告知用户是否要接受推送。
之后再在这个文件中加入如下代码:
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
// Convert the token to a hex string and make sure it's all caps
NSMutableString *tokenString = [NSMutableString stringWithString:[[deviceToken description] uppercaseString]];
[tokenString replaceOccurrencesOfString:@"<" withString:@"" options:0 range:NSMakeRange(0, tokenString.length)];
[tokenString replaceOccurrencesOfString:@">" withString:@"" options:0 range:NSMakeRange(0, tokenString.length)];
[tokenString replaceOccurrencesOfString:@" " withString:@"" options:0 range:NSMakeRange(0, tokenString.length)];
NSLog(@"Token: %@", tokenString);
// Create the NSURL for the request
NSString *urlFormat = @"https://go.urbanairship.com/api/device_tokens/%@";
NSURL *registrationURL = [NSURL URLWithString:[NSString stringWithFormat:
urlFormat, tokenString]];
// Create the registration request
NSMutableURLRequest *registrationRequest = [[NSMutableURLRequest alloc]
initWithURL:registrationURL];
[registrationRequest setHTTPMethod:@"PUT"];
// And fire it off
NSURLConnection *connection = [NSURLConnection connectionWithRequest:registrationRequest
delegate:self];
[connection start];
// TODO: Pass the token to our server
NSLog(@"We successfully registered for push notifications");
}
- (void)connection:(NSURLConnection *)connection
didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
// Check for previous failures
if ([challenge previousFailureCount] > 0)
{
// We've already tried - something is wrong with our credentials
NSLog(@"Urban Airship credentials invalid");
return;
}
// Send our Urban Airship credentials
NSURLCredential *airshipCredentials = [NSURLCredential credentialWithUser:@"这里需要填写Application Key"
password:@"这里填Application Secret"
persistence:NSURLCredentialPersistenceNone];
[[challenge sender] useCredential:airshipCredentials
forAuthenticationChallenge:challenge];
}
最后这段代码中所需的 Application Key 和 Application Secret 会在你刚才创建的Urban Airship网站的应用Detail页面找到,如图:
![](https://img-my.csdn.net/uploads/201303/02/1362223197_6169.png)
现在再回到Urban Airship的页面。刷新一下,看看Details页面中的Device tokens一项是不是变成了1.如果是的话,表示你的手机已经成功在服务器上注册了。
点击页面左边工具栏中的PUSH项,试试发送一条推送信息
![](https://img-my.csdn.net/uploads/201303/02/1362223205_8048.png)
![](https://img-my.csdn.net/uploads/201303/02/1362223214_7679.png)
其它功能都很直观,自己研究下吧。
最后有一点,就是在进行production推送的时候需要输入信用卡号,不用紧张,只要每月推送量小于一百万条的时候是不会扣费的,呵呵。
感谢Let the right one in的分享!