首先创建 项目工程Bundel ID 记得设置 push
再次创建证书
1 开发证书
2 生产证书
去苹果开发者中心即可,创建的时候仅仅和自己电脑cer文件有关系,其他毛关系没有,不必担心和害怕,创建完毕,下载,从钥匙串 导出P12上传到极光后台即可(如果刚开始用自己公司开发者账号,最后切换客户的账号,一定要在极光后台把以前的应用删除然后重建即可,否则会有推送历史,导致提示你 ID不一致的错误)
然后在项目里面 Cocoapods极光相关东西。
在#import "AppDelegate.h"里面粘贴下面代码,我这里关于App在前台后台运行都做好了。
#pragma mark - 极光推送
// 1.0 初始化代码
-(void)configJPushWithOptions:(NSDictionary *)launchOptions
{
NSString *advertisingId = [[[ASIdentifierManagersharedManager] advertisingIdentifier]UUIDString];
//Required
if ([[UIDevicecurrentDevice].systemVersionfloatValue] >= 8.0) {
//可以添加自定义categories
[JPUSHServiceregisterForRemoteNotificationTypes:(UIUserNotificationTypeBadge |
UIUserNotificationTypeSound |
UIUserNotificationTypeAlert)
categories:nil];
}
else {
//categories 必须为nil
[JPUSHServiceregisterForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeSound |
UIRemoteNotificationTypeAlert)
categories:nil];
}
//Required
[JPUSHServicesetupWithOption:launchOptions appKey:KJG_Key
channel:@"AppStore"
apsForProduction:YES
advertisingIdentifier:advertisingId];
// 获取自定义消息推送内容
NSNotificationCenter *defaultCenter = [NSNotificationCenterdefaultCenter];
[defaultCenter addObserver:selfselector:@selector(networkDidReceiveMessage:)name:kJPFNetworkDidReceiveMessageNotificationobject:nil];
}
// 2.0 注册APNs成功并上报DeviceToken
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
/// Required - 注册 DeviceToken
[JPUSHServiceregisterDeviceToken:deviceToken];
}
// 3.0 实现注册APNs失败接口(可选)
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
//Optional
NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
}
// 4.0 添加处理APNs通知回调方法
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
// Required, iOS 7 Support
[JPUSHServicehandleRemoteNotification:userInfo];
completionHandler(UIBackgroundFetchResultNewData);
// 4.1程序打开的时候接送推送通知
NSLog(@"收到通知:%@--1", [selflogDic:userInfo]);
// 声音
AudioServicesPlaySystemSound(1007);
// 2.0弹出框
UIAlertController *alertController = [UIAlertControlleralertControllerWithTitle:@"信息通知"message:@"你有一条消息是否查看"preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertActionactionWithTitle: @"取消"style:UIAlertActionStyleCancelhandler:^(UIAlertAction *action) {
}];
UIAlertAction *otherAction = [UIAlertActionactionWithTitle: @"查看"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction *action) {
[selfgoToMssageViewControllerWith:userInfo];
}];
[alertController addAction:cancelAction];
[alertController addAction:otherAction];
[self.window.rootViewController presentViewController:alertControlleranimated:YEScompletion:nil];
}
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo {
[JPUSHServicehandleRemoteNotification:userInfo];
NSLog(@"收到通知:%@--2", [selflogDic:userInfo]);
// 4.2程序在后台的时候接送推送通知-打开APP消息中心
[selfgoToMssageViewControllerWith:userInfo];
}
- (NSString *)logDic:(NSDictionary *)dic {
if (![diccount]) {
returnnil;
}
NSString *tempStr1 =
[[dic description]stringByReplacingOccurrencesOfString:@"\\u"
withString:@"\\U"];
NSString *tempStr2 =
[tempStr1 stringByReplacingOccurrencesOfString:@"\""withString:@"\\\""];
NSString *tempStr3 =
[[@"\""stringByAppendingString:tempStr2]stringByAppendingString:@"\""];
NSData *tempData = [tempStr3dataUsingEncoding:NSUTF8StringEncoding];
NSString *str =
[NSPropertyListSerializationpropertyListFromData:tempData
mutabilityOption:NSPropertyListImmutable
format:NULL
errorDescription:NULL];
return str;
}
// 5.0 用户点击通知调到相应界面
- (void)goToMssageViewControllerWith:(NSDictionary*)msgDic{
//将字段存入本地,因为要在你要跳转的页面用它来判断,这里我只介绍跳转一个页面,
[NSUSERDEFAULTSsetValue:@"kPushFlag"forKey:kPushFlag];
MsgCenterViewCtl * Vc = [[MsgCenterViewCtlalloc]init];
RootNavigationCtl * Nav = [[RootNavigationCtlalloc]initWithRootViewController:Vc];
[self.window.rootViewControllerpresentViewController:Nav animated:YEScompletion:nil];
}
// 6.0 获取自定义消息推送
- (void)networkDidReceiveMessage:(NSNotification *)notification {
NSDictionary * userInfo = [notificationuserInfo];
NSLog(@"收到通知:%@--1", [selflogDic:userInfo]);
// 声音
AudioServicesPlaySystemSound(1007);
// 2.0弹出框
UIAlertController *alertController = [UIAlertControlleralertControllerWithTitle:@"信息通知"message:@"你有一条消息是否查看"preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertActionactionWithTitle: @"取消"style:UIAlertActionStyleCancelhandler:^(UIAlertAction *action) {
}];
UIAlertAction *otherAction = [UIAlertActionactionWithTitle: @"查看"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction *action) {
[selfgoToMssageViewControllerWith:userInfo];
}];
[alertController addAction:cancelAction];
[alertController addAction:otherAction];
[self.window.rootViewController presentViewController:alertControlleranimated:YEScompletion:nil];
}
这样的话 就集成好了,服务器给咱们客户端推送的时候一定会以别名形式,所以登录成功我们需要设置别名。
// 登录成功,设置推送别名
[JPUSHServicesetTags:nilaliasInbackground:[NSStringstringWithFormat:@"%@",dicTemp[@"id"]]];
当点击消息中心的时候我们需要把 app logo的数量制空
// 置空消息未读数量
[JPUSHServiceresetBadge];
[UIApplicationsharedApplication].applicationIconBadgeNumber=0;