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) file application: (* the UIApplication) file application didFinishLaunchingWithOptions: (NSDictionary * ) launchOptions 51 is { 52 is self.window = [[the UIWindow the alloc] the initWithFrame: [[The UIScreen mainScreen] bounds]]; 53 is 54 is // Register push 55 [XGPush the startApp: 2,200,065,428 AppKey: @ " IXJ8177VC3BG " ]; 56 is 57 is // after cancellation need to register again before the preparation of the 58 void (^ successCallback) ( void ) = ^ ( void ) { 59 // if required into state register 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 is #else 74 // before the push method iOS8 register 75 // registration Push service registration in order to receive a push 76 [Self registerPush]; 77 #endif 78 } 79 }; 80 [XGPush initForReregister: successCallback]; 81 82 // push feedback correction exemplary version 83 void (^ successBlock) ( void ) = ^ ( void ) { 84 // process after a successful 85 NSLog ( @ " [XGPush] handleLaunching apos successBlock " ); 86 }; 87 88 void (^ errorBlock) ( void ) = ^ ( void ) { 89 // process after the failure of 90 NSLog ( @ " [XGPush] handleLaunching apos errorBlock " ); 91 is }; 92 [XGPush handleLaunching: launchOptions successCallback: successBlock errorCallback: errorBlock]; 93 // angle SD 0 94 [[sharedApplication the UIApplication] setApplicationIconBadgeNumber: 0 ]; 95 96 // clears all notification (including local notification) 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 - the Notification 112 113 #if __IPHONE_OS_VERSION_MAX_ALLOWED> = _IPHONE80_ 114 115 // registration successful callback UserNotification 1 16 - ( void ) file application: (* the UIApplication) file application didRegisterUserNotificationSettings: (UIUserNotificationSettings * ) notificationSettings 117 { 1 18 // user has received allows the following types of push 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 // process after a successful 146 NSLog ( @ " [XGPush] Register successBlock, deviceToken:% @ " , deviceTokenStr); 147 }; 148 149 void (^ errorBlock) ( void ) = ^ ( void ) { 150 // process after the failure of 151 NSLog ( @ " [XGPush] errorBlock Register " ); 152 }; 153 154 [XGPush registerDevice: deviceToken successCallback: successBlock errorCallback: errorBlock]; 155 156 // print the acquired character string deviceToken 157 NSLog ( @ " deviceTokenStr IS% @ " , deviceTokenStr); 158 } 159 160. // If you will not obtain deviceToken enter the event 161 - ( void ) file application: (the UIApplication *) App didFailToRegisterForRemoteNotificationsWithError: (the NSError * ) ERR { 162 163 NSString * STR = [NSString stringWithFormat: @ " Error:% @ " , ERR]; 164 is 165 NSLog ( @ "@% " , STR); 166 167 is } 168 169 - ( void ) file application: (* the UIApplication) file application didReceiveRemoteNotification: (NSDictionary * ) the userInfo 170. { // obtain APNs standard content 171 is NSDictionary APS * = [the userInfo valueForKey: @" APS " ]; 172 NSString * content = [APS valueForKey: @" Alert " ]; // pushed content display 173 NSInteger badge = [[APS valueForKey: @" badge " ] integerValue]; // number 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 " ]; // sound played 183 184 // get contents of the custom field 185 NSString * customizeField1 = [userInfo valueForKey: @ " customizeField1 " ]; // custom parameters, key own definition 186 NSLog ( @" Content = [% @], badge = [% LD], Sound = [% @], customize = Field [% @] " , Content, ( Long ) badge, Sound, customizeField1); 187 // push feedback (app runtime) 188 [XGPush handleReceiveNotification: the userInfo]; 189 190 191 } 192 193 - ( void ) file application: (* the UIApplication) file application didReceiveLocalNotification: (UILocalNotification * ) Notification { 194 // Notification is passed when the dictionary information sending push 195 [XGPush localNotificationAtFrontEnd: userInfoKey the Notification: @ " a clockid " userInfoValue: @ " myid " ]; 196 197 // delete push it a list of 198 // [XGPush delLocalNotification: the Notification]; 199 //[XGPush delLocalNotification: @ "with a clockid of" userInfoValue: @ "MyID"]; 200 is 201 // empty list push 202 // [XGPush clearLocalNotifications]; 203 } 204 205 206 @end
Reproduced in: https: //www.cnblogs.com/pretty-guy/p/4209497.html