123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- //
- // AppDelegate+notification.m
- // Overseas Watch
- //
- // Created by 刘振兴 on 2023/11/28.
- // Copyright © 2023 BaH Cy. All rights reserved.
- //
- #import "AppDelegate+notification.h"
- #import "PushModel.h"
- // iOS10注册APNs所需头文件
- #ifdef NSFoundationVersionNumber_iOS_9_x_Max
- #import <UserNotifications/UserNotifications.h>
- #endif
- @import Firebase;
- @interface AppDelegate()<UNUserNotificationCenterDelegate, FIRMessagingDelegate>
- @end
- @implementation AppDelegate (notification)
- + (void)initNotification:(UIApplication *)application {
- [UNUserNotificationCenter currentNotificationCenter].delegate = self;
- UNAuthorizationOptions authOptions = UNAuthorizationOptionAlert |
- UNAuthorizationOptionSound | UNAuthorizationOptionBadge;
- [[UNUserNotificationCenter currentNotificationCenter]
- requestAuthorizationWithOptions:authOptions
- completionHandler:^(BOOL granted, NSError * _Nullable error) {
- // ...
- }];
- [application registerForRemoteNotifications];
-
- [FIRApp configure];
- [FIRMessaging messaging].delegate = self;
- }
- - (void)applicationDidEnterBackground:(UIApplication *)application {
- [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
- }
- - (void)applicationWillEnterForeground:(UIApplication *)application {
- [application setApplicationIconBadgeNumber:0];
- [[UNUserNotificationCenter currentNotificationCenter] removeAllPendingNotificationRequests];
- }
- - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
- // 注册APNS成功, 注册deviceToken
- NSString *token = [NSString stringWithFormat:@"%@", deviceToken];
- //[MiPushSDK bindDeviceToken:deviceToken];
-
- [FIRMessaging messaging].APNSToken = deviceToken;
- }
- - (void)messaging:(FIRMessaging *)messaging didReceiveRegistrationToken:(NSString *)fcmToken {
- NSLog(@"FCM registration token: %@", fcmToken);
-
- // TODO: If necessary send token to application server.
- // Note: This callback is fired at each app startup and whenever a new token is generated.
- NSLog(@"didReceiveRegistrationToken fcmToken: %@", fcmToken);
- if (fcmToken == nil || [fcmToken isEqualToString:@""])
- return;
-
- [[NSUserDefaults standardUserDefaults] setObject:fcmToken forKey:@"FCM_TOKEN"];
- [[NSUserDefaults standardUserDefaults] synchronize];
- [[NSNotificationCenter defaultCenter] postNotificationName:@"onRefreshFCMToken" object:nil];
- }
- - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
- NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
- // 注册APNS失败
- }
- - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
-
- [self recivePush:NO userInfo:userInfo];
-
- completionHandler(UIBackgroundFetchResultNewData);
- }
- - (void)recivePush:(BOOL)isFinishLaunching userInfo:(NSDictionary*)userInfo {
- [PushModel recivePush:userInfo isFinishLaunching:isFinishLaunching];
- }
- - (void)userNotificationCenter:(UNUserNotificationCenter *)center
- willPresentNotification:(UNNotification *)notification
- withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
- NSDictionary *userInfo = notification.request.content.userInfo;
- [self recivePush:NO userInfo:userInfo];
- completionHandler(UNNotificationPresentationOptionNone);
- }
- - (void)userNotificationCenter:(UNUserNotificationCenter *)center
- didReceiveNotificationResponse:(UNNotificationResponse *)response
- withCompletionHandler:(void(^)(void))completionHandler {
- NSDictionary *userInfo = response.notification.request.content.userInfo;
- [self recivePush:NO userInfo:userInfo];
- completionHandler();
- }
- @end
|