一、配置APNs
苹果APNs(英文全称:Apple Push Notification Service)
1.配置开发证书
2.配置生产证书
和配置开发证书的流程相同。
3.将配置好的证书导出为 .p12 文件
一、注册极光推送帐号
可登录 极光推送官网 注册帐号。
注册成功后进入控制台
二、集成极光推送SDK
sdk下载地址 点击传送
我使用的是JPush-iOS-SDK-2.1.0。下载的文件里有个pdf文件iOS+SDK+Integration+Guide.pdf,介绍了集成极光推送的详细代码。
在工程文件中,做如下设置:
这是我的代码
AppDelegate.h
static NSString *appKey = @"你在极光申请到的KEY"; //AppKey *必填
static NSString *channel = @"Publish channel"; //发布聚到 选填
static BOOL isProduction = FALSE; //是否为生产环境
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//Required
[self configurationPushInfo:launchOptions];
return YES;
}
-(void)configurationPushInfo:(NSDictionary *)launchOptions{
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
//可以添加自定义categories
[JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |
UIUserNotificationTypeSound |
UIUserNotificationTypeAlert)
categories:nil];
} else {
//categories 必须为nil
[JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeSound |
UIRemoteNotificationTypeAlert)
categories:nil];
}
[JPUSHService setupWithOption:launchOptions appKey:appKey
channel:channel apsForProduction:isProduction];
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
/// Required - 注册 DeviceToken
[JPUSHService registerDeviceToken:deviceToken];
}
/**
* 当前APP处于前台活跃状态时接收到推送通知时调用的方法
* 当前APP处于后台挂起时接到推送点击顶部提示框进到app中调用的方法
* 当前APP处于后台挂起时接到推送点击通知栏消息进入到app中调用的方法
* @param application <#application description#>
* @param userInfo <#userInfo description#>
* @param completionHandler <#completionHandler description#>
*/
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
completionHandler(UIBackgroundFetchResultNewData);
NSLog(@"收到通知:%@", [self logDic:userInfo]);
//[[NSNotificationCenter defaultCenter] postNotificationName:@"gotoMessageView" object:nil userInfo:<#(nullable NSDictionary *)#>];
[[NSNotificationCenter defaultCenter] postNotificationName:@"gotoMessageView" object:userInfo];
}
/**
* 注册失败的Push 方法
*
* @param application 应用描述
* @param error 错误信息
*/
- (void)application:(UIApplication *)application
didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
}
#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_7_1
- (void)application:(UIApplication *)application
didRegisterUserNotificationSettings:
(UIUserNotificationSettings *)notificationSettings {
}
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier
forLocalNotification:(UILocalNotification *)notification
completionHandler:(void (^)())completionHandler {
}
- (void)application:(UIApplication *)application
handleActionWithIdentifier:(NSString *)identifier
forRemoteNotification:(NSDictionary *)userInfo
completionHandler:(void (^)())completionHandler {
}
#endif
- (void)application:(UIApplication *)application
didReceiveLocalNotification:(UILocalNotification *)notification {
[JPUSHService showLocalNotificationAtFront:notification identifierKey:nil];
}
//编码问题
- (NSString *)logDic:(NSDictionary *)dic {
if (![dic count]) {
return nil;
}
NSString *tempStr1 =
[[dic description] stringByReplacingOccurrencesOfString:@"\\u"
withString:@"\\U"];
NSString *tempStr2 =
[tempStr1 stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];
NSString *tempStr3 =
[[@"\"" stringByAppendingString:tempStr2] stringByAppendingString:@"\""];
NSData *tempData = [tempStr3 dataUsingEncoding:NSUTF8StringEncoding];
NSString *str =
[NSPropertyListSerialization propertyListFromData:tempData
mutabilityOption:NSPropertyListImmutable
format:NULL
errorDescription:NULL];
return str;
}
本文参考自简书作者:hrscy
如有转载请注明出处