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