1 #import "AppDelegate.h" 2 #import "XGPush.h" 3 #import "XGSetting.h" 4 5 #define _IPHONE80_ 80000 6 7 @implementation AppDelegate 8 - (void)registerPushForIOS8{ 9 #if __IPHONE_OS_VERSION_MAX_ALLOWED >= _IPHONE80_ 10 11 //Types 12 UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert; 13 14 //Actions 15 UIMutableUserNotificationAction *acceptAction = [[UIMutableUserNotificationAction alloc] init]; 16 17 acceptAction.identifier = @"ACCEPT_IDENTIFIER"; 18 acceptAction.title = @"Accept"; 19 20 acceptAction.activationMode = UIUserNotificationActivationModeForeground; 21 acceptAction.destructive = NO; 22 acceptAction.authenticationRequired = NO; 23 24 //Categories 25 UIMutableUserNotificationCategory *inviteCategory = [[UIMutableUserNotificationCategory alloc] init]; 26 27 inviteCategory.identifier = @"INVITE_CATEGORY"; 28 29 [inviteCategory setActions:@[acceptAction] forContext:UIUserNotificationActionContextDefault]; 30 31 [inviteCategory setActions:@[acceptAction] forContext:UIUserNotificationActionContextMinimal]; 32 33 NSSet *categories = [NSSet setWithObjects:inviteCategory, nil]; 34 35 36 UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:categories]; 37 38 [[UIApplication sharedApplication] registerUserNotificationSettings:mySettings]; 39 40 41 [[UIApplication sharedApplication] registerForRemoteNotifications]; 42 #endif 43 } 44 45 - (void)registerPush{ 46 [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)]; 47 } 48 49 50 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 51 { 52 self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 53 54 //注册推送 55 [XGPush startApp:2200065428 appKey:@"IXJ8177VC3BG"]; 56 57 //注销之后需要再次注册前的准备 58 void (^successCallback)(void) = ^(void){ 59 //如果变成需要注册状态 60 if(![XGPush isUnRegisterStatus]) 61 { 62 //iOS8注册push方法 63 #if __IPHONE_OS_VERSION_MAX_ALLOWED >= _IPHONE80_ 64 65 float sysVer = [[[UIDevice currentDevice] systemVersion] floatValue]; 66 if(sysVer < 8){ 67 [self registerPush]; 68 } 69 else{ 70 [self registerPushForIOS8]; 71 } 72 NSLog(@"sysVer ----------- %g", sysVer); 73 #else 74 //iOS8之前注册push方法 75 //注册Push服务,注册后才能收到推送 76 [self registerPush]; 77 #endif 78 } 79 }; 80 [XGPush initForReregister:successCallback]; 81 82 //推送反馈回调版本示例 83 void (^successBlock)(void) = ^(void){ 84 //成功之后的处理 85 NSLog(@"[XGPush]handleLaunching's successBlock"); 86 }; 87 88 void (^errorBlock)(void) = ^(void){ 89 //失败之后的处理 90 NSLog(@"[XGPush]handleLaunching's errorBlock"); 91 }; 92 [XGPush handleLaunching:launchOptions successCallback:successBlock errorCallback:errorBlock]; 93 //角标清0 94 [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0]; 95 96 //清除所有通知(包含本地通知) 97 [XGPush clearLocalNotifications]; 98 //本地推送示例 99 // [XGPush handleLaunching:launchOptions successCallback:successBlock errorCallback:errorBlock]; 100 // 101 // NSDate *fireDate = [[NSDate new] dateByAddingTimeInterval:5]; 102 // 103 // NSMutableDictionary *dicUserInfo = [[NSMutableDictionary alloc] init]; 104 // [dicUserInfo setValue:@"myid" forKey:@"clockID"]; 105 // NSDictionary *userInfo = dicUserInfo; 106 // 107 // [XGPush localNotification:fireDate alertBody:@"测试本地推送" badge:2 alertAction:@"确定" userInfo:userInfo]; 108 [self.window makeKeyAndVisible]; 109 return YES; 110 } 111 #pragma mark - Notification 112 113 #if __IPHONE_OS_VERSION_MAX_ALLOWED >= _IPHONE80_ 114 115 //注册UserNotification成功的回调 116 - (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings 117 { 118 //用户已经允许接收以下类型的推送 119 //UIUserNotificationType allowedTypes = [notificationSettings types]; 120 NSLog(@"didRegisterUserNotificationSettings"); 121 } 122 123 //按钮点击事件回调 124 - (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void (^)())completionHandler{ 125 if([identifier isEqualToString:@"ACCEPT_IDENTIFIER"]){ 126 NSLog(@"ACCEPT_IDENTIFIER is clicked"); 127 } 128 129 completionHandler(); 130 } 131 132 #endif 133 134 - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { 135 136 NSLog(@"Token----------------: %@", [[NSString alloc] initWithData:deviceToken encoding:NSUTF8StringEncoding]); 137 138 NSString * deviceTokenStr = [XGPush registerDevice:deviceToken]; 139 140 //注册设备 141 [XGPush setAccount:@"123456"]; 142 // [XGPush registerDevice:deviceToken]; 143 144 void (^successBlock)(void) = ^(void){ 145 //成功之后的处理 146 NSLog(@"[XGPush]register successBlock ,deviceToken: %@",deviceTokenStr); 147 }; 148 149 void (^errorBlock)(void) = ^(void){ 150 //失败之后的处理 151 NSLog(@"[XGPush]register errorBlock"); 152 }; 153 154 [XGPush registerDevice:deviceToken successCallback:successBlock errorCallback:errorBlock]; 155 156 //打印获取的deviceToken的字符串 157 NSLog(@"deviceTokenStr is %@",deviceTokenStr); 158 } 159 160 //如果deviceToken获取不到会进入此事件 161 - (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err { 162 163 NSString *str = [NSString stringWithFormat: @"Error: %@",err]; 164 165 NSLog(@"%@",str); 166 167 } 168 169 - (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo 170 {// 取得 APNs 标准信息内容 171 NSDictionary *aps = [userInfo valueForKey:@"aps"]; 172 NSString *content = [aps valueForKey:@"alert"]; //推送显示的内容 173 NSInteger badge = [[aps valueForKey:@"badge"] integerValue]; //badge数量 174 // badge += [UIApplication sharedApplication].applicationIconBadgeNumber; 175 176 NSLog(@"didReceiveRemoteNotification badge = %ld, systemBadgeNumber = %ld", (long)badge, (long)[UIApplication sharedApplication].applicationIconBadgeNumber); 177 // [APService setBadge:badge]; 178 NSLog(@"data:%@ --- dic:%@", aps, userInfo); 179 180 [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0]; 181 // [[UIApplication sharedApplication] setApplicationIconBadgeNumber:badge]; 182 NSString *sound = [aps valueForKey:@"sound"]; //播放的声音 183 184 // 取得自定义字段内容 185 NSString *customizeField1 = [userInfo valueForKey:@"customizeField1"]; //自定义参数,key是自己定义的 186 NSLog(@"content =[%@], badge=[%ld], sound=[%@], customize field =[%@]",content,(long)badge,sound,customizeField1); 187 //推送反馈(app运行时) 188 [XGPush handleReceiveNotification:userInfo]; 189 190 191 } 192 193 -(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{ 194 //notification是发送推送时传入的字典信息 195 [XGPush localNotificationAtFrontEnd:notification userInfoKey:@"clockID" userInfoValue:@"myid"]; 196 197 //删除推送列表中的这一条 198 // [XGPush delLocalNotification:notification]; 199 //[XGPush delLocalNotification:@"clockID" userInfoValue:@"myid"]; 200 201 //清空推送列表 202 //[XGPush clearLocalNotifications]; 203 } 204 205 206 @end
转载于:https://www.cnblogs.com/pretty-guy/p/4209497.html