admin管理员组

文章数量:1130349

极光推送使用 IOS端

极光推送官网:

极光推送IOS官方文档:


集成方式

1、在JPush Portal上创建应用
在JPush的管理Portal上 上传证书并创建应用。如果对APNs证书不太了解 请参考 iOS 证书设置指南,


创建成功后自动生成 AppKey 用以标识该应用。



2、导入API开发包到应用程序项目
将SDK包解压,在XCode中选择“Add files to 'Your project name'...”,将解压后的lib子文件夹(包含APService.h、libPushSDK.a)添加到你的工程目录中。


3、必要的框架
CFNetwork.framework
CoreFoundation.framework
CoreTelephony.framework
SystemConfiguration.framework
CoreGraphics.framework
Foundation.framework
UIKit.framework
Security.framework
libz.dylib


4、Build Settings
设置 Search Paths 下的 User Header Search Paths 和 Library Search Paths,比如SDK文件夹(默认为lib)与工程文件在同一级目录下,则都设置为"$(SRCROOT)/[文件夹名称]"即可。


5、创建并配置PushConfig.plist文件
在你的工程中创建一个新的Property List文件,并将其命名为PushConfig.plist,填入Portal为你的应用提供的APP_KEY等参数。

{
"APS_FOR_PRODUCTION = "0";
"CHANNEL" = "Publish channel";
"APP_KEY" = "AppKey copied from JPush Portal application";
}



  • CHANNEL
指明应用程序包的下载渠道,为方便分渠道统计。根据你的需求自行定义即可。
  • APP_KEY
在管理Portal上创建应用时自动生成的(AppKey)用以标识该应用。请确保应用内配置的 AppKey 与第1步在 Portal 上创建应用时生成的 AppKey 一致,AppKey 可以在应用详情中查询。

  • APS_FOR_PRODUCTION
1.3.1版本新增,表示应用是否采用生产证书发布( Ad_Hoc 或 APP Store ),0 (默认值)表示采用的是开发者证书,1 表示采用生产证书发布应用。请注意此处配置与 Web Portal 应用环境设置匹配。
在1.2.2或之前版本的配置文件中,有 TEST_MODE 这个键,新版的SDK不再使用,可以将它删除。


6、添加代码:

在 APPDelegate.m文件中的 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法中添加代码:

//极光推送NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];[defaultCenter addObserver:self selector:@selector(networkDidSetup:) name:kAPNetworkDidSetupNotification object:nil];[defaultCenter addObserver:self selector:@selector(networkDidClose:) name:kAPNetworkDidCloseNotification object:nil];[defaultCenter addObserver:self selector:@selector(networkDidRegister:) name:kAPNetworkDidRegisterNotification object:nil];[defaultCenter addObserver:self selector:@selector(networkDidLogin:) name:kAPNetworkDidLoginNotification object:nil];[defaultCenter addObserver:self selector:@selector(networkDidReceiveMessage:) name:kAPNetworkDidReceiveMessageNotification object:nil];[APService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |UIRemoteNotificationTypeSound |UIRemoteNotificationTypeAlert)];// Required[APService setupWithOption:launchOptions];

同时在APPDelegate.m 文件中添加代码

#pragma mark - 推送- (void)networkDidSetup:(NSNotification *)notification {NSLog(@"已连接");
}- (void)networkDidClose:(NSNotification *)notification {NSLog(@"未连接。。。");
}- (void)networkDidRegister:(NSNotification *)notification {NSLog(@"已注册");
}- (void)networkDidLogin:(NSNotification *)notification {NSLog(@"已登录");NSLog(@"registrationID:%@",[APService registrationID]);
}- (void)networkDidReceiveMessage:(NSNotification *)notification {NSDictionary * userInfo = [notification userInfo];NSString *title = [userInfo valueForKey:@"title"];NSString *content = [userInfo valueForKey:@"content"];NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];[dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];NSLog(@"%@",[NSString stringWithFormat:@"收到消息\ndate:%@\ntitle:%@\ncontent:%@", [dateFormatter stringFromDate:[NSDate date]],title,content]);}- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {[APService registerDeviceToken:deviceToken];
}- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {[APService handleRemoteNotification:userInfo];
}- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {// 取得 APNs 标准信息内容NSDictionary *aps = [userInfo valueForKey:@"aps"];NSString *content = [aps valueForKey:@"alert"]; //推送显示的内容NSInteger badge = [[aps valueForKey:@"badge"] integerValue]; //badge数量NSString *sound = [aps valueForKey:@"sound"]; //播放的声音NSDictionary *dic = @{@"Lo": [userInfo valueForKey:@"Lo"], @"La": [userInfo valueForKey:@"La"], @"address": [userInfo valueForKey:@"address"]};[[SRViewController shareInstance] addPointAnnotationForAPNS:dic];// 取得自定义字段内容NSLog(@"content =[%@], badge=[%d], sound=[%@]",content,badge,sound);// Required// IOS 7 Support Required[APService handleRemoteNotification:userInfo];completionHandler(UIBackgroundFetchResultNewData);
}//设置badge
- (void)applicationDidEnterBackground:(UIApplication *)application
{[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}- (void)applicationWillEnterForeground:(UIApplication *)application
{[application setApplicationIconBadgeNumber:0];
}


极光推送使用 IOS端

极光推送官网:

极光推送IOS官方文档:


集成方式

1、在JPush Portal上创建应用
在JPush的管理Portal上 上传证书并创建应用。如果对APNs证书不太了解 请参考 iOS 证书设置指南,


创建成功后自动生成 AppKey 用以标识该应用。



2、导入API开发包到应用程序项目
将SDK包解压,在XCode中选择“Add files to 'Your project name'...”,将解压后的lib子文件夹(包含APService.h、libPushSDK.a)添加到你的工程目录中。


3、必要的框架
CFNetwork.framework
CoreFoundation.framework
CoreTelephony.framework
SystemConfiguration.framework
CoreGraphics.framework
Foundation.framework
UIKit.framework
Security.framework
libz.dylib


4、Build Settings
设置 Search Paths 下的 User Header Search Paths 和 Library Search Paths,比如SDK文件夹(默认为lib)与工程文件在同一级目录下,则都设置为"$(SRCROOT)/[文件夹名称]"即可。


5、创建并配置PushConfig.plist文件
在你的工程中创建一个新的Property List文件,并将其命名为PushConfig.plist,填入Portal为你的应用提供的APP_KEY等参数。

{
"APS_FOR_PRODUCTION = "0";
"CHANNEL" = "Publish channel";
"APP_KEY" = "AppKey copied from JPush Portal application";
}



  • CHANNEL
指明应用程序包的下载渠道,为方便分渠道统计。根据你的需求自行定义即可。
  • APP_KEY
在管理Portal上创建应用时自动生成的(AppKey)用以标识该应用。请确保应用内配置的 AppKey 与第1步在 Portal 上创建应用时生成的 AppKey 一致,AppKey 可以在应用详情中查询。

  • APS_FOR_PRODUCTION
1.3.1版本新增,表示应用是否采用生产证书发布( Ad_Hoc 或 APP Store ),0 (默认值)表示采用的是开发者证书,1 表示采用生产证书发布应用。请注意此处配置与 Web Portal 应用环境设置匹配。
在1.2.2或之前版本的配置文件中,有 TEST_MODE 这个键,新版的SDK不再使用,可以将它删除。


6、添加代码:

在 APPDelegate.m文件中的 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法中添加代码:

//极光推送NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];[defaultCenter addObserver:self selector:@selector(networkDidSetup:) name:kAPNetworkDidSetupNotification object:nil];[defaultCenter addObserver:self selector:@selector(networkDidClose:) name:kAPNetworkDidCloseNotification object:nil];[defaultCenter addObserver:self selector:@selector(networkDidRegister:) name:kAPNetworkDidRegisterNotification object:nil];[defaultCenter addObserver:self selector:@selector(networkDidLogin:) name:kAPNetworkDidLoginNotification object:nil];[defaultCenter addObserver:self selector:@selector(networkDidReceiveMessage:) name:kAPNetworkDidReceiveMessageNotification object:nil];[APService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |UIRemoteNotificationTypeSound |UIRemoteNotificationTypeAlert)];// Required[APService setupWithOption:launchOptions];

同时在APPDelegate.m 文件中添加代码

#pragma mark - 推送- (void)networkDidSetup:(NSNotification *)notification {NSLog(@"已连接");
}- (void)networkDidClose:(NSNotification *)notification {NSLog(@"未连接。。。");
}- (void)networkDidRegister:(NSNotification *)notification {NSLog(@"已注册");
}- (void)networkDidLogin:(NSNotification *)notification {NSLog(@"已登录");NSLog(@"registrationID:%@",[APService registrationID]);
}- (void)networkDidReceiveMessage:(NSNotification *)notification {NSDictionary * userInfo = [notification userInfo];NSString *title = [userInfo valueForKey:@"title"];NSString *content = [userInfo valueForKey:@"content"];NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];[dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];NSLog(@"%@",[NSString stringWithFormat:@"收到消息\ndate:%@\ntitle:%@\ncontent:%@", [dateFormatter stringFromDate:[NSDate date]],title,content]);}- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {[APService registerDeviceToken:deviceToken];
}- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {[APService handleRemoteNotification:userInfo];
}- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {// 取得 APNs 标准信息内容NSDictionary *aps = [userInfo valueForKey:@"aps"];NSString *content = [aps valueForKey:@"alert"]; //推送显示的内容NSInteger badge = [[aps valueForKey:@"badge"] integerValue]; //badge数量NSString *sound = [aps valueForKey:@"sound"]; //播放的声音NSDictionary *dic = @{@"Lo": [userInfo valueForKey:@"Lo"], @"La": [userInfo valueForKey:@"La"], @"address": [userInfo valueForKey:@"address"]};[[SRViewController shareInstance] addPointAnnotationForAPNS:dic];// 取得自定义字段内容NSLog(@"content =[%@], badge=[%d], sound=[%@]",content,badge,sound);// Required// IOS 7 Support Required[APService handleRemoteNotification:userInfo];completionHandler(UIBackgroundFetchResultNewData);
}//设置badge
- (void)applicationDidEnterBackground:(UIApplication *)application
{[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}- (void)applicationWillEnterForeground:(UIApplication *)application
{[application setApplicationIconBadgeNumber:0];
}


本文标签: 极光推送使用 IOS端