1 1 #import "AppDelegate.h"
2 2 #import "XGPush.h"
3 3 #import "XGSetting.h"
4 4
5 5 #define _IPHONE80_ 80000
6 6
7 7 @implementation AppDelegate
8 8 - (void)registerPushForIOS8{
9 9 #if __IPHONE_OS_VERSION_MAX_ALLOWED >= _IPHONE80_
10 10
11 11 //Types
12 12 UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
13 13
14 14 //Actions
15 15 UIMutableUserNotificationAction *acceptAction = [[UIMutableUserNotificationAction alloc] init];
16 16
17 17 acceptAction.identifier = @"ACCEPT_IDENTIFIER";
18 18 acceptAction.title = @"Accept";
19 19
20 20 acceptAction.activationMode = UIUserNotificationActivationModeForeground;
21 21 acceptAction.destructive = NO;
22 22 acceptAction.authenticationRequired = NO;
23 23
24 24 //Categories
25 25 UIMutableUserNotificationCategory *inviteCategory = [[UIMutableUserNotificationCategory alloc] init];
26 26
27 27 inviteCategory.identifier = @"INVITE_CATEGORY";
28 28
29 29 [inviteCategory setActions:@[acceptAction] forContext:UIUserNotificationActionContextDefault];
30 30
31 31 [inviteCategory setActions:@[acceptAction] forContext:UIUserNotificationActionContextMinimal];
32 32
33 33 NSSet *categories = [NSSet setWithObjects:inviteCategory, nil];
34 34
35 35
36 36 UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:categories];
37 37
38 38 [[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
39 39
40 40
41 41 [[UIApplication sharedApplication] registerForRemoteNotifications];
42 42 #endif
43 43 }
44 44
45 45 - (void)registerPush{
46 46 [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
47 47 }
48 48
49 49
50 50 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
51 51 {
52 52 self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
53 53
54 54 //注册推送
55 55 [XGPush startApp:2200065428 appKey:@"IXJ8177VC3BG"];
56 56
57 57 //注销之后需要再次注册前的准备
58 58 void (^successCallback)(void) = ^(void){
59 59 //如果变成需要注册状态
60 60 if(![XGPush isUnRegisterStatus])
61 61 {
62 62 //iOS8注册push方法
63 63 #if __IPHONE_OS_VERSION_MAX_ALLOWED >= _IPHONE80_
64 64
65 65 float sysVer = [[[UIDevice currentDevice] systemVersion] floatValue];
66 66 if(sysVer < 8){
67 67 [self registerPush];
68 68 }
69 69 else{
70 70 [self registerPushForIOS8];
71 71 }
72 72 NSLog(@"sysVer ----------- %g", sysVer);
73 73 #else
74 74 //iOS8之前注册push方法
75 75 //注册Push服务,注册后才能收到推送
76 76 [self registerPush];
77 77 #endif
78 78 }
79 79 };
80 80 [XGPush initForReregister:successCallback];
81 81
82 82 //推送反馈回调版本示例
83 83 void (^successBlock)(void) = ^(void){
84 84 //成功之后的处理
85 85 NSLog(@"[XGPush]handleLaunching's successBlock");
86 86 };
87 87
88 88 void (^errorBlock)(void) = ^(void){
89 89 //失败之后的处理
90 90 NSLog(@"[XGPush]handleLaunching's errorBlock");
91 91 };
92 92 [XGPush handleLaunching:launchOptions successCallback:successBlock errorCallback:errorBlock];
93 93 //角标清0
94 94 [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
95 95
96 96 //清除所有通知(包含本地通知)
97 97 [XGPush clearLocalNotifications];
98 98 //本地推送示例
99 99 // [XGPush handleLaunching:launchOptions successCallback:successBlock errorCallback:errorBlock];
100 100 //
101 101 // NSDate *fireDate = [[NSDate new] dateByAddingTimeInterval:5];
102 102 //
103 103 // NSMutableDictionary *dicUserInfo = [[NSMutableDictionary alloc] init];
104 104 // [dicUserInfo setValue:@"myid" forKey:@"clockID"];
105 105 // NSDictionary *userInfo = dicUserInfo;
106 106 //
107 107 // [XGPush localNotification:fireDate alertBody:@"测试本地推送" badge:2 alertAction:@"确定" userInfo:userInfo];
108 108 [self.window makeKeyAndVisible];
109 109 return YES;
110 110 }
111 111 #pragma mark - Notification
112 112
113 113 #if __IPHONE_OS_VERSION_MAX_ALLOWED >= _IPHONE80_
114 114
115 115 //注册UserNotification成功的回调
116 116 - (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
117 117 {
118 118 //用户已经允许接收以下类型的推送
119 119 //UIUserNotificationType allowedTypes = [notificationSettings types];
120 120 NSLog(@"didRegisterUserNotificationSettings");
121 121 }
122 122
123 123 //按钮点击事件回调
124 124 - (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void (^)())completionHandler{
125 125 if([identifier isEqualToString:@"ACCEPT_IDENTIFIER"]){
126 126 NSLog(@"ACCEPT_IDENTIFIER is clicked");
127 127 }
128 128
129 129 completionHandler();
130 130 }
131 131
132 132 #endif
133 133
134 134 - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
135 135
136 136 NSLog(@"Token----------------: %@", [[NSString alloc] initWithData:deviceToken encoding:NSUTF8StringEncoding]);
137 137
138 138 NSString * deviceTokenStr = [XGPush registerDevice:deviceToken];
139 139
140 140 //注册设备
141 141 [XGPush setAccount:@"123456"];
142 142 // [XGPush registerDevice:deviceToken];
143 143
144 144 void (^successBlock)(void) = ^(void){
145 145 //成功之后的处理
146 146 NSLog(@"[XGPush]register successBlock ,deviceToken: %@",deviceTokenStr);
147 147 };
148 148
149 149 void (^errorBlock)(void) = ^(void){
150 150 //失败之后的处理
151 151 NSLog(@"[XGPush]register errorBlock");
152 152 };
153 153
154 154 [XGPush registerDevice:deviceToken successCallback:successBlock errorCallback:errorBlock];
155 155
156 156 //打印获取的deviceToken的字符串
157 157 NSLog(@"deviceTokenStr is %@",deviceTokenStr);
158 158 }
159 159
160 160 //如果deviceToken获取不到会进入此事件
161 161 - (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {
162 162
163 163 NSString *str = [NSString stringWithFormat: @"Error: %@",err];
164 164
165 165 NSLog(@"%@",str);
166 166
167 167 }
168 168
169 169 - (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo
170 170 {// 取得 APNs 标准信息内容
171 171 NSDictionary *aps = [userInfo valueForKey:@"aps"];
172 172 NSString *content = [aps valueForKey:@"alert"]; //推送显示的内容
173 173 NSInteger badge = [[aps valueForKey:@"badge"] integerValue]; //badge数量
174 174 // badge += [UIApplication sharedApplication].applicationIconBadgeNumber;
175 175
176 176 NSLog(@"didReceiveRemoteNotification badge = %ld, systemBadgeNumber = %ld", (long)badge, (long)[UIApplication sharedApplication].applicationIconBadgeNumber);
177 177 // [APService setBadge:badge];
178 178 NSLog(@"data:%@ --- dic:%@", aps, userInfo);
179 179
180 180 [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
181 181 // [[UIApplication sharedApplication] setApplicationIconBadgeNumber:badge];
182 182 NSString *sound = [aps valueForKey:@"sound"]; //播放的声音
183 183
184 184 // 取得自定义字段内容
185 185 NSString *customizeField1 = [userInfo valueForKey:@"customizeField1"]; //自定义参数,key是自己定义的
186 186 NSLog(@"content =[%@], badge=[%ld], sound=[%@], customize field =[%@]",content,(long)badge,sound,customizeField1);
187 187 //推送反馈(app运行时)
188 188 [XGPush handleReceiveNotification:userInfo];
189 189
190 190
191 191 }
192 192
193 193 -(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
194 194 //notification是发送推送时传入的字典信息
195 195 [XGPush localNotificationAtFrontEnd:notification userInfoKey:@"clockID" userInfoValue:@"myid"];
196 196
197 197 //删除推送列表中的这一条
198 198 // [XGPush delLocalNotification:notification];
199 199 //[XGPush delLocalNotification:@"clockID" userInfoValue:@"myid"];
200 200
201 201 //清空推送列表
202 202 //[XGPush clearLocalNotifications];
203 203 }
204 204
205 205
206 206 @end
207 复制代码