AppDelegate+notification.m 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //
  2. // AppDelegate+notification.m
  3. // Overseas Watch
  4. //
  5. // Created by 刘振兴 on 2023/11/28.
  6. // Copyright © 2023 BaH Cy. All rights reserved.
  7. //
  8. #import "AppDelegate+notification.h"
  9. #import "SKPushMessage.h"
  10. // iOS10注册APNs所需头文件
  11. #ifdef NSFoundationVersionNumber_iOS_9_x_Max
  12. #import <UserNotifications/UserNotifications.h>
  13. #endif
  14. #import <FirebaseCore/FirebaseCore.h>
  15. #import <FIRMessaging.h>
  16. @interface AppDelegate()<UNUserNotificationCenterDelegate, FIRMessagingDelegate>
  17. @end
  18. @implementation AppDelegate (notification)
  19. - (void)initNotification:(UIApplication *)application {
  20. [UNUserNotificationCenter currentNotificationCenter].delegate = self;
  21. UNAuthorizationOptions authOptions = UNAuthorizationOptionAlert |
  22. UNAuthorizationOptionSound | UNAuthorizationOptionBadge;
  23. [[UNUserNotificationCenter currentNotificationCenter]
  24. requestAuthorizationWithOptions:authOptions
  25. completionHandler:^(BOOL granted, NSError * _Nullable error) {
  26. // ...
  27. }];
  28. [application registerForRemoteNotifications];
  29. #ifdef SK_DEBUG_GOOGLE_FACEBOOK
  30. [FIRApp configure];
  31. [FIRMessaging messaging].delegate = self;
  32. #endif
  33. }
  34. - (void)applicationDidEnterBackground:(UIApplication *)application {
  35. [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
  36. }
  37. - (void)applicationWillEnterForeground:(UIApplication *)application {
  38. [application setApplicationIconBadgeNumber:0];
  39. [[UNUserNotificationCenter currentNotificationCenter] removeAllPendingNotificationRequests];
  40. }
  41. - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
  42. //方法 1
  43. NSString *deviceTokenString = [[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
  44. deviceTokenString = [deviceTokenString stringByReplacingOccurrencesOfString:@" " withString:@""];
  45. //方法 2
  46. if (![deviceToken isKindOfClass:[NSData class]]) return;
  47. const unsigned *tokenBytes = [deviceToken bytes];
  48. NSString *hexToken = [NSString stringWithFormat:@"%08x%08x%08x%08x%08x%08x%08x%08x",
  49. ntohl(tokenBytes[0]), ntohl(tokenBytes[1]), ntohl(tokenBytes[2]),
  50. ntohl(tokenBytes[3]), ntohl(tokenBytes[4]), ntohl(tokenBytes[5]),
  51. ntohl(tokenBytes[6]), ntohl(tokenBytes[7])];
  52. HDNormalLog(([NSString stringWithFormat:@"APNS Apple: %@", hexToken]));
  53. [SKUserDataHelper setPushToken:hexToken];
  54. [[NSNotificationCenter defaultCenter] postNotificationName:@"onRefreshPushToken" object:nil];
  55. #ifdef SK_DEBUG_GOOGLE_FACEBOOK
  56. //方法:fcm
  57. [FIRMessaging messaging].APNSToken = deviceToken;
  58. #endif
  59. }
  60. - (void)messaging:(FIRMessaging *)messaging didReceiveRegistrationToken:(NSString *)fcmToken {
  61. if (fcmToken == nil || [fcmToken isEqualToString:@""])
  62. return;
  63. HDNormalLog(([NSString stringWithFormat:@"PUSH TOKEN: %@", fcmToken]));
  64. [[NSNotificationCenter defaultCenter] postNotificationName:@"onRefreshPushToken" object:nil];
  65. }
  66. - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
  67. HDNormalLog(([NSString stringWithFormat:@"APNS Apple: didFailToRegisterForRemoteNotificationsWithError: %@", error]));
  68. }
  69. - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
  70. [self recivePush:NO userInfo:userInfo];
  71. completionHandler(UIBackgroundFetchResultNewData);
  72. }
  73. - (void)userNotificationCenter:(UNUserNotificationCenter *)center
  74. willPresentNotification:(UNNotification *)notification
  75. withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
  76. NSDictionary *userInfo = notification.request.content.userInfo;
  77. [self recivePush:NO userInfo:userInfo];
  78. completionHandler(UNNotificationPresentationOptionNone);
  79. }
  80. - (void)userNotificationCenter:(UNUserNotificationCenter *)center
  81. didReceiveNotificationResponse:(UNNotificationResponse *)response
  82. withCompletionHandler:(void(^)(void))completionHandler {
  83. NSDictionary *userInfo = response.notification.request.content.userInfo;
  84. [self recivePush:NO userInfo:userInfo];
  85. completionHandler();
  86. }
  87. - (void)recivePush:(BOOL)isFinishLaunching userInfo:(NSDictionary*)userInfo {
  88. NSString* message = userInfo[@"message"];
  89. if (!message || message.length <= 0)
  90. return;
  91. [SKPushMessage didReceiveMessage:message];
  92. }
  93. @end