- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { NSLog(@"$$$$$$$$ %@",launchOptions); // 创建keyWindow self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds]; self.window.backgroundColor = [UIColor whiteColor];
loginViewController *login = [[loginViewController alloc]init]; [self.window setRootViewController:login]; // 设置为主窗口并且可见 [self.window makeKeyAndVisible]; // [1]:使用APPID/APPKEY/APPSECRENT创建个推实例 [self startSdkWith:kAppId appKey:kAppKey appSecret:kAppSecret]; // [2]:注册APNS [self registerRemoteNotification]; // 用户通过点击图标启动程序 还是 点击通知启动程序 // [2-EXT]: 获取启动时收到的APN NSDictionary* message = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]; if (message) { NSString *payloadMsg = [message objectForKey:@"payload"]; NSString *record = [NSString stringWithFormat:@"[APN]%@, %@", [NSDate date], payloadMsg]; self.window.rootViewController = self.viewController; }
[[UIApplication sharedApplication] cancelAllLocalNotifications]; [UIApplication sharedApplication].applicationIconBadgeNumber = 0; return YES; } - (void)applicationDidEnterBackground:(UIApplication *)application { // [EXT] 切后台关闭SDK,让SDK第一时间断线,让个推先用APN推送 [self stopSdk]; }
- (void)applicationDidBecomeActive:(UIApplication *)application { // [EXT] 重新上线 [self startSdkWith:_appID appKey:_appKey appSecret:_appSecret]; } - (void)applicationWillEnterForeground:(UIApplication *)application { [[UIApplication sharedApplication] cancelAllLocalNotifications]; [UIApplication sharedApplication].applicationIconBadgeNumber = 0; } - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken { NSString *token = [[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
_deviceToken = [token stringByReplacingOccurrencesOfString:@" " withString:@""]; NSLog(@"deviceToken:%@", _deviceToken); // [3]:向个推服务器注册deviceToken if (_gexinPusher) { [_gexinPusher registerDeviceToken:_deviceToken]; } }
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error { // [3-EXT]:如果APNS注册失败,通知个推服务器 if (_gexinPusher) { [_gexinPusher registerDeviceToken:@""]; } [_viewController logMsg:[NSString stringWithFormat:@"didFailToRegisterForRemoteNotificationsWithError:%@", [error localizedDescription]]]; }
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userinfo { [[UIApplication sharedApplication] cancelAllLocalNotifications]; [UIApplication sharedApplication].applicationIconBadgeNumber = 0; // [4-EXT]:处理APN NSString *payloadMsg = [userinfo objectForKey:@"payload"]; NSString *record = [NSString stringWithFormat:@"[APN]%@, %@", [NSDate date], payloadMsg]; [_viewController logMsg:record]; self.window.rootViewController = self.viewController;
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler { [[UIApplication sharedApplication] cancelAllLocalNotifications]; [UIApplication sharedApplication].applicationIconBadgeNumber = 0; // [4-EXT]:处理APN NSString *payloadMsg = [userInfo objectForKey:@"payload"]; NSDictionary *aps = [userInfo objectForKey:@"aps"]; NSNumber *contentAvailable = aps == nil ? nil : [aps objectForKeyedSubscript:@"content-available"]; NSString *record = [NSString stringWithFormat:@"[APN]%@, %@, [content-available: %@]", [NSDate date], payloadMsg, contentAvailable]; [_viewController logMsg:record]; if ([UIApplication sharedApplication].applicationState ==UIApplicationStateActive ) { orderInformationViewController *order = [[orderInformationViewController alloc]init]; [self.viewController presentViewController:order animated:YES completion:nil]; } completionHandler(UIBackgroundFetchResultNewData); }
- (void)startSdkWith:(NSString *)appID appKey:(NSString *)appKey appSecret:(NSString *)appSecret { if (!_gexinPusher) { _sdkStatus = SdkStatusStoped; self.appID = appID; self.appKey = appKey; self.appSecret = appSecret; _clientId = nil; NSError *err = nil; _gexinPusher = [GexinSdk createSdkWithAppId:_appID appKey:_appKey appSecret:_appSecret appVersion:@"0.0.0" delegate:self error:&err]; if (!_gexinPusher) { [_viewController logMsg:[NSString stringWithFormat:@"%@", [err localizedDescription]]]; } else { _sdkStatus = SdkStatusStarting; } [_viewController updateStatusView:self]; } }
- (void)stopSdk { if (_gexinPusher) { [_gexinPusher destroy]; _gexinPusher = nil; _sdkStatus = SdkStatusStoped; _clientId = nil; [_viewController updateStatusView:self]; } }
- (BOOL)checkSdkInstance { if (!_gexinPusher) { UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"错误" message:@"SDK未启动" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil]; [alertView show];
return NO; } return YES; }
- (void)setDeviceToken:(NSString *)aToken { if (![self checkSdkInstance]) { return; } [_gexinPusher registerDeviceToken:aToken]; }
- (BOOL)setTags:(NSArray *)aTags error:(NSError **)error { if (![self checkSdkInstance]) { return NO; } return [_gexinPusher setTags:aTags]; }
- (NSString *)sendMessage:(NSData *)body error:(NSError **)error { if (![self checkSdkInstance]) { return nil; } return [_gexinPusher sendMessage:body error:error]; }
- (void)bindAlias:(NSString *)aAlias { if (![self checkSdkInstance]) { return; } return [_gexinPusher bindAlias:aAlias]; }
- (void)unbindAlias:(NSString *)aAlias { if (![self checkSdkInstance]) { return; } return [_gexinPusher unbindAlias:aAlias]; } - (void)testGetClientId { NSString *clientId = [_gexinPusher clientId]; UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"当前的CID" message:clientId delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
[alertView show]; }
#pragma mark - GexinSdkDelegate - (void)GexinSdkDidRegisterClient:(NSString *)clientId { // [4-EXT-1]: 个推SDK已注册 _sdkStatus = SdkStatusStarted;
_clientId = clientId; [_viewController updateStatusView:self]; // [self stopSdk]; }
- (void)GexinSdkDidReceivePayload:(NSString *)payloadId fromApplication:(NSString *)appId { // [4]: 收到个推消息
_payloadId = payloadId; NSData *payload = [_gexinPusher retrivePayloadById:payloadId]; NSString *payloadMsg = nil; if (payload) { payloadMsg = [[NSString alloc] initWithBytes:payload.bytes length:payload.length encoding:NSUTF8StringEncoding]; } [_viewController logMsg:payloadMsg]; if (payloadMsg!=nil) { NSData *jsonData = [payloadMsg dataUsingEncoding:NSUTF8StringEncoding]; NSError *err; NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&err]; if(err) { NSLog(@"json解析失败:%@",err); return ; } //1. 创建一个plist文件 NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); NSString *patho=[paths objectAtIndex:0]; NSLog(@"path = %@",patho); NSString *filename=[patho stringByAppendingPathComponent:@"dataPlist.plist"]; NSFileManager* fm = [NSFileManager defaultManager]; [fm createFileAtPath:filename contents:nil attributes:nil]; //创建一个dic,写到plist文件里 NSMutableDictionary* dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:dic[@"address"],@"address",dic[@"bespeak_time"],@"bespeak_time",dic[@"car_model"],@"car_model",dic[@"id"],@"id",dic[@"car_num"],@"car_num",dic[@"create_time"],@"create_time",dic[@"pay_price"],@"pay_price",dic[@"pay_type"],@"pay_type",dic[@"price"],@"price",dic[@"service_begin_time"],@"service_begin_time",dic[@"service_end_time"],@"service_end_time",dic[@"settlement_time"],@"settlement_time",dic[@"status"],@"status",dic[@"wash_type"],@"wash_type",nil]; [dict writeToFile:filename atomically:YES]; //读文件 NSMutableDictionary* dict2 = [NSMutableDictionary dictionaryWithContentsOfFile:filename]; NSLog(@"dic is:%@",dict2); }
} - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { } - (void)GexinSdkDidSendMessage:(NSString *)messageId result:(int)result { // [4-EXT]:发送上行消息结果反馈 NSString *record = [NSString stringWithFormat:@"Received sendmessage:%@ result:%d", messageId, result]; [_viewController logMsg:record]; }
- (void)GexinSdkDidOccurError:(NSError *)error { // [EXT]:个推错误报告,集成步骤发生的任何错误都在这里通知,如果集成后,无法正常收到消息,查看这里的通知。 [_viewController logMsg:[NSString stringWithFormat:@">>>[GexinSdk error]:%@", [error localizedDescription]]]; }
@end |