参考http://www.cnblogs.com/XYQ-208910/p/5463802.html
//1.创建一个类
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
@interface CFPushHelper : NSObject
///----------------------------------------------------
/// @name Setup 启动相关
///----------------------------------------------------
+ (NSString *)registrationID;
/*!
* @abstract 启动SDK
*
* @param launchingOption 启动参数.
* @param appKey 一个JPush 应用必须的,唯一的标识. 请参考 JPush 相关说明文档来获取这个标识.
* @param channel 发布渠道. 可选.
* @param isProduction 是否生产环境. 如果为开发状态,设置为 NO; 如果为生产状态,应改为 YES.
* @param advertisingIdentifier 广告标识符(IDFA) 如果不需要使用IDFA,传nil.
*
* @discussion 提供SDK启动必须的参数, 来启动 SDK.
* 此接口必须在 App 启动时调用, 否则 JPush SDK 将无法正常工作.
*/
+ (void)setupWithOption:(NSDictionary *)launchingOption
appKey:(NSString *)appKey
channel:(NSString *)channel
apsForProduction:(BOOL)isProduction
advertisingIdentifier:(NSString *)advertisingId;
///----------------------------------------------------
/// @name APNs about 通知相关
///----------------------------------------------------
/*!
* @abstract 注册要处理的远程通知类型
*
* @param types 通知类型
* @param categories
*
* @discussion
*/
//+ (void)registerForRemoteNotificationTypes:(NSUInteger)types
// categories:(NSSet *)categories;
//上传deviceToken
+ (void)registerDeviceToken:(NSData *)deviceToken;
/*!
* @abstract 处理收到的 APNs 消息
*/
//+ (void)handleRemoteNotification:(NSDictionary *)userInfo;
// ios7以后,才有completion,否则传nil
+ (void)handleRemoteNotification:(NSDictionary *)userInfo completion:(void (^)(UIBackgroundFetchResult))completion;
/*!
* @abstract 前台展示本地推送
*
* @param notification 本地推送对象
* @param notificationKey 需要前台显示的本地推送通知的标示符
*
* @discussion 默认App在前台运行时不会进行弹窗,在程序接收通知调用此接口可实现指定的推送弹窗。
*/
+ (void)showLocalNotificationAtFront:(UILocalNotification *)notification identifierKey:(NSString *)notificationKey;
@end
//导入极光推送头文件@"JPUSHService.h"
#import "CFPushHelper.h"
#import "JPUSHService.h"
@implementation CFPushHelper
+ (NSString *)registrationID{
return [JPUSHService registrationID];
}
+ (void)setupWithOption:(NSDictionary *)launchingOption appKey:(NSString *)appKey channel:(NSString *)channel apsForProduction:(BOOL)isProduction advertisingIdentifier:(NSString *)advertisingId{
//Required
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
// 如需继续使用pushConfig.plist文件声明appKey等配置内容,请依旧使用[JPUSHService setupWithOption:launchOptions]方式初始化。
[JPUSHService setupWithOption:launchingOption appKey:appKey
channel:channel
apsForProduction:isProduction
advertisingIdentifier:advertisingId];
return;
}
+ (void)registerDeviceToken:(NSData *)deviceToken{
[JPUSHService registerDeviceToken:deviceToken];
return;
}
//+ (void)handleRemoteNotification:(NSDictionary *)userInfo{
//
// [CFPushHelper handleRemoteNotification:userInfo];
//
//}
+ (void)handleRemoteNotification:(NSDictionary *)userInfo completion:(void (^)(UIBackgroundFetchResult))completion{
[JPUSHService handleRemoteNotification:userInfo];
if (completion) {
completion(UIBackgroundFetchResultNewData);
}
return;
}
+ (void)showLocalNotificationAtFront:(UILocalNotification *)notification
identifierKey:(NSString *)notificationKey{
[CFPushHelper showLocalNotificationAtFront:notification identifierKey:nil];
return;
}
@end
//我这里创建的AppDelegate的类别
#import "AppDelegate.h"
@interface AppDelegate (CFPushSDK) <UIAlertViewDelegate>
- (void)JPushapplication:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;
@end
#import "AppDelegate+CFPushSDK.h"
#import "JPUSHService.h"
#import "CFPushHelper.h"
#define JPushSDK_AppKey @"顾名思义"
#define isProduction NO
@implementation AppDelegate (CFPushSDK)
- (void)JPushapplication:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
[CFPushHelper setupWithOption:launchOptions appKey:JPushSDK_AppKey channel:nil apsForProduction:isProduction advertisingIdentifier:nil];
//注册
NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
[defaultCenter addObserver:self selector:@selector(networkDidReceiveMessage:) name:kJPFNetworkDidLoginNotification object:nil];
}
//通知方法
- (void)networkDidReceiveMessage:(NSNotification *)notification {
//调用接口
NSLog(@"\n\n极光推送注册成功\n\n");
//通知后台registrationID
//NSLog(@"registrationID2 = %@",[CFPushHelper registrationID]);
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (![CFPushHelper registrationID]) {
NSString *registrationID = @"";
[defaults setObject:registrationID forKey:@"registrationID"];
} else {
[defaults setObject:[CFPushHelper registrationID] forKey:@"registrationID"];
}
[defaults synchronize];
//[CFPushHelper registrationID]
//注销通知
[[NSNotificationCenter defaultCenter] removeObserver:self name:kJPFNetworkDidLoginNotification object:nil];
}
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
/// Required - 注册 DeviceToken
//NSLog(@"%@", [NSString stringWithFormat:@"Device Token: %@", deviceToken]);
[CFPushHelper registerDeviceToken:deviceToken];
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
// IOS 7 Support Required
[CFPushHelper handleRemoteNotification:userInfo completion:completionHandler];
NSLog(@"收到通知:%@", [self logDic:userInfo]);
completionHandler(UIBackgroundFetchResultNewData);
//保存推送消息
[self gotoViewControllerWith:userInfo];
// 应用正处理前台状态下,不会收到推送消息,因此在此处需要额外处理一下
if (application.applicationState == UIApplicationStateActive) {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"收到推送消息"
message:userInfo[@"aps"][@"alert"]
delegate:self
cancelButtonTitle:@"取消"
otherButtonTitles:@"确定",nil];
[alert show];
// UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"收到推送消息" message:userInfo[@"aps"][@"alert"] preferredStyle:UIAlertControllerStyleAlert];
//
// UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
//
// }];
//
// UIAlertAction *confirm = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
//
// }];
//
// [alertVC addAction:cancel];
// [alertVC addAction:confirm];
//
}
//[self gotoViewController];
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{
//NSLog(@"did Fail To Register For Remote Notifications With Error : %@",error);
return;
}
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
[CFPushHelper showLocalNotificationAtFront:notification identifierKey:nil];
return;
}
- (void)applicationDidBecomeActive:(UIApplication *)application{
[application setApplicationIconBadgeNumber:0];
//[application cancelAllLocalNotifications];
return;
}
@end
#import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
//注册极光推送
[self JPushapplication:application didFinishLaunchingWithOptions:launchOptions];
return YES;
}
//获取当前控制器,如果是登陆之后的,收到推送直接进入推送界面,如果不是则进入登陆,保存推送消息,登陆后进入推送
- (UIViewController *)currentViewController{
if ([self.window.rootViewController isKindOfClass:[UINavigationController class]]) {
//UIViewController *vc = [(UINavigationController *)self.window.rootViewController visibleViewController];
return nil;
} else {
// UINavigationController *vc1 = ((UITabBarController *)self.window.rootViewController).selectedViewController;
UIViewController *vc = ((UINavigationController *)((UITabBarController *)self.window.rootViewController).selectedViewController).visibleViewController;
return vc;
}
return 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;
}
- (void)gotoViewControllerWith:(NSDictionary *)dic{
// NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
// [defaults setObject:@"push" forKey:@"push"];
// [defaults synchronize];
NSString *urlStr = [dic objectForKey:@"url"];
if (urlStr) {
//isNontice 用来做判断是否有推送
NSInteger isNontice = 1;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:urlStr forKey:@"urlStr"];
[defaults setInteger:isNontice forKey:@"isNontice"];
[defaults synchronize];
// UIStoryboard *mainSB = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
// ZsTabBarController * zsVC= [mainSB instantiateViewControllerWithIdentifier:@"ZsTabBarController"];
//
// self.window.rootViewController = zsVC;
}
}
#pragma mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 1) {
UIStoryboard *mainSB = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
NoticeViewController *noticeVC = [mainSB instantiateViewControllerWithIdentifier:@"NoticeViewController"];
[[self currentViewController].navigationController pushViewController:noticeVC animated:YES];
NSLog(@"%@",[self currentViewController].title);
}
}