123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- //
- // AppDelegate+notification.m
- // Overseas Watch
- //
- // Created by 刘振兴 on 2023/11/28.
- // Copyright © 2023 BaH Cy. All rights reserved.
- //
- #import "AppDelegate+notification.h"
- #import "SKPushMessage.h"
- // iOS10注册APNs所需头文件
- #ifdef NSFoundationVersionNumber_iOS_9_x_Max
- #import <UserNotifications/UserNotifications.h>
- #endif
- #import <FirebaseCore/FirebaseCore.h>
- #import <FIRMessaging.h>
- @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];
-
- #ifdef SK_DEBUG_GOOGLE_FACEBOOK
- [FIRApp configure];
- [FIRMessaging messaging].delegate = self;
- #endif
- }
- - (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 {
-
- //方法 1
- NSString *deviceTokenString = [[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
- deviceTokenString = [deviceTokenString stringByReplacingOccurrencesOfString:@" " withString:@""];
-
-
- //方法 2
- if (![deviceToken isKindOfClass:[NSData class]]) return;
- const unsigned *tokenBytes = [deviceToken bytes];
- NSString *hexToken = [NSString stringWithFormat:@"%08x%08x%08x%08x%08x%08x%08x%08x",
- ntohl(tokenBytes[0]), ntohl(tokenBytes[1]), ntohl(tokenBytes[2]),
- ntohl(tokenBytes[3]), ntohl(tokenBytes[4]), ntohl(tokenBytes[5]),
- ntohl(tokenBytes[6]), ntohl(tokenBytes[7])];
- HDNormalLog(([NSString stringWithFormat:@"APNS Apple: %@", hexToken]));
- [SKUserDataHelper setPushToken:hexToken];
- [[NSNotificationCenter defaultCenter] postNotificationName:@"onRefreshPushToken" object:nil];
-
- #ifdef SK_DEBUG_GOOGLE_FACEBOOK
- //方法:fcm
- [FIRMessaging messaging].APNSToken = deviceToken;
- #endif
- }
- - (void)messaging:(FIRMessaging *)messaging didReceiveRegistrationToken:(NSString *)fcmToken {
- if (fcmToken == nil || [fcmToken isEqualToString:@""])
- return;
- HDNormalLog(([NSString stringWithFormat:@"PUSH TOKEN: %@", fcmToken]));
- [[NSNotificationCenter defaultCenter] postNotificationName:@"onRefreshPushToken" object:nil];
- }
- - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
- HDNormalLog(([NSString stringWithFormat:@"APNS Apple: didFailToRegisterForRemoteNotificationsWithError: %@", error]));
- }
- - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
- [self recivePush:NO userInfo:userInfo];
- completionHandler(UIBackgroundFetchResultNewData);
- }
- - (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();
- }
- - (void)recivePush:(BOOL)isFinishLaunching userInfo:(NSDictionary*)userInfo {
- NSString* message = userInfo[@"message"];
- if (!message || message.length <= 0)
- return;
- [SKPushMessage didReceiveMessage:message];
- }
- @end
|