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
//                __    __                ________
//    | |    | |  \ \  / /  | |    | |   / _______|
//    | |____| |   \ \/ /   | |____| |  / /
//    | |____| |    \  /    | |____| |  | |   _____
//    | |    | |    /  \    | |    | |  | |  |____ |
//  | |    | |   / /\ \   | |    | |  \ \______| |
//  | |    | |  /_/  \_\  | |    | |   \_________|
//
//    Copyright (c) 2012年 HXHG. All rights reserved.
//    http://www.jpush.cn
//  Created by Zhanghao
//
 
#import "setLocalNotificationViewController.h"
#import "JPUSHService.h"
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h>
#endif
 
@interface setLocalNotificationViewController () {
  CGRect _frame;
}
@end
 
@implementation setLocalNotificationViewController {
  id _notification;
}
 
- (void)viewDidLoad {
  [super viewDidLoad];
  int fixLength;
#ifdef __IPHONE_7_0
  if ([[UIDevice currentDevice].systemVersion floatValue] >= 7.0) {
    fixLength = 0;
  } else {
    fixLength = 20;
  }
#else
  fixLength = 20;
#endif
  _frame =
      CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y - fixLength,
                 self.view.frame.size.width, self.view.frame.size.height);
  // Do any additional setup after loading the view from its nib.
}
- (IBAction)setNotification:(id)sender {
// v2.1.9版以前方式
//  _notification = [JPUSHService
//      setLocalNotification:_notificationDatePicker.date
//                 alertBody:_notificationBodyTextField.text
//                     badge:[_notificationBadgeTextField.text intValue]
//               alertAction:_notificationButtonTextField.text
//             identifierKey:_notificationIdentifierTextField.text
//                  userInfo:nil
//                 soundName:nil];
//  [self clearAllInput];
//  NSString *result;
//  if (_notification) {
//    result = @"设置本地通知成功";
//  } else {
//    result = @"设置本地通知失败";
//  }
//  UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"设置"
//                                                  message:@"设置成功"
//                                                 delegate:self
//                                        cancelButtonTitle:@"确定"
//                                        otherButtonTitles:nil, nil];
//  [alert show];
  
  // v2.1.9版以后方式
  JPushNotificationContent *content = [[JPushNotificationContent alloc] init];
  content.body = _notificationBodyTextField.text;
  content.badge = @([_notificationBadgeTextField.text intValue]);
  content.action = _notificationButtonTextField.text;
  JPushNotificationTrigger *trigger = [[JPushNotificationTrigger alloc] init];
  if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 10.0) {
    trigger.timeInterval = [_notificationDatePicker.date timeIntervalSinceNow]; // iOS10以上有效
  }
  else {
    trigger.fireDate = _notificationDatePicker.date; // iOS10以下有效
  }
  JPushNotificationRequest *request = [[JPushNotificationRequest alloc] init];
  request.content = content;
  request.trigger = trigger;
  request.requestIdentifier = _notificationIdentifierTextField.text;
  request.completionHandler = ^(id result) {
    NSLog(@"%@", result); // iOS10以上成功则result为UNNotificationRequest对象,失败则result为nil;iOS10以下成功result为UILocalNotification对象,失败则result为nil
    _notification = result;
    if (result) {
      void (^block)() = ^() {
//      [self clearAllInput];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"设置"
                                                        message:@"设置成功"
                                                       delegate:self
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:nil, nil];
        [alert show];
      };
      if ([NSThread isMainThread]) {
        block();
      }
      else {
        dispatch_async(dispatch_get_main_queue(), block);
      }
    }
  };
  [JPUSHService addNotification:request];
}
 
- (void)clearAllInput {
  _notificationBadgeTextField.text = nil;
  _notificationBodyTextField.text = @"";
  _notificationButtonTextField.text = @"";
  _notificationDatePicker.date = [[NSDate new] dateByAddingTimeInterval:0];
  _notificationIdentifierTextField.text = @"";
}
 
- (IBAction)clearAllNotification:(id)sender {
  // v2.1.9版以前方式
//  [JPUSHService deleteLocalNotificationWithIdentifierKey:@"test"];
//  [JPUSHService deleteLocalNotification:_notification];
//  [JPUSHService clearAllLocalNotifications];
  
  // v2.1.9版以后方式
  [JPUSHService removeNotification:nil];
//  JPushNotificationIdentifier *identifier = [[JPushNotificationIdentifier alloc] init];
//  identifier.identifiers = @[@"test"];  // iOS10以上还需要设置delivered标志
//  if ([[[UIDevice currentDevice] systemVersion] floatValue] < 10.0) {
//    identifier.notificationObj = _notification;
//  }
//  else {
//#ifdef NSFoundationVersionNumber_iOS_9_x_Max
//    UNNotificationRequest *request = _notification;
//    identifier.identifiers = @[request.identifier];  // 还需要设置delivered标志
//#endif
//  }
//  [JPUSHService removeNotification:identifier];
  
  UIAlertView *alert =
      [[UIAlertView alloc] initWithTitle:@"设置"
                                 message:@"取消所有本地通知成功"
                                delegate:self
                       cancelButtonTitle:@"确定"
                       otherButtonTitles:nil, nil];
  [alert show];
}
 
- (IBAction)clearLastNotification {
  NSString *alertMessage;
  if (_notification) {
    // v2.1.9版以前方式
//    [JPUSHService deleteLocalNotification:_notification];
    
    // v2.1.9版以后方式
    JPushNotificationIdentifier *identifier = [[JPushNotificationIdentifier alloc] init];
    if ([[[UIDevice currentDevice] systemVersion] floatValue] < 10.0) {
      identifier.notificationObj = _notification;
    }
    else {
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
      UNNotificationRequest *request = _notification;
      identifier.identifiers = @[request.identifier]; // 还需要设置delivered标志
#endif
    }
    [JPUSHService removeNotification:identifier];
    
    _notification = nil;
    alertMessage = @"取消上一个通知成功";
  } else {
    alertMessage = @"不存在上一个设置通知";
  }
  UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"设置"
                                                  message:alertMessage
                                                 delegate:self
                                        cancelButtonTitle:@"确定"
                                        otherButtonTitles:nil, nil];
  [alert show];
}
 
- (void)textFieldDidBeginEditing:(UITextField *)textField {
  _backgroundView.frame = CGRectMake(_frame.origin.x, _frame.origin.y - 110,
                                     _frame.size.width, _frame.size.height);
}
 
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
  [textField resignFirstResponder];
  _backgroundView.frame = _frame;
  return YES;
}
 
- (BOOL)textField:(UITextField *)textField
    shouldChangeCharactersInRange:(NSRange)range
                replacementString:(NSString *)string {
  if (textField.tag != 10) {
    return YES;
  }
  return YES;
}
 
- (IBAction)View_TouchDown:(id)sender {
  // 发送resignFirstResponder.
  [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder)
                                             to:nil
                                           from:nil
                                       forEvent:nil];
  _backgroundView.frame = _frame;
}
 
- (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