• 免费iOS第三方推送工具Urban Airship使用教程


     本文转载至 http://blog.csdn.net/mamong/article/details/8542404

     

     http://www.dapps.net/dev/iphone/ios-free-push-notification-tool-urban-airship-tutorial.html

    Urban Airship公司是位于俄勒冈州波特兰地区的一家“推送”功能提供商。每月的推送数量达到5.2亿次,平均每分钟的信息发送量约为1.3万次。

    开始教你怎么用Urban Airship快速进行推送,省去自己搭建服务器又省事的好方法。

    先进入网站,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的服务器中,告诉他们可以向你的手机发送推送信息。

    打开你的APP工程文件,找到XXXAppDelegate.m文件,在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法中添加如下代码:

    1
    2
    3
    4
    [application registerForRemoteNotificationTypes:  
         UIRemoteNotificationTypeBadge | 
         UIRemoteNotificationTypeAlert |              
         UIRemoteNotificationTypeSound];

    这段代码会在用户第一次安装应用的时候告知用户是否要接受推送。
    之后再在这个文件中加入如下代码:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    - (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 KeyApplication Secret会在你刚才创建的Urban Airship网站的应用Detail页面找到,如图:

    之后就可以运行编译软件了。成功的话会在output窗口显示We successfully registered for push notifications和Device token。
    现在再回到Urban Airship的页面。刷新一下,看看Details页面中的Device tokens一项是不是变成了1.如果是的话,表示你的手机已经成功在服务器上注册了。
    点击页面左边工具栏中的PUSH项,试试发送一条推送信息

    Test Push Notifications的意思是通过指定的Device token推送信息,如图:

    点击Send it!成功了有木有!

    其它功能都很直观,自己研究下吧。
    最后有一点,就是在进行production推送的时候需要输入信用卡号,不用紧张,只要每月推送量小于一百万条的时候是不会扣费的,呵呵。

    感谢Let the right one in的分享!

  • 相关阅读:
    HDU
    HDU
    A. Reorder the Array
    A. New Building for SIS Codeforce
    HUD Is It A Tree?!!!!!)
    博客园申请博客批准
    一起学CC3200之CRC校验
    新安装CCS 编译问题Process_begin :createProcess
    一起学CC3200之开发环境简介(2)烧录程序
    新安装CCS 后编译出现问题:gmake:No rule to make target clean
  • 原文地址:https://www.cnblogs.com/Camier-myNiuer/p/3455876.html
Copyright © 2020-2023  润新知