#import "AppDelegate.h"
#import "APService.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_7_1
if ([[UIDevicecurrentDevice].systemVersionfloatValue] >= 8.0) {
//可以添加自定义categories
[APServiceregisterForRemoteNotificationTypes:(UIUserNotificationTypeBadge |
UIUserNotificationTypeSound |
UIUserNotificationTypeAlert)
categories:nil];
}else {
//categories 必须为nil
[APServiceregisterForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeSound |
UIRemoteNotificationTypeAlert)
categories:nil];
}
#else
//categories 必须为nil
[APService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeSound |
UIRemoteNotificationTypeAlert)
categories:nil];
#endif
[APServicesetupWithOption:launchOptions];
NSNotificationCenter *defaultCenter = [NSNotificationCenterdefaultCenter];
[defaultCenteraddObserver:selfselector:@selector(networkDidSetup:)name:kJPFNetworkDidSetupNotificationobject:nil];
[defaultCenteraddObserver:selfselector:@selector(networkDidClose:)name:kJPFNetworkDidCloseNotificationobject:nil];
[defaultCenteraddObserver:selfselector:@selector(networkDidRegister:)name:kJPFNetworkDidRegisterNotificationobject:nil];
[defaultCenteraddObserver:selfselector:@selector(networkDidLogin:)name:kJPFNetworkDidLoginNotificationobject:nil];
[defaultCenteraddObserver:selfselector:@selector(networkDidReceiveMessage:)name:kJPFNetworkDidReceiveMessageNotificationobject:nil];
//可以不写。不影响功能。
[APServicesetTags:[NSSetsetWithObjects:@"tag4",nil]alias:@"别名"callbackSelector:@selector(tagsAliasCallback:tags:alias:)target:self];
_customViewController=[[CustomViewControlleralloc]init];
self.window.rootViewController=_customViewController;
return YES;
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
[application setApplicationIconBadgeNumber:0];
[application cancelAllLocalNotifications];
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
NSLog(@"my device token is = %@",deviceToken);
[APServiceregisterDeviceToken:deviceToken];
//单推,可以和后台联系发送单推
NSString *registratationID = [APServiceregistrationID];
//通过这个registratationID可以进行单推功能
NSLog(@"registratationID is = %@",registratationID);
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{
NSLog(@"注册 device_token失败原因 %@",error);
}
#pragma mark iOS7 之前版本收到推送消息
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
[APServicehandleRemoteNotification:userInfo];
NSLog(@"iOS7之前版本收到推送消息: %@" ,[selflogDic:userInfo]);
//收到通知
NSString *topId = [NSStringstringWithFormat:@"%@",
[[userInfoobjectForKey:@"aps"]objectForKey:@"alert"]];
NSLog(@"topId = %@",topId);
//清除图标上的通知记录条数
application.applicationIconBadgeNumber =0;
}
#ifdef __IPHONE_7_0
#pragma mark iOS7 以后版本收到推送消息
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
[APServicehandleRemoteNotification:userInfo];
// completionHandler(UIBackgroundFetchResultNoData);
NSLog(@" iOS7以后版本收到推送消息: %@" ,[selflogDic:userInfo]);
application.applicationIconBadgeNumber =0;
}
#endif
#pragma mark 点击提示框的打开 本地通知
- (void)application:(UIApplication *)application
didReceiveLocalNotification:(UILocalNotification *)notification {
// 极光可以本地推送也可以APNs远程推送
// 还可以记录每个界面停留的时间以及打印crash日志
// 极光的自动显示出来提示框。一般关闭掉。自己写就OK
// [APService showLocalNotificationAtFront:notification identifierKey:nil];
//点击提示框的打开
application.applicationIconBadgeNumber =0;
NSLog(@"收到的本地推送的消息是%@",notification);
if (application.applicationState ==UIApplicationStateActive ) {
UIAlertView *alert = [[UIAlertViewalloc]initWithTitle:@"推送来了"message:notification.alertBodydelegate:nilcancelButtonTitle:@"确定"otherButtonTitles:notification.alertAction,nil];
[alertshow];
}
}
// log NSSet with UTF8
// if not ,log will be \Uxxx
- (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;
}
#pragma mark -
- (void)networkDidSetup:(NSNotification *)notification {
NSLog(@"已连接");
}
- (void)networkDidClose:(NSNotification *)notification {
NSLog(@"未连接。。。");
}
- (void)networkDidRegister:(NSNotification *)notification {
NSLog(@"已注册");
}
- (void)networkDidLogin:(NSNotification *)notification {
NSLog(@"已登录");
}
- (void)networkDidReceiveMessage:(NSNotification *)notification {
NSDictionary * userInfo = [notificationuserInfo];
NSString *title = [userInfovalueForKey:@"title"];
NSString *content = [userInfovalueForKey:@"content"];
NSDateFormatter *dateFormatter = [[NSDateFormatteralloc]init];
[dateFormattersetDateFormat:@"yyyy-MM-dd hh:mm:ss"];
NSString *message = [NSStringstringWithFormat:@"收到消息\ndate:%@\ntitle:%@\ncontent:%@", [dateFormatterstringFromDate:[NSDatedate]],title,content];
NSLog(@"message is %@",message);
}
- (void)tagsAliasCallback:(int)iResCode tags:(NSSet*)tags alias:(NSString*)alias {
NSLog(@"rescode: %d, \ntags: %@, \nalias: %@\n", iResCode, tags , alias);
}
@end