第一步:进入开发者的网站
用app的Bundle Identifier注册一个App ID···
此时拖到最后要选择Push
注册一个ID之后注册证书
证书也要选择push
继续下一步需要选择一个CSA文件
我们需要从钥匙串请求
继续下一步
存储到本地成功
这时在证书处选择该文件 继续下一步 证书下载 下载成功.cer文件
双击打开 在钥匙串中导出该文件
导出为.p12文件即可
此时在极光需要证书的地方上传上去即可。
另外,在Xcode中需要设置的地方
在AppDelegate.m中
#define JPushKey @"........."
设置两个属性
@property NSData * deviceToken;
@property NSString * noticeType;
然后重写didFinishLaunching方法将后面的代码粘贴上去即可尝试推送。。。- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
static NSString *jPushChannel = @"Publish channel";
static BOOL isProduction = TRUE;
//初始化
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];
}
// Required
//如需兼容旧版本的方式,请依旧使用[JPUSHService setupWithOption:launchOptions]方式初始化和同时使用pushConfig.plist文件声明appKey等配置内容。
[JPUSHService setupWithOption:launchOptions appKey:JPushKey channel:jPushChannel apsForProduction:isProduction];
NSString * userInfoJson =
[[NSUserDefaults standardUserDefaults] objectForKey:@"userInfoJson"];
// if (userInfoJson){
NSLog(@"userInfoJson from out : %@", userInfoJson);
// }
//
[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"userInfoJson"];
if (launchOptions){
NSDictionary * remoteNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
//这个判断是在程序没有运行的情况下收到通知,点击通知跳转页面
if (remoteNotification) {
// remoteNotification 就是userInfo
[self networkDidReceiveMessage: remoteNotification];
}
}
[application setApplicationIconBadgeNumber:0];
[application cancelAllLocalNotifications];
[JPUSHService resetBadge];
return YES;
}
- (void)networkDidReceiveMessage: (NSDictionary *) userInfo{
// 这里判断一下userInfo里的消息,以决定使用哪种类型的页面。
NSError *error;
NSData *userInfoJsonData = [NSJSONSerialization dataWithJSONObject:userInfo options:NSJSONWritingPrettyPrinted error:&error];
if (error){
NSLog(@"error : %@" , [error localizedDescription]);
return;
}
if (!userInfoJsonData){
NSLog(@"error : %@" , [error localizedDescription]);
return;
}
NSString *jsonString = [[NSString alloc] initWithData:userInfoJsonData encoding:NSUTF8StringEncoding];
[[NSUserDefaults standardUserDefaults] setObject:jsonString forKey:@"userInfoJson"];
}
// =============================JPush 监听方法======================================================================
/*
// 建议开发者加上API里面提供下面 5 种类型的通知:
extern NSString * const kJPFNetworkDidSetupNotification; // 建立连接
extern NSString * const kJPFNetworkDidCloseNotification; // 关闭连接
extern NSString * const kJPFNetworkDidRegisterNotification; // 注册成功
extern NSString * const kJPFNetworkDidLoginNotification; // 登录成功
*/
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSLog(@"=======");
NSLog(@"%@", [NSString stringWithFormat:@"Device Token: %@", deviceToken]);
[JPUSHService registerDeviceToken:deviceToken];
_deviceToken = deviceToken;
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
}
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings: (UIUserNotificationSettings *)notificationSettings {
NSLog(@"bbb");
[application registerForRemoteNotifications];
}
// 获取服务端的通知
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *) userInfo {
NSLog(@"*********");
//[application setApplicationIconBadgeNumber:0];
//[application cancelAllLocalNotifications];
//[JPUSHService resetBadge];
NSNotification *notification = [[NSNotification alloc] initWithName:kJPFNetworkDidReceiveMessageNotification object:nil userInfo:userInfo];
[[NSNotificationCenter defaultCenter] postNotification:notification];
[JPUSHService handleRemoteNotification:userInfo];
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler: (void (^)(UIBackgroundFetchResult)) completionHandler {
[JPUSHService handleRemoteNotification: userInfo];
[[NSNotificationCenter defaultCenter]postNotificationName:@"openWeb" object:nil];
NSLog(@"通知信息%@",userInfo);
[application setApplicationIconBadgeNumber:0];
[application cancelAllLocalNotifications];
[JPUSHService resetBadge];
NSNotification *notification = [[NSNotification alloc] initWithName:kJPFNetworkDidReceiveMessageNotification object:nil userInfo:userInfo];
[[NSNotificationCenter defaultCenter] postNotification:notification];
NSLog(@"………………………………");
self.noticeType = (NSString *)[userInfo objectForKey:@"noticeType"];
if (self.noticeType == nil) {
}
completionHandler(UIBackgroundFetchResultNewData);
}
// Called when your app has been activated by the user selecting an action from
// a local notification.
// A nil action identifier indicates the default action.
// You should call the completion handler as soon as you've finished handling
// the action.
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *) notification completionHandler:(void (^)())completionHandler {
NSLog(@"ccccc");
}
// Called when your app has been activated by the user selecting an action from
// a remote notification.
// A nil action identifier indicates the default action.
// You should call the completion handler as soon as you've finished handling
// the action.
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *) identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void (^)())completionHandler {
NSLog(@"aaaaa");
}