ViewController.m 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. //
  2. // ViewController.m
  3. // AJPushDemo
  4. //
  5. // Created by Black on 2021/2/3.
  6. // Copyright © 2018 anji-plus 安吉加加信息技术有限公司 http://www.anji-plus.com. All rights reserved.
  7. //
  8. #import "ViewController.h"
  9. #import "SettingController.h"
  10. #import "RequestManager.h"
  11. #import "SettingManager.h"
  12. #import <JPush/JPUSHService.h>
  13. #import <AJPushSDK/AJPushSDK.h>
  14. #import "Toast.h"
  15. #import "AJScrollView.h"
  16. @interface ViewController ()<UIScrollViewDelegate, UITextFieldDelegate,UITextViewDelegate>
  17. @property (nonatomic, strong) UITextView *textview;
  18. @property (nonatomic, strong) AJScrollView *scrollView;
  19. @property (nonatomic, strong) UITextField *titleText;
  20. @property (nonatomic, strong) UITextField *contentText;
  21. @property (nonatomic, strong) UITextField *pushIdText;
  22. @property (nonatomic, strong) UIButton *pushButton;
  23. @property (nonatomic, strong) UIButton *passthroughButton;
  24. @property (nonatomic, strong) UIToolbar * toobar;
  25. @end
  26. @implementation ViewController
  27. - (void)viewDidLoad {
  28. [super viewDidLoad];
  29. [self.view setBackgroundColor:[UIColor colorWithRed:245/255.0 green:245/255.0 blue:245/255.0 alpha:1]];
  30. self.title = @"AJPushDemo";
  31. [self buildSetting];
  32. [self setUI];
  33. //添加推送通知
  34. [self initNSNotificationCenter];
  35. [self registerForKeyboardNotifications];
  36. }
  37. - (void)setUI {
  38. CGRect bounds = [[UIScreen mainScreen] bounds];
  39. _toobar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 38.0f)];
  40. _toobar.translucent = YES;
  41. _toobar.barStyle = UIBarStyleDefault;
  42. UIBarButtonItem * spaceBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
  43. UIBarButtonItem * doneBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:self action:@selector(resignKeyboard)];
  44. [_toobar setItems:@[spaceBarButtonItem,doneBarButtonItem]];
  45. self.scrollView = [[AJScrollView alloc]initWithFrame:bounds];
  46. self.scrollView.backgroundColor = UIColor.orangeColor;
  47. [_scrollView setDelegate:self];
  48. UILabel *toastLabel = [[UILabel alloc]init];
  49. toastLabel.frame = CGRectMake(10, 20, bounds.size.width-20, 1);
  50. toastLabel.textColor = [UIColor redColor];
  51. [self.scrollView addSubview:toastLabel];
  52. UILabel *label = [[UILabel alloc]init];
  53. label.frame = CGRectMake(10, CGRectGetMaxY(toastLabel.frame) + 20, 80, 30);
  54. label.text = @"标题:";
  55. [self.scrollView addSubview:label];
  56. self.titleText = [[UITextField alloc]init];
  57. self.titleText.frame = CGRectMake(CGRectGetMaxX(label.frame) + 5, CGRectGetMaxY(toastLabel.frame) + 20, bounds.size.width-CGRectGetMaxX(label.frame) - 20, 30);
  58. self.titleText.placeholder = @"请输入推送/透传标题";
  59. self.titleText.text = @"iOS发送推送/透传标题";
  60. self.titleText.clearButtonMode=UITextFieldViewModeWhileEditing;
  61. self.titleText.delegate=self;
  62. self.titleText.backgroundColor = [UIColor whiteColor];
  63. self.titleText.inputAccessoryView = _toobar;
  64. [self.scrollView addSubview:self.titleText];
  65. UILabel *label1 = [[UILabel alloc]init];
  66. label1.frame = CGRectMake(10, CGRectGetMaxY(self.titleText.frame)+20, 80, 30);
  67. label1.text = @"内容:";
  68. [self.scrollView addSubview:label1];
  69. //
  70. self.contentText = [[UITextField alloc]init];
  71. self.contentText.frame = CGRectMake(CGRectGetMaxX(label1.frame) + 5, CGRectGetMaxY(self.titleText.frame)+20, bounds.size.width-CGRectGetMaxX(label.frame) - 20, 30);
  72. self.contentText.placeholder = @"请输入推送/透传内容";
  73. self.contentText.text = @"iOS发送推送/透传内容";
  74. self.contentText.delegate=self;
  75. self.contentText.clearButtonMode=UITextFieldViewModeWhileEditing;
  76. self.contentText.backgroundColor = [UIColor whiteColor];
  77. self.contentText.inputAccessoryView = _toobar;
  78. [self.scrollView addSubview:self.contentText];
  79. UILabel *label2 = [[UILabel alloc]init];
  80. label2.frame = CGRectMake(10, CGRectGetMaxY(self.contentText.frame)+20, 80, 30);
  81. label2.text = @"推送id:";
  82. [self.scrollView addSubview:label2];
  83. self.pushIdText = [[UITextField alloc]init];
  84. self.pushIdText.frame = CGRectMake(CGRectGetMaxX(label2.frame) + 5, CGRectGetMaxY(self.contentText.frame)+20, bounds.size.width-CGRectGetMaxX(label2.frame) - 20 - 60, 30);
  85. self.pushIdText.placeholder = @"请输入推送id,默认本机推送id";
  86. self.pushIdText.text = [JPUSHService registrationID];
  87. self.pushIdText.clearButtonMode=UITextFieldViewModeWhileEditing;
  88. self.pushIdText.inputAccessoryView = _toobar;
  89. self.pushIdText.delegate=self;
  90. self.pushIdText.backgroundColor = [UIColor whiteColor];
  91. [self.scrollView addSubview:self.pushIdText];
  92. UIButton *copyButton = [UIButton buttonWithType:UIButtonTypeCustom];
  93. copyButton.frame = CGRectMake(CGRectGetMaxX(self.pushIdText.frame) + 10, CGRectGetMaxY(self.contentText.frame) + 20, 60, 30);
  94. copyButton.backgroundColor = [self colorR:100 g:149 b:237];
  95. [copyButton addTarget:self action:@selector(copyButtonClick) forControlEvents:UIControlEventTouchUpInside];
  96. [copyButton setTitle:@"复制" forState:UIControlStateNormal];
  97. [self.scrollView addSubview:copyButton];
  98. UILabel *label22 = [[UILabel alloc]init];
  99. label22.frame = CGRectMake(10, CGRectGetMaxY(self.pushIdText.frame)+20, bounds.size.width - 20, 60);
  100. label22.text = @"Tip: 可以尝试获取其他手机的推送id进行推送测试(将推送id复制给其他手机)";
  101. label22.numberOfLines = 2;
  102. label22.textColor = [UIColor redColor];
  103. [self.scrollView addSubview:label22];
  104. [label22 setNeedsLayout];
  105. UILabel *label3 = [[UILabel alloc]init];
  106. label3.frame = CGRectMake(10, CGRectGetMaxY(label22.frame)+20, 200, 30);
  107. label3.text = @"推送/透传结果:";
  108. [self.scrollView addSubview:label3];
  109. self.textview = [[UITextView alloc] init];
  110. self.textview.frame = CGRectMake(10, CGRectGetMaxY(label3.frame)+20, bounds.size.width-20, 100);
  111. self.textview.textColor = [UIColor blueColor];
  112. self.textview.delegate = self;
  113. self.textview.inputAccessoryView = _toobar;
  114. [_scrollView addSubview:self.textview];
  115. //整体再将ScrollerView显示在窗口中
  116. [self.view addSubview:_scrollView];
  117. [_scrollView setContentSize:CGSizeMake(bounds.size.width, bounds.size.height*1.2)];
  118. self.pushButton = [UIButton buttonWithType:UIButtonTypeCustom];
  119. [self.pushButton addTarget:self action:@selector(pushButtonClick) forControlEvents:UIControlEventTouchUpInside];
  120. self.pushButton.backgroundColor = [self colorR:100 g:149 b:237];
  121. [self.pushButton setTitle:@"推送测试" forState:UIControlStateNormal];
  122. self.passthroughButton = [UIButton buttonWithType:UIButtonTypeCustom];
  123. [self.passthroughButton addTarget:self action:@selector(passthroughButtonClick) forControlEvents:UIControlEventTouchUpInside];
  124. self.passthroughButton.backgroundColor = [self colorR:100 g:149 b:237];
  125. [self.passthroughButton setTitle:@"透传测试" forState:UIControlStateNormal];
  126. UIStackView *subStackView = [[UIStackView alloc] init];
  127. //子视图之间的间距
  128. subStackView.spacing = 10.0;
  129. //子视图的对齐方式。
  130. subStackView.alignment = UIStackViewAlignmentFill;
  131. //stackView排列方向 横向排列
  132. subStackView.axis = UILayoutConstraintAxisHorizontal;
  133. //子视图的宽度或高度布局。
  134. subStackView.distribution = UIStackViewDistributionFillEqually;
  135. [subStackView addArrangedSubview:self.pushButton];
  136. [subStackView addArrangedSubview:self.passthroughButton];
  137. subStackView.frame = CGRectMake(30, CGRectGetMaxY(self.textview.frame) + 40, self.view.bounds.size.width -60, 40);
  138. [_scrollView addSubview:subStackView];
  139. }
  140. - (void)copyButtonClick {
  141. if (self.pushIdText.text.length == 0) {
  142. [self setToast:@"推送Id不存在"];
  143. return;
  144. }
  145. UIPasteboard *appPasteBoard = [UIPasteboard generalPasteboard];
  146. [appPasteBoard setString:self.pushIdText.text];
  147. [self setToast:@"复制成功"];
  148. }
  149. - (void)resignKeyboard{
  150. [self.view endEditing:YES];
  151. }
  152. //推送按钮
  153. -(void)pushButtonClick{
  154. if (self.titleText.text.length == 0) {
  155. [self setToast:@"请输入标题"];
  156. return;
  157. }
  158. if (self.contentText.text.length == 0) {
  159. [self setToast:@"请输入内容"];
  160. return;
  161. }
  162. if (self.pushIdText.text.length == 0) {
  163. [self setToast:@"推送Id不存在"];
  164. return;
  165. }
  166. NSMutableDictionary *dic = @{
  167. @"appKey": [SettingManager getAppSpKey],
  168. @"secretKey": [SettingManager getAppSpSecretKey],
  169. @"title":self.titleText.text,
  170. @"content":self.contentText.text,
  171. @"pushType":@"0",
  172. @"deviceIds":@[self.pushIdText.text],
  173. @"extras":@{
  174. @"AJPush":@"1234",
  175. @"key":@"test"
  176. },
  177. @"iosConfig":@{
  178. @"sound":@"aa.caf"
  179. },
  180. @"androidConfig":@{
  181. @"sound":@"aa.caf"
  182. }
  183. }.mutableCopy;
  184. [[RequestManager sharedInstance]POSTRequestWithUrl:[NSString stringWithFormat:@"%@/sp/push/pushBatch", [SettingManager getAppSpUrl]] paramaters:dic successBlock:^(id _Nonnull object, NSURLResponse * _Nonnull response) {
  185. if([object[@"repCode"] isEqual: @"0000"]){
  186. self.textview.text = @"推送成功";
  187. }else {
  188. self.textview.text = [NSString stringWithFormat:@"%@\n%@",@"推送失败",[self stringConvert:object[@"repMsg"]]];
  189. }
  190. } FailBlock:^(NSError * _Nonnull error) {
  191. dispatch_queue_t queue = dispatch_get_main_queue();
  192. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), queue, ^{
  193. self.textview.text = [NSString stringWithFormat:@"%@\n%@",@"推送失败", error.description];
  194. });
  195. }];
  196. }
  197. //透传按钮
  198. -(void)passthroughButtonClick{
  199. if (self.titleText.text.length == 0) {
  200. [self setToast:@"请输入标题"];
  201. return;
  202. }
  203. if (self.contentText.text.length == 0) {
  204. [self setToast:@"请输入内容"];
  205. return;
  206. }
  207. if (self.pushIdText.text.length == 0) {
  208. [self setToast:@"推送Id不存在"];
  209. return;
  210. }
  211. NSMutableDictionary *dic = @{
  212. @"appKey": [SettingManager getAppSpKey],
  213. @"secretKey": [SettingManager getAppSpSecretKey],
  214. @"title":self.titleText.text,
  215. @"content":self.contentText.text,
  216. @"pushType":@"1",
  217. @"deviceIds":@[self.pushIdText.text],
  218. @"extras":@{
  219. @"AJPush":@"1234",
  220. @"key":@"test"
  221. },
  222. @"iosConfig":@{
  223. @"sound":@"aa.caf"
  224. },
  225. @"androidConfig":@{
  226. @"sound":@"aa"
  227. }
  228. }.mutableCopy;
  229. [[RequestManager sharedInstance]POSTRequestWithUrl:[NSString stringWithFormat:@"%@/sp/push/pushBatch", [SettingManager getAppSpUrl]] paramaters:dic successBlock:^(id _Nonnull object, NSURLResponse * _Nonnull response) {
  230. if([object[@"repCode"] isEqual: @"0000"]){
  231. self.textview.text = @"透传成功";
  232. }else {
  233. self.textview.text = [NSString stringWithFormat:@"%@\n%@",@"透传失败",[self stringConvert:object[@"repMsg"]]];
  234. }
  235. } FailBlock:^(NSError * _Nonnull error) {
  236. dispatch_queue_t queue = dispatch_get_main_queue();
  237. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), queue, ^{
  238. self.textview.text = [NSString stringWithFormat:@"%@\n%@",@"透传失败", error.description];
  239. });
  240. }];
  241. }
  242. //创建配置中心入口
  243. - (void)buildSetting {
  244. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"setting"] style:UIBarButtonItemStylePlain target:self action:@selector(settingClick)];
  245. }
  246. - (void)settingClick {
  247. SettingController *setVc = [[SettingController alloc] init];
  248. [self.navigationController pushViewController:setVc animated:YES];
  249. }
  250. //注册推送通知中心
  251. - (void)initNSNotificationCenter {
  252. NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
  253. [defaultCenter addObserver:self
  254. selector:@selector(noticeBarMessage:)
  255. name:kAJPushServiceNoticeBarMessageNotification
  256. object:nil];
  257. [defaultCenter addObserver:self
  258. selector:@selector(innerMessage:)
  259. name:kAJPushServiceInnerMessageNotification
  260. object:nil];
  261. [defaultCenter addObserver:self
  262. selector:@selector(customMessage:)
  263. name:kAJPushServiceCustomMessageNotification
  264. object:nil];
  265. }
  266. //注册键盘监听
  267. - (void)registerForKeyboardNotifications {
  268. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
  269. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBeHidden:) name:UIKeyboardWillHideNotification object:nil];
  270. }
  271. //键盘弹出代理
  272. - (void)keyboardWillShow:(NSNotification *)aNotification {
  273. NSDictionary* info = [aNotification userInfo];
  274. // 注意不要用UIKeyboardFrameBeginUserInfoKey,第三方键盘可能会存在高度不准,相差40高度的问题
  275. CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
  276. // 修改滚动天和tableView的contentInset
  277. self.scrollView.contentInset = UIEdgeInsetsMake(0, 0, kbSize.height, 0);
  278. self.scrollView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, kbSize.height, 0);
  279. //获取当前文本框架大小
  280. UIView *v = [UIView new];
  281. if (self.titleText.isFirstResponder) {
  282. v = self.titleText;
  283. }
  284. if (self.contentText.isFirstResponder) {
  285. v = self.contentText;
  286. }
  287. if (self.pushIdText.isFirstResponder) {
  288. v = self.pushIdText;
  289. }
  290. if (self.textview.isFirstResponder) {
  291. v = self.textview;
  292. }
  293. CGRect textFieldRect = [v frame];
  294. //滚动到当前文本框
  295. [self.scrollView scrollRectToVisible:textFieldRect animated:YES];
  296. }
  297. //键盘隐藏代理
  298. - (void)keyboardWillBeHidden:(NSNotification *)aNotification {
  299. self.scrollView.contentInset = UIEdgeInsetsZero;
  300. self.scrollView.scrollIndicatorInsets = UIEdgeInsetsZero;
  301. }
  302. // textfield代理
  303. - (BOOL)textFieldShouldReturn:(UITextField *)textField{
  304. [textField resignFirstResponder];
  305. return YES;
  306. }
  307. // textView代理
  308. - (BOOL) textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
  309. if ([text isEqualToString:@"\n"]){
  310. [textView resignFirstResponder];
  311. }
  312. return YES;
  313. }
  314. //消息栏通知响应通知
  315. - (void)noticeBarMessage:(NSNotification *)notification {
  316. NSDictionary *userInfo = [notification userInfo];
  317. NSLog(@"++++++++++noticeBarMessage++++++++++++++");
  318. NSLog(@"%@",userInfo);
  319. [self showMessageInfo:userInfo delay:0.5];
  320. NSLog(@"++++++++++noticeBarMessage++++++++++++++");
  321. }
  322. //应用内部响应通知
  323. - (void)innerMessage:(NSNotification *)notification {
  324. NSDictionary *userInfo = [notification userInfo];
  325. NSLog(@"+++++++++++innerMessage+++++++++++++");
  326. NSLog(@"%@",userInfo);
  327. NSString *title = userInfo[@"aps"][@"alert"][@"title"];
  328. NSString *body = userInfo[@"aps"][@"alert"][@"body"];
  329. UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:body preferredStyle:UIAlertControllerStyleAlert];
  330. UIAlertAction *conform = [UIAlertAction actionWithTitle:@"查看" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  331. //处理跳转
  332. [self showMessageInfo:userInfo delay:0.1];
  333. }];
  334. UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消查看" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  335. }];
  336. [alert addAction:conform];
  337. [alert addAction:cancel];
  338. [self presentViewController:alert animated:YES completion:nil];
  339. NSLog(@"+++++++++++innerMessage+++++++++++++");
  340. }
  341. //自定义消息响应通知
  342. - (void)customMessage:(NSNotification *)notification {
  343. NSDictionary *userInfo = [notification userInfo];
  344. NSLog(@"+++++++++++customMessage+++++++++++++");
  345. NSLog(@"%@",userInfo);
  346. NSString *body = userInfo[@"content"];
  347. UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"自定义消息" message:body preferredStyle:UIAlertControllerStyleAlert];
  348. UIAlertAction *conform = [UIAlertAction actionWithTitle:@"查看" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  349. //处理跳转
  350. [self showMessageInfo:userInfo delay:0.1];
  351. }];
  352. UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消查看" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  353. }];
  354. [alert addAction:conform];
  355. [alert addAction:cancel];
  356. [self presentViewController:alert animated:YES completion:nil];
  357. NSLog(@"+++++++++++customMessage+++++++++++++");
  358. }
  359. //页面显示通知消息详情
  360. - (void)showMessageInfo:(NSDictionary *)info delay:(NSTimeInterval)time {
  361. NSData *jsonData = [NSJSONSerialization dataWithJSONObject:info options:NSJSONWritingPrettyPrinted error:NULL];
  362. NSString *str = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
  363. dispatch_queue_t queue = dispatch_get_main_queue();
  364. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(time * NSEC_PER_SEC)), queue, ^{
  365. self.textview.text = [NSString stringWithFormat:@"%@", str];
  366. [self.textview scrollRectToVisible:CGRectMake(0, self.textview.contentSize.height-15, self.textview.contentSize.width, 10) animated:YES];
  367. });
  368. }
  369. //屏幕手势事件
  370. - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
  371. [self.view endEditing:YES];
  372. }
  373. // 转UTF8
  374. - (NSString *)convert:(id)dic {
  375. NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:nil];
  376. if (![jsonData isKindOfClass:[NSData class]]) {
  377. return nil;
  378. }
  379. NSString *tmp=[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
  380. return tmp;
  381. }
  382. // string转UTF8
  383. - (NSString *)stringConvert:(NSString*)dic {
  384. NSData* jsonData = [dic dataUsingEncoding:NSUTF8StringEncoding];
  385. if (![jsonData isKindOfClass:[NSData class]]) {
  386. return nil;
  387. }
  388. NSString *tmp=[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
  389. return tmp;
  390. }
  391. //异常toast
  392. - (void)setToast:(NSString*)toast{
  393. [[Toast makeText:toast] showWithType:AJShortTime];
  394. }
  395. - (UIColor *)colorR:(CGFloat)r g:(CGFloat)g b:(CGFloat)b {
  396. return [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1];
  397. }
  398. @end