c1_unity/Assets/Plugins/iOS/CustomAppController.mm
2023-06-08 17:23:03 +08:00

63 lines
2.6 KiB
Plaintext

#import "UnityAppController.h"
#import "FirebaseSDK.h"
#import "FirebaseMessaging/FIRMessaging.h"
#import "UserNotifications/UNUserNotificationCenter.h"
@interface CustomAppController : UnityAppController <UNUserNotificationCenterDelegate, FIRMessagingDelegate>
@end
IMPL_APP_CONTROLLER_SUBCLASS (CustomAppController)
@implementation CustomAppController
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[super application:application didFinishLaunchingWithOptions:launchOptions];
// [[FirebaseSDK getInstance] initialize];
// [START set_messaging_delegate]
// [FIRMessaging messaging].delegate = self;
// [END set_messaging_delegate]
if ([UNUserNotificationCenter class] != nil) {
// iOS 10 or later
// For iOS 10 display notification (sent via APNS)
[UNUserNotificationCenter currentNotificationCenter].delegate = self;
UNAuthorizationOptions authOptions = UNAuthorizationOptionAlert |
UNAuthorizationOptionSound | UNAuthorizationOptionBadge;
[[UNUserNotificationCenter currentNotificationCenter]
requestAuthorizationWithOptions:authOptions
completionHandler:^(BOOL granted, NSError * _Nullable error) {
// ...
}];
} else {
// iOS 10 notifications aren't available; fall back to iOS 8-9 notifications.
UIUserNotificationType allNotificationTypes =
(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);
UIUserNotificationSettings *settings =
[UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil];
[application registerUserNotificationSettings:settings];
}
[application registerForRemoteNotifications];
// [[FIRMessaging messaging] tokenWithCompletion:^(NSString *token, NSError *error) {
// if (error != nil) {
// NSLog(@"Error getting FCM registration token: %@", error);
// } else {
// NSLog(@"FCM tokenWithCompletion registration token: %@", token);
// }
// }];
return YES;
}
// [START refresh_token]
- (void)messaging:(FIRMessaging *)messaging didReceiveRegistrationToken:(NSString *)fcmToken {
NSLog(@"FCM registration token: %@", fcmToken);
// Notify about received token.
NSDictionary *dataDict = [NSDictionary dictionaryWithObject:fcmToken forKey:@"token"];
[[NSNotificationCenter defaultCenter] postNotificationName:
@"FCMToken" object:nil userInfo:dataDict];
// TODO: If necessary send token to application server.
// Note: This callback is fired at each app startup and whenever a new token is generated.
}
// [END refresh_token]
@end