AppDelegate+notification.m 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 "PushModel.h"
  10. // iOS10注册APNs所需头文件
  11. #ifdef NSFoundationVersionNumber_iOS_9_x_Max
  12. #import <UserNotifications/UserNotifications.h>
  13. #endif
  14. @import Firebase;
  15. @interface AppDelegate()<UNUserNotificationCenterDelegate, FIRMessagingDelegate>
  16. @end
  17. @implementation AppDelegate (notification)
  18. + (void)initNotification:(UIApplication *)application {
  19. [UNUserNotificationCenter currentNotificationCenter].delegate = self;
  20. UNAuthorizationOptions authOptions = UNAuthorizationOptionAlert |
  21. UNAuthorizationOptionSound | UNAuthorizationOptionBadge;
  22. [[UNUserNotificationCenter currentNotificationCenter]
  23. requestAuthorizationWithOptions:authOptions
  24. completionHandler:^(BOOL granted, NSError * _Nullable error) {
  25. // ...
  26. }];
  27. [application registerForRemoteNotifications];
  28. [FIRApp configure];
  29. [FIRMessaging messaging].delegate = self;
  30. }
  31. - (void)applicationDidEnterBackground:(UIApplication *)application {
  32. [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
  33. }
  34. - (void)applicationWillEnterForeground:(UIApplication *)application {
  35. [application setApplicationIconBadgeNumber:0];
  36. [[UNUserNotificationCenter currentNotificationCenter] removeAllPendingNotificationRequests];
  37. }
  38. - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
  39. // 注册APNS成功, 注册deviceToken
  40. NSString *token = [NSString stringWithFormat:@"%@", deviceToken];
  41. //[MiPushSDK bindDeviceToken:deviceToken];
  42. [FIRMessaging messaging].APNSToken = deviceToken;
  43. }
  44. - (void)messaging:(FIRMessaging *)messaging didReceiveRegistrationToken:(NSString *)fcmToken {
  45. NSLog(@"FCM registration token: %@", fcmToken);
  46. // TODO: If necessary send token to application server.
  47. // Note: This callback is fired at each app startup and whenever a new token is generated.
  48. NSLog(@"didReceiveRegistrationToken fcmToken: %@", fcmToken);
  49. if (fcmToken == nil || [fcmToken isEqualToString:@""])
  50. return;
  51. [[NSUserDefaults standardUserDefaults] setObject:fcmToken forKey:@"FCM_TOKEN"];
  52. [[NSUserDefaults standardUserDefaults] synchronize];
  53. [[NSNotificationCenter defaultCenter] postNotificationName:@"onRefreshFCMToken" object:nil];
  54. }
  55. - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
  56. NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
  57. // 注册APNS失败
  58. }
  59. - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
  60. [self recivePush:NO userInfo:userInfo];
  61. completionHandler(UIBackgroundFetchResultNewData);
  62. }
  63. - (void)recivePush:(BOOL)isFinishLaunching userInfo:(NSDictionary*)userInfo {
  64. [PushModel recivePush:userInfo isFinishLaunching:isFinishLaunching];
  65. }
  66. - (void)userNotificationCenter:(UNUserNotificationCenter *)center
  67. willPresentNotification:(UNNotification *)notification
  68. withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
  69. NSDictionary *userInfo = notification.request.content.userInfo;
  70. [self recivePush:NO userInfo:userInfo];
  71. completionHandler(UNNotificationPresentationOptionNone);
  72. }
  73. - (void)userNotificationCenter:(UNUserNotificationCenter *)center
  74. didReceiveNotificationResponse:(UNNotificationResponse *)response
  75. withCompletionHandler:(void(^)(void))completionHandler {
  76. NSDictionary *userInfo = response.notification.request.content.userInfo;
  77. [self recivePush:NO userInfo:userInfo];
  78. completionHandler();
  79. }
  80. @end