Utils.m 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. //
  2. // Utils.m
  3. // GtSdkDemo
  4. //
  5. // Created by ak on 2020/03/20.
  6. // Copyright © 2019 Gexin Interactive (Beijing) Network Technology Co.,LTD. All rights reserved.
  7. //
  8. #import "Utils.h"
  9. #import <UserNotifications/UserNotifications.h>
  10. @implementation Utils
  11. + (BOOL)PushModel {
  12. return [[NSUserDefaults standardUserDefaults] boolForKey:@"OffPushMode"];
  13. }
  14. + (void)SetPushModel:(BOOL)mode {
  15. [[NSUserDefaults standardUserDefaults] setBool:mode forKey:@"OffPushMode"];
  16. [[NSUserDefaults standardUserDefaults] synchronize];
  17. }
  18. + (NSString *)getHexStringForData:(NSData *)data {
  19. NSUInteger len = [data length];
  20. char *chars = (char *) [data bytes];
  21. NSMutableString *hexString = [[NSMutableString alloc] init];
  22. for (NSUInteger i = 0; i < len; i++) {
  23. [hexString appendString:[NSString stringWithFormat:@"%0.2hhx", chars[i]]];
  24. }
  25. return hexString;
  26. }
  27. /// 时间转字符串
  28. + (NSString *)formateTime:(NSDate *)date {
  29. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  30. [formatter setDateFormat:@"HH:mm:ss"];
  31. NSString *dateTime = [formatter stringFromDate:date];
  32. return dateTime;
  33. }
  34. + (void)pushLocalNotification:(NSString *)title userInfo:(NSDictionary *)userInfo {
  35. NSData *jsonData = [title dataUsingEncoding:NSUTF8StringEncoding];
  36. NSError *error;
  37. NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:jsonData
  38. options:NSJSONReadingMutableContainers
  39. error:&error];
  40. if (error){
  41. //NSLog(@"json格式string解析失败:%@",error);
  42. return ;
  43. }
  44. UNMutableNotificationContent *content = [UNMutableNotificationContent new];
  45. content.title = dict[@"title"];
  46. content.body = dict[@"content"];
  47. content.userInfo = userInfo;
  48. UNNotificationRequest *req = [UNNotificationRequest requestWithIdentifier:@"id1" content:content trigger:nil];
  49. [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:req withCompletionHandler:^(NSError * _Nullable error) {
  50. NSLog(@"addNotificationRequest added");
  51. }];
  52. }
  53. @end