XGPush集成(信鸽集成)demo
2015-01-07 21:47
120 查看
#import "AppDelegate.h" #import "XGPush.h" #import "XGSetting.h" #define _IPHONE80_ 80000 @implementation AppDelegate - (void)registerPushForIOS8{ #if __IPHONE_OS_VERSION_MAX_ALLOWED >= _IPHONE80_ //Types UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert; //Actions UIMutableUserNotificationAction *acceptAction = [[UIMutableUserNotificationAction alloc] init]; acceptAction.identifier = @"ACCEPT_IDENTIFIER"; acceptAction.title = @"Accept"; acceptAction.activationMode = UIUserNotificationActivationModeForeground; acceptAction.destructive = NO; acceptAction.authenticationRequired = NO; //Categories UIMutableUserNotificationCategory *inviteCategory = [[UIMutableUserNotificationCategory alloc] init]; inviteCategory.identifier = @"INVITE_CATEGORY"; [inviteCategory setActions:@[acceptAction] forContext:UIUserNotificationActionContextDefault]; [inviteCategory setActions:@[acceptAction] forContext:UIUserNotificationActionContextMinimal]; NSSet *categories = [NSSet setWithObjects:inviteCategory, nil]; UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:categories]; [[UIApplication sharedApplication] registerUserNotificationSettings:mySettings]; [[UIApplication sharedApplication] registerForRemoteNotifications]; #endif } - (void)registerPush{ [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)]; } - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; //注册推送 [XGPush startApp:2200065428 appKey:@"IXJ8177VC3BG"]; //注销之后需要再次注册前的准备 void (^successCallback)(void) = ^(void){ //如果变成需要注册状态 if(![XGPush isUnRegisterStatus]) { //iOS8注册push方法 #if __IPHONE_OS_VERSION_MAX_ALLOWED >= _IPHONE80_ float sysVer = [[[UIDevice currentDevice] systemVersion] floatValue]; if(sysVer < 8){ [self registerPush]; } else{ [self registerPushForIOS8]; } NSLog(@"sysVer ----------- %g", sysVer); #else //iOS8之前注册push方法 //注册Push服务,注册后才能收到推送 [self registerPush]; #endif } }; [XGPush initForReregister:successCallback]; //推送反馈回调版本示例 void (^successBlock)(void) = ^(void){ //成功之后的处理 NSLog(@"[XGPush]handleLaunching's successBlock"); }; void (^errorBlock)(void) = ^(void){ //失败之后的处理 NSLog(@"[XGPush]handleLaunching's errorBlock"); }; [XGPush handleLaunching:launchOptions successCallback:successBlock errorCallback:errorBlock]; //角标清0 [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0]; //清除所有通知(包含本地通知) [XGPush clearLocalNotifications]; //本地推送示例 // [XGPush handleLaunching:launchOptions successCallback:successBlock errorCallback:errorBlock]; // // NSDate *fireDate = [[NSDate new] dateByAddingTimeInterval:5]; // // NSMutableDictionary *dicUserInfo = [[NSMutableDictionary alloc] init]; // [dicUserInfo setValue:@"myid" forKey:@"clockID"]; // NSDictionary *userInfo = dicUserInfo; // // [XGPush localNotification:fireDate alertBody:@"测试本地推送" badge:2 alertAction:@"确定" userInfo:userInfo]; [self.window makeKeyAndVisible]; return YES; } #pragma mark - Notification #if __IPHONE_OS_VERSION_MAX_ALLOWED >= _IPHONE80_ //注册UserNotification成功的回调 - (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings { //用户已经允许接收以下类型的推送 //UIUserNotificationType allowedTypes = [notificationSettings types]; NSLog(@"didRegisterUserNotificationSettings"); } //按钮点击事件回调 - (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void (^)())completionHandler{ if([identifier isEqualToString:@"ACCEPT_IDENTIFIER"]){ NSLog(@"ACCEPT_IDENTIFIER is clicked"); } completionHandler(); } #endif - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { NSLog(@"Token----------------: %@", [[NSString alloc] initWithData:deviceToken encoding:NSUTF8StringEncoding]); NSString * deviceTokenStr = [XGPush registerDevice:deviceToken]; //注册设备 [XGPush setAccount:@"123456"]; // [XGPush registerDevice:deviceToken]; void (^successBlock)(void) = ^(void){ //成功之后的处理 NSLog(@"[XGPush]register successBlock ,deviceToken: %@",deviceTokenStr); }; void (^errorBlock)(void) = ^(void){ //失败之后的处理 NSLog(@"[XGPush]register errorBlock"); }; [XGPush registerDevice:deviceToken successCallback:successBlock errorCallback:errorBlock]; //打印获取的deviceToken的字符串 NSLog(@"deviceTokenStr is %@",deviceTokenStr); } //如果deviceToken获取不到会进入此事件 - (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err { NSString *str = [NSString stringWithFormat: @"Error: %@",err]; NSLog(@"%@",str); } - (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo {// 取得 APNs 标准信息内容 NSDictionary *aps = [userInfo valueForKey:@"aps"]; NSString *content = [aps valueForKey:@"alert"]; //推送显示的内容 NSInteger badge = [[aps valueForKey:@"badge"] integerValue]; //badge数量 // badge += [UIApplication sharedApplication].applicationIconBadgeNumber; NSLog(@"didReceiveRemoteNotification badge = %ld, systemBadgeNumber = %ld", (long)badge, (long)[UIApplication sharedApplication].applicationIconBadgeNumber); // [APService setBadge:badge]; NSLog(@"data:%@ --- dic:%@", aps, userInfo); [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0]; // [[UIApplication sharedApplication] setApplicationIconBadgeNumber:badge]; NSString *sound = [aps valueForKey:@"sound"]; //播放的声音 // 取得自定义字段内容 NSString *customizeField1 = [userInfo valueForKey:@"customizeField1"]; //自定义参数,key是自己定义的 NSLog(@"content =[%@], badge=[%ld], sound=[%@], customize field =[%@]",content,(long)badge,sound,customizeField1); //推送反馈(app运行时) [XGPush handleReceiveNotification:userInfo]; } -(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{ //notification是发送推送时传入的字典信息 [XGPush localNotificationAtFrontEnd:notification userInfoKey:@"clockID" userInfoValue:@"myid"]; //删除推送列表中的这一条 // [XGPush delLocalNotification:notification]; //[XGPush delLocalNotification:@"clockID" userInfoValue:@"myid"]; //清空推送列表 //[XGPush clearLocalNotifications]; } @end
相关文章推荐
- XGPush集成(信鸽集成)
- 环信即时通讯-服务器端集成(REST API)-php版demo
- Spring Boot 集成RabbitMQ入门Demo
- ActiveMQ消息中间件 原理详解 &&附demo实现、以及与springboot集成的demo
- 【springboot】springboot项目集成swagger的demo
- android 视频开发 集成vitamio 播放rtsp m3u8 视频 有图有demo
- Struts2 json jQuery ajax 的集成配置与使用及 demo 演示源码示范
- spring+springmvc+mybatis+shiro+ehcache集成demo
- 腾讯 信鸽测试demo
- JAVA应用程序单独集成Mybatis使用Demo
- thinkphp集成系列之rbac的升级版auth权限管理系统demo
- 支付宝集成过程详解——运行DEMO
- 【Demo】springboot微服务+activemq消息中间件集成
- PHP集成腾讯云短信的Demo
- Android集成ZXing二维码扫描,附加竖屏并且不拉伸图片的demo
- 【网站国际化必备】Asp.Net MVC 集成Paypal(贝宝)快速结账 支付接口 ,附源码demo
- spring集成web socket小demo(demo1)
- dubbo+springmvc+mybatis集成demo
- Android项目集成信鸽推送
- 环信集成小demo - 1