iOS经典讲解之实现App消息推送功能(二)

作者:Loving_iOS

转载请标明出处:http://blog.csdn.net/loving_ios/article/details/49000189

上一篇博客iOS经典讲解之实现App消息推送功能(一)讲解了实现消息推送的的准备工作,接下来我们来讲解第二部分的内容,实现具体的推送及代码示例。

消息推送的第三方平台有很多,这里我们使用极光推送平台,注册极光推送平台的账号。

登陆后进入控制台,创建推送消息的应用。

填写应用的名称并上传iOS开发证书,iOS开发证书需要从我们准备的推送证书导出,下面是导出的具体步骤。

获取iOS开发证书的步骤:

1、打开之前准备的推送证书

2、在钥匙串访问中找到该证书,然后右键导出。

3、选择导出的格式,一定是.p12格式,然后存储。

4、给导出的证书添加密码,可以不添加。如果添加,请记住,在导入到推送平台时使用。

5、填写登陆密码(电脑账户登录密码),到此获得到.p12证书。


将获得到的证书导入,并写入导出证书时填写的密码,如果没有设置密码,可不写,然后创建应用。

创建完后会得到应用的信息,包括APPKey(很重要),一会儿用得到。

剩下的步骤可以参考极光推送平台提供的文档,根据以下步骤获得到开发指南文档:

①点击文档

②选择iOS

③选择iOS集成指南,得到开发指南,参考里面的步骤,完成相关配置操作(很关键)。


到目前为止,所有的工作都已准备完成,下面是代码示例:

//
//  AppDelegate.m
//  PushDemo
//

#import "AppDelegate.h"
#import "APService.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
   
    
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {

        //可以添加自定义categories
        [APService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeSound |UIUserNotificationTypeAlert) categories:nil];
    } else {
        //categories 必须为nil
        
        [APService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert) categories:nil];
    }
    // Required
    [APService setupWithOption:launchOptions];
    return YES;
}
#pragma mark - 注册成功 获取设备deviceToken
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
    
    NSLog(@"%@", deviceToken);
    // Required
    [APService registerDeviceToken:deviceToken];
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    
    // Required
    [APService handleRemoteNotification:userInfo];
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    
    // IOS 7 Support Required
    [APService handleRemoteNotification:userInfo];
    completionHandler(UIBackgroundFetchResultNewData);
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end

在工程的配置方面还有需要注意的地方:

工程的Bundle Identifier一定要和在苹果开发者平台获取的AppID保持一致

其次把描述文件与工程绑定,如图所示,选择对应的描述文件(Provisioning Profile),如果找不到之前你获取的描述文件的名称,请找到你下载的描述文件双击,然后再到工程中,你的描述文件的名称就出现了,然后选中。


如果工程中找不到描述文件名称,请双击。

写好代码,配置好工程之后,就可以发送消息试验了。

在极光推送平台选择对应的应用,然后发送消息。

你的手机对应的应用,就收到推送的消息了,到此消息推送功能已经成功实现。

注意:必须真机测试,才能实现消息推送功能。

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Gateway worker是一种可以将消息推送APP iOS和Android的解决方案。通常情况下,这种技术需要服务器端与客户端进行交互来传达消息,但是Gateway worker的出现可以极大地简化这个过程,将消息的传递变得更加高效和简便。 Gateway worker的实现方法比较简单,需要在服务器端添加一个插件,使其可以与客户端实现双向通信。这个插件需要支持Swoole协议,这是一种最为流行的网络通信协议。通过这个插件,HTTP请求可以转换为WebSocket请求,从而实现推送消息功能。 在使用Gateway worker推送消息时,通过设置一个设备ID和Token,可以将消息推送iOS或Android设备。这个过程非常快速,基本上可以在几毫秒内完成。因此,Gateway worker尤其适合需要进行实时信息推送的应用程序。 在总结上述,Gateway worker可以快速高效地将消息推送APP iOS和Android,通过设置设备ID和Token,可以实现消息的快速传递。Gateway worker主要基于Swoole协议和WebSocket通信协议,因此在使用前需要对这些技术进行深入理解。但是,一旦掌握了这些知识,使用Gateway worker将变得非常容易和便利。 ### 回答2: Gatewayworker是一款支持消息推送的工具,可以将推送消息通过网关传输到移动应用程序(如iOS和Android)。使用Gatewayworker消息推送服务,需要先在开发者平台上创建一个应用程序,并生成对应的应用程序ID和应用程序Key。 在应用程序中,需要将Gatewayworker SDK集成到应用程序中,将应用程序ID和应用程序Key配置为Gatewayworker SDK的参数,并获取设备标识符(如设备ID、APNS或GCM的token等)。在使用Gatewayworker SDK的过程中,需要先向Gatewayworker服务器注册设备,然后才能够向设备推送消息。 要推送消息到设备,在应用程序中需要将消息封装为Gatewayworker SDK支持的格式,并指定接收消息的设备标识符。然后,将消息发送到Gatewayworker服务器,并由Gatewayworker服务器转发到接收方设备。接收方设备通过Gatewayworker SDK接收推送消息,并在应用程序中处理消息。 总的来说,Gatewayworker是一个强大的推送服务工具,使得移动应用程序开发人员可以方便地向iOS和Android设备推送消息,提高了应用程序的使用体验和用户满意度。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值