iOS集成极光推送
一、配置APNs
苹果APNs(英文全称:Apple Push Notification Service)
1.配置开发证书
![Uploading apns_13_803165.png …]
2.配置生产证书
和配置开发证书的流程相同。
3.将配置好的证书导出为 .p12 文件
一、注册极光推送帐号
可登录 极光推送官网 注册帐号。
注册成功后进入控制台
二、集成极光推送SDK
sdk地址
我使用的是JPush-iOS-SDK-2.1.0。下载的文件里有个pdf文件iOS+SDK+Integration+Guide.pdf,介绍了集成极光推送的详细代码。
在工程文件中,做如下设置:
![Uploading apns_18_876090.png …]
下面是我写的代码:
AppDelegate.m文件
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//设置appKey
[JPushConfig registerJPush:launchOptions];
return YES;
}
#pragma mark 修改通知处理函数
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(nonnull void (^)(UIBackgroundFetchResult))completionHandler {
//推送消息携带 content-available: 1 是Background 运行的必须参数,如果不携带此字段则与iOS7 之前版本的普通推送一样。
//清空角标
[JPUSHService resetBadge];
if (!userInfo) {
completionHandler(UIBackgroundFetchResultNoData);
return ;
}
completionHandler(UIBackgroundFetchResultNewData);
//根据服务器设置的 userInfo 的格式,进行解析
}
// Required
-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
[JPUSHService registerDeviceToken:deviceToken];
}
// Required
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
[JPUSHService handleRemoteNotification:userInfo];
application.applicationIconBadgeNumber = 0;
}
- (void)application:(UIApplication *)application
didReceiveLocalNotification:(UILocalNotification *)notification {
[JPUSHService showLocalNotificationAtFront:notification identifierKey:nil];
}
-(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
[application setApplicationIconBadgeNumber:0];
}
JPushConfig.m文件
/** 注册 JPush */
+(void)registerJPush:(NSDictionary *)launchOptions {
// Required
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
//可以添加自定义categories
[JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |
UIUserNotificationTypeSound |
UIUserNotificationTypeAlert)
categories:nil];
} else {
//categories 必须为nil
[JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |
UIUserNotificationTypeBadge |
UIUserNotificationTypeBadge)
categories:nil];
}
NSString *appKey = @"极光推送生成的KEY";
NSString *channel = @"App Store";//发布渠道,可以随便定义
BOOL isProduction = false;//这个要根据极光推送控制台里设置的是开发环境还是生产环境
//Required
[JPUSHService setupWithOption:launchOptions
appKey:appKey
channel:channel
apsForProduction:isProduction];
}
收到通知消息后,在AppDelegate.m跳转到指定界面的方法:
UIViewController *vc = [[UIViewController alloc] init];
[self.window.rootViewController presentViewController:vc animated:YES completion:nil];
时间: 2024-09-29 18:12:39