#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: 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 < ){
[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:]; //清除所有通知(包含本地通知)
[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:@""];
// [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:];
// [[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
复制代码

最新文章

  1. 展望 2017年商业智能BI 发展的趋势
  2. C# 利用SQLite对.DB和.logdb加密和解密和SQLite创建数据库
  3. [Effective JavaScript 笔记]第41条:将原型视为实现细节
  4. shell软件工具设计的原则_转
  5. poj 1475 || zoj 249 Pushing Boxes
  6. 一台服务器搭载两个tomcat项目
  7. Objective-C 中的方法回掉
  8. jquery 获取选中的文字.当前光标所在的位置等jquery-fieldselection 插件
  9. python游戏编程——跟13岁儿童学编程
  10. 卓尼斯ZT-180评測
  11. Devpexpress 打印预览问题
  12. VxWorks6.6 pcPentium BSP 使用说明(二):创建启动盘
  13. Delphi的String内存结构(够清楚) good
  14. swift类名称显示变量
  15. Android jni 编程2(对基本类型一维整型数组的操作)
  16. Python创建二维数组(关于list的一个小坑)
  17. BZOJ 1260:[CQOI2007]涂色paint
  18. iOS开发基础-九宫格坐标(2)之模型
  19. Winform 基础二 最小化 最大化 关闭 点击任务栏隐藏显示 点击鼠标左键移动窗体
  20. SPI内容随笔

热门文章

  1. C++继承与派生的概念、什么是继承和派生
  2. iOS开发相关图书推荐
  3. ACCESS TOKEN
  4. JavaScript如何判断参数为浮点型
  5. 使用CocoaPods管理依赖库
  6. CentOs中iptables配置允许mysql远程访问
  7. ASP.NET WebForm中前台代码如何绑定后台变量
  8. POJ 2777 Count Color (线段树成段更新+二进制思维)
  9. UVaLive 7374 Racing Gems (DP,LIS)
  10. UITableView section header 不固定