Hale Cai
2018-08-10 42c80d0b43ff8736edb5be4bbfcbc7a99fc8a119
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
//                __    __                ________
//    | |    | |  \ \  / /  | |    | |   / _______|
//    | |____| |   \ \/ /   | |____| |  / /
//    | |____| |    \  /    | |____| |  | |   _____
//    | |    | |    /  \    | |    | |  | |  |____ |
//  | |    | |   / /\ \   | |    | |  \ \______| |
//  | |    | |  /_/  \_\  | |    | |   \_________|
//
//    Copyright (c) 2012年 HXHG. All rights reserved.
//    http://www.jpush.cn
//  Created by Zhanghao
//
 
#import "RootViewController.h"
#import "JPUSHService.h"
#import "AppDelegate.h"
 
@interface RootViewController ()
 
@end
 
@implementation RootViewController
 
- (void)viewDidLoad {
  [super viewDidLoad];
  _messageCount = 0;
  _notificationCount = 0;
  _messageContents = [[NSMutableArray alloc] initWithCapacity:6];
  NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
  [defaultCenter addObserver:self
                    selector:@selector(networkDidSetup:)
                        name:kJPFNetworkDidSetupNotification
                      object:nil];
  [defaultCenter addObserver:self
                    selector:@selector(networkDidClose:)
                        name:kJPFNetworkDidCloseNotification
                      object:nil];
  [defaultCenter addObserver:self
                    selector:@selector(networkDidRegister:)
                        name:kJPFNetworkDidRegisterNotification
                      object:nil];
  [defaultCenter addObserver:self
                    selector:@selector(networkDidLogin:)
                        name:kJPFNetworkDidLoginNotification
                      object:nil];
  [defaultCenter addObserver:self
                    selector:@selector(networkDidReceiveMessage:)
                        name:kJPFNetworkDidReceiveMessageNotification
                      object:nil];
  [defaultCenter addObserver:self
                    selector:@selector(serviceError:)
                        name:kJPFServiceErrorNotification
                      object:nil];
 
// Do any additional setup after loading the view from its nib.
 
  _UDIDValueLabel.textColor = [UIColor colorWithRed:0.0 / 255
                                              green:122.0 / 255
                                               blue:255.0 / 255
                                              alpha:1];
  _registrationValueLabel.text = [JPUSHService registrationID];
  _registrationValueLabel.textColor = [UIColor colorWithRed:0.0 / 255
                                                      green:122.0 / 255
                                                       blue:255.0 / 255
                                                      alpha:1];
  // show appKey
  NSString *appKey = [self getAppKey];
  if (appKey) {
    _appKeyLabel.text = appKey;
    _appKeyLabel.textColor = [UIColor colorWithRed:0.0 / 255
                                             green:122.0 / 255
                                              blue:255.0 / 255
                                             alpha:1];
  }
}
 
//获取appKey
- (NSString *)getAppKey {
  return [appKey lowercaseString];
}
 
- (void)dealloc {
  [self unObserveAllNotifications];
}
 
- (void)unObserveAllNotifications {
  NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
  [defaultCenter removeObserver:self
                           name:kJPFNetworkDidSetupNotification
                         object:nil];
  [defaultCenter removeObserver:self
                           name:kJPFNetworkDidCloseNotification
                         object:nil];
  [defaultCenter removeObserver:self
                           name:kJPFNetworkDidRegisterNotification
                         object:nil];
  [defaultCenter removeObserver:self
                           name:kJPFNetworkDidLoginNotification
                         object:nil];
  [defaultCenter removeObserver:self
                           name:kJPFNetworkDidReceiveMessageNotification
                         object:nil];
  [defaultCenter removeObserver:self
                           name:kJPFServiceErrorNotification
                         object:nil];
}
 
- (void)networkDidSetup:(NSNotification *)notification {
  _netWorkStateLabel.text = @"已连接";
  NSLog(@"已连接");
  _netWorkStateLabel.textColor = [UIColor colorWithRed:0.0 / 255
                                                 green:122.0 / 255
                                                  blue:255.0 / 255
                                                 alpha:1];
}
 
- (void)networkDidClose:(NSNotification *)notification {
  _netWorkStateLabel.text = @"未连接。。。";
  NSLog(@"未连接");
  _netWorkStateLabel.textColor = [UIColor redColor];
}
 
- (void)networkDidRegister:(NSNotification *)notification {
  NSLog(@"%@", [notification userInfo]);
  _netWorkStateLabel.text = @"已注册";
  _netWorkStateLabel.textColor = [UIColor colorWithRed:0.0 / 255
                                                 green:122.0 / 255
                                                  blue:255.0 / 255
                                                 alpha:1];
  _registrationValueLabel.text =
      [[notification userInfo] valueForKey:@"RegistrationID"];
  _registrationValueLabel.textColor = [UIColor colorWithRed:0.0 / 255
                                                      green:122.0 / 255
                                                       blue:255.0 / 255
                                                      alpha:1];
  NSLog(@"已注册");
}
 
- (void)networkDidLogin:(NSNotification *)notification {
  _netWorkStateLabel.text = @"已登录";
  _netWorkStateLabel.textColor = [UIColor colorWithRed:0.0 / 255
                                                 green:122.0 / 255
                                                  blue:255.0 / 255
                                                 alpha:1];
  NSLog(@"已登录");
 
  if ([JPUSHService registrationID]) {
    _registrationValueLabel.text = [JPUSHService registrationID];
    NSLog(@"get RegistrationID");
  }
}
 
- (void)networkDidReceiveMessage:(NSNotification *)notification {
  NSDictionary *userInfo = [notification userInfo];
  NSString *title = [userInfo valueForKey:@"title"];
  NSString *content = [userInfo valueForKey:@"content"];
  NSDictionary *extra = [userInfo valueForKey:@"extras"];
  NSUInteger messageID = [[userInfo valueForKey:@"_j_msgid"] unsignedIntegerValue];
 
  NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
 
  [dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
 
  NSString *currentContent = [NSString
      stringWithFormat:
          @"收到自定义消息:%@\ntitle:%@\ncontent:%@\nextra:%@\nmessage:%ld\n",
          [NSDateFormatter localizedStringFromDate:[NSDate date]
                                         dateStyle:NSDateFormatterNoStyle
                                         timeStyle:NSDateFormatterMediumStyle],
                              title, content, [self logDic:extra],(unsigned long)messageID];
  NSLog(@"%@", currentContent);
 
  [_messageContents insertObject:currentContent atIndex:0];
 
  NSString *allContent = [NSString
      stringWithFormat:@"%@收到消息:\n%@\nextra:%@",
                       [NSDateFormatter
                           localizedStringFromDate:[NSDate date]
                                         dateStyle:NSDateFormatterNoStyle
                                         timeStyle:NSDateFormatterMediumStyle],
                       [_messageContents componentsJoinedByString:nil],
                       [self logDic:extra]];
 
  _messageContentView.text = allContent;
  _messageCount++;
  [self reloadMessageCountLabel];
}
 
// log NSSet with UTF8
// if not ,log will be \Uxxx
- (NSString *)logDic:(NSDictionary *)dic {
  if (![dic count]) {
    return nil;
  }
  NSString *tempStr1 =
      [[dic description] stringByReplacingOccurrencesOfString:@"\\u"
                                                   withString:@"\\U"];
  NSString *tempStr2 =
      [tempStr1 stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];
  NSString *tempStr3 =
      [[@"\"" stringByAppendingString:tempStr2] stringByAppendingString:@"\""];
  NSData *tempData = [tempStr3 dataUsingEncoding:NSUTF8StringEncoding];
  NSString *str =
      [NSPropertyListSerialization propertyListFromData:tempData
                                       mutabilityOption:NSPropertyListImmutable
                                                 format:NULL
                                       errorDescription:NULL];
  return str;
}
 
- (void)serviceError:(NSNotification *)notification {
  NSDictionary *userInfo = [notification userInfo];
  NSString *error = [userInfo valueForKey:@"error"];
  NSLog(@"%@", error);
}
 
- (void)addNotificationCount {
  _notificationCount++;
  [self reloadNotificationCountLabel];
}
 
- (void)addMessageCount {
  _messageCount++;
  [self reloadMessageCountLabel];
}
 
- (void)reloadMessageContentView {
  _messageContentView.text = @"";
}
 
- (void)reloadMessageCountLabel {
  _messageCountLabel.text = [NSString stringWithFormat:@"%d", _messageCount];
}
 
- (void)reloadNotificationCountLabel {
  _notificationCountLabel.text =
      [NSString stringWithFormat:@"%d", _notificationCount];
}
 
- (IBAction)cleanMessage:(id)sender {
  _messageCount = 0;
  _notificationCount = 0;
  [self reloadMessageCountLabel];
  [_messageContents removeAllObjects];
  [self reloadMessageContentView];
  self.notificationCountLabel.text = @"0";
}
 
- (void)viewWillAppear:(BOOL)animated {
  [super viewWillAppear:animated];
}
 
- (void)viewWillDisappear:(BOOL)animated {
  [super viewWillDisappear:animated];
}
 
- (void)didReceiveMemoryWarning {
  [super didReceiveMemoryWarning];
  // Dispose of any resources that can be recreated.
}
 
@end