Commit d503b3f4 authored by Libin Lu's avatar Libin Lu

remove local bridge

parent ba4368b4
...@@ -39,58 +39,58 @@ RCT_ENUM_CONVERTER(NSCalendarUnit, ...@@ -39,58 +39,58 @@ RCT_ENUM_CONVERTER(NSCalendarUnit,
+ (UNNotificationRequest *)UNNotificationRequest:(id)json + (UNNotificationRequest *)UNNotificationRequest:(id)json
{ {
NSDictionary<NSString *, id> *details = [self NSDictionary:json]; NSDictionary<NSString *, id> *details = [self NSDictionary:json];
UNMutableNotificationContent *content = [UNMutableNotificationContent new]; UNMutableNotificationContent *content = [UNMutableNotificationContent new];
content.title =[RCTConvert NSString:details[@"title"]]; content.title =[RCTConvert NSString:details[@"title"]];
content.body =[RCTConvert NSString:details[@"body"]]; content.body =[RCTConvert NSString:details[@"body"]];
NSString* sound = [RCTConvert NSString:details[@"sound"]]; NSString* sound = [RCTConvert NSString:details[@"sound"]];
if(sound != nil){ if(sound != nil){
content.sound = [UNNotificationSound soundNamed:sound]; content.sound = [UNNotificationSound soundNamed:sound];
}else{ }else{
content.sound = [UNNotificationSound defaultSound]; content.sound = [UNNotificationSound defaultSound];
}
content.categoryIdentifier = [RCTConvert NSString:details[@"click_action"]];
content.userInfo = details;
content.badge = [RCTConvert NSNumber:details[@"badge"]];
NSDate *fireDate = [RCTConvert NSDate:details[@"fire_date"]];
if(fireDate == nil){
return [UNNotificationRequest requestWithIdentifier:[RCTConvert NSString:details[@"id"]] content:content trigger:nil];
}
NSCalendarUnit interval = [RCTConvert NSCalendarUnit:details[@"repeat_interval"]];
NSCalendarUnit unitFlags;
switch (interval) {
case NSCalendarUnitMinute: {
unitFlags = NSCalendarUnitSecond;
break;
} }
case NSCalendarUnitHour: { content.categoryIdentifier = [RCTConvert NSString:details[@"click_action"]];
unitFlags = NSCalendarUnitMinute | NSCalendarUnitSecond; content.userInfo = details;
break; content.badge = [RCTConvert NSNumber:details[@"badge"]];
}
case NSCalendarUnitDay: { NSDate *fireDate = [RCTConvert NSDate:details[@"fire_date"]];
unitFlags = NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
break; if(fireDate == nil){
} return [UNNotificationRequest requestWithIdentifier:[RCTConvert NSString:details[@"id"]] content:content trigger:nil];
case NSCalendarUnitWeekOfYear: {
unitFlags = NSCalendarUnitWeekday | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
break;
}
case NSCalendarUnitMonth:{
unitFlags = NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
} }
case NSCalendarUnitYear:{
unitFlags = NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond; NSCalendarUnit interval = [RCTConvert NSCalendarUnit:details[@"repeat_interval"]];
NSCalendarUnit unitFlags;
switch (interval) {
case NSCalendarUnitMinute: {
unitFlags = NSCalendarUnitSecond;
break;
}
case NSCalendarUnitHour: {
unitFlags = NSCalendarUnitMinute | NSCalendarUnitSecond;
break;
}
case NSCalendarUnitDay: {
unitFlags = NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
break;
}
case NSCalendarUnitWeekOfYear: {
unitFlags = NSCalendarUnitWeekday | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
break;
}
case NSCalendarUnitMonth:{
unitFlags = NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
}
case NSCalendarUnitYear:{
unitFlags = NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
}
default:
unitFlags = NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
break;
} }
default: NSDateComponents *components = [[NSCalendar currentCalendar] components:unitFlags fromDate:fireDate];
unitFlags = NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond; UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:interval != 0];
break; return [UNNotificationRequest requestWithIdentifier:[RCTConvert NSString:details[@"id"]] content:content trigger:trigger];
}
NSDateComponents *components = [[NSCalendar currentCalendar] components:unitFlags fromDate:fireDate];
UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:interval != 0];
return [UNNotificationRequest requestWithIdentifier:[RCTConvert NSString:details[@"id"]] content:content trigger:trigger];
} }
@end @end
...@@ -99,20 +99,20 @@ RCT_ENUM_CONVERTER(NSCalendarUnit, ...@@ -99,20 +99,20 @@ RCT_ENUM_CONVERTER(NSCalendarUnit,
+ (UILocalNotification *)UILocalNotification:(id)json + (UILocalNotification *)UILocalNotification:(id)json
{ {
NSDictionary<NSString *, id> *details = [self NSDictionary:json]; NSDictionary<NSString *, id> *details = [self NSDictionary:json];
UILocalNotification *notification = [UILocalNotification new]; UILocalNotification *notification = [UILocalNotification new];
notification.fireDate = [RCTConvert NSDate:details[@"fire_date"]] ?: [NSDate date]; notification.fireDate = [RCTConvert NSDate:details[@"fire_date"]] ?: [NSDate date];
if([notification respondsToSelector:@selector(setAlertTitle:)]){ if([notification respondsToSelector:@selector(setAlertTitle:)]){
[notification setAlertTitle:[RCTConvert NSString:details[@"title"]]]; [notification setAlertTitle:[RCTConvert NSString:details[@"title"]]];
} }
notification.alertBody = [RCTConvert NSString:details[@"body"]]; notification.alertBody = [RCTConvert NSString:details[@"body"]];
notification.alertAction = [RCTConvert NSString:details[@"alert_action"]]; notification.alertAction = [RCTConvert NSString:details[@"alert_action"]];
notification.soundName = [RCTConvert NSString:details[@"sound"]] ?: UILocalNotificationDefaultSoundName; notification.soundName = [RCTConvert NSString:details[@"sound"]] ?: UILocalNotificationDefaultSoundName;
notification.userInfo = details; notification.userInfo = details;
notification.category = [RCTConvert NSString:details[@"click_action"]]; notification.category = [RCTConvert NSString:details[@"click_action"]];
notification.repeatInterval = [RCTConvert NSCalendarUnit:details[@"repeat_interval"]]; notification.repeatInterval = [RCTConvert NSCalendarUnit:details[@"repeat_interval"]];
notification.applicationIconBadgeNumber = [RCTConvert NSInteger:details[@"badge"]]; notification.applicationIconBadgeNumber = [RCTConvert NSInteger:details[@"badge"]];
return notification; return notification;
} }
RCT_ENUM_CONVERTER(UIBackgroundFetchResult, (@{ RCT_ENUM_CONVERTER(UIBackgroundFetchResult, (@{
...@@ -122,363 +122,362 @@ RCT_ENUM_CONVERTER(UIBackgroundFetchResult, (@{ ...@@ -122,363 +122,362 @@ RCT_ENUM_CONVERTER(UIBackgroundFetchResult, (@{
}), UIBackgroundFetchResultNoData, integerValue) }), UIBackgroundFetchResultNoData, integerValue)
RCT_ENUM_CONVERTER(UNNotificationPresentationOptions, (@{ RCT_ENUM_CONVERTER(UNNotificationPresentationOptions, (@{
@"UNNotificationPresentationOptionAll": @(UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound), @"UNNotificationPresentationOptionAll": @(UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound),
@"UNNotificationPresentationOptionNone": @(UNNotificationPresentationOptionNone)}), UIBackgroundFetchResultNoData, integerValue) @"UNNotificationPresentationOptionNone": @(UNNotificationPresentationOptionNone)}), UIBackgroundFetchResultNoData, integerValue)
@end @end
@interface RNFIRMessaging () @interface RNFIRMessaging ()
@property (nonatomic, strong) NSMutableDictionary *notificationCallbacks; @property (nonatomic, strong) NSMutableDictionary *notificationCallbacks;
@end @end
@implementation RNFIRMessaging @implementation RNFIRMessaging
@synthesize bridge = _bridge;
RCT_EXPORT_MODULE(); RCT_EXPORT_MODULE();
- (NSArray<NSString *> *)supportedEvents { - (NSArray<NSString *> *)supportedEvents {
return @[FCMNotificationReceived, FCMTokenRefreshed, FCMDirectChannelConnectionChanged]; return @[FCMNotificationReceived, FCMTokenRefreshed, FCMDirectChannelConnectionChanged];
} }
+ (void)didReceiveRemoteNotification:(nonnull NSDictionary *)userInfo fetchCompletionHandler:(nonnull RCTRemoteNotificationCallback)completionHandler { + (void)didReceiveRemoteNotification:(nonnull NSDictionary *)userInfo fetchCompletionHandler:(nonnull RCTRemoteNotificationCallback)completionHandler {
NSMutableDictionary* data = [[NSMutableDictionary alloc] initWithDictionary: userInfo]; NSMutableDictionary* data = [[NSMutableDictionary alloc] initWithDictionary: userInfo];
[data setValue:@"remote_notification" forKey:@"_notificationType"]; [data setValue:@"remote_notification" forKey:@"_notificationType"];
[data setValue:@(RCTSharedApplication().applicationState == UIApplicationStateInactive) forKey:@"opened_from_tray"]; [data setValue:@(RCTSharedApplication().applicationState == UIApplicationStateInactive) forKey:@"opened_from_tray"];
[[NSNotificationCenter defaultCenter] postNotificationName:FCMNotificationReceived object:self userInfo:@{@"data": data, @"completionHandler": completionHandler}]; [[NSNotificationCenter defaultCenter] postNotificationName:FCMNotificationReceived object:self userInfo:@{@"data": data, @"completionHandler": completionHandler}];
} }
+ (void)didReceiveLocalNotification:(UILocalNotification *)notification { + (void)didReceiveLocalNotification:(UILocalNotification *)notification {
NSMutableDictionary* data = [[NSMutableDictionary alloc] initWithDictionary: notification.userInfo]; NSMutableDictionary* data = [[NSMutableDictionary alloc] initWithDictionary: notification.userInfo];
[data setValue:@"local_notification" forKey:@"_notificationType"]; [data setValue:@"local_notification" forKey:@"_notificationType"];
[[NSNotificationCenter defaultCenter] postNotificationName:FCMNotificationReceived object:self userInfo:@{@"data": data}]; [[NSNotificationCenter defaultCenter] postNotificationName:FCMNotificationReceived object:self userInfo:@{@"data": data}];
} }
+ (void)didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(nonnull RCTNotificationResponseCallback)completionHandler + (void)didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(nonnull RCTNotificationResponseCallback)completionHandler
{ {
NSMutableDictionary* data = [[NSMutableDictionary alloc] initWithDictionary: response.notification.request.content.userInfo]; NSMutableDictionary* data = [[NSMutableDictionary alloc] initWithDictionary: response.notification.request.content.userInfo];
[data setValue:@"notification_response" forKey:@"_notificationType"]; [data setValue:@"notification_response" forKey:@"_notificationType"];
[data setValue:@YES forKey:@"opened_from_tray"]; [data setValue:@YES forKey:@"opened_from_tray"];
if (response.actionIdentifier) { if (response.actionIdentifier) {
[data setValue:response.actionIdentifier forKey:@"_actionIdentifier"]; [data setValue:response.actionIdentifier forKey:@"_actionIdentifier"];
} }
[[NSNotificationCenter defaultCenter] postNotificationName:FCMNotificationReceived object:self userInfo:@{@"data": data, @"completionHandler": completionHandler}]; [[NSNotificationCenter defaultCenter] postNotificationName:FCMNotificationReceived object:self userInfo:@{@"data": data, @"completionHandler": completionHandler}];
} }
+ (void)willPresentNotification:(UNNotification *)notification withCompletionHandler:(nonnull RCTWillPresentNotificationCallback)completionHandler + (void)willPresentNotification:(UNNotification *)notification withCompletionHandler:(nonnull RCTWillPresentNotificationCallback)completionHandler
{ {
NSMutableDictionary* data = [[NSMutableDictionary alloc] initWithDictionary: notification.request.content.userInfo]; NSMutableDictionary* data = [[NSMutableDictionary alloc] initWithDictionary: notification.request.content.userInfo];
[data setValue:@"will_present_notification" forKey:@"_notificationType"]; [data setValue:@"will_present_notification" forKey:@"_notificationType"];
[[NSNotificationCenter defaultCenter] postNotificationName:FCMNotificationReceived object:self userInfo:@{@"data": data, @"completionHandler": completionHandler}]; [[NSNotificationCenter defaultCenter] postNotificationName:FCMNotificationReceived object:self userInfo:@{@"data": data, @"completionHandler": completionHandler}];
} }
- (void)dealloc - (void)dealloc
{ {
[[NSNotificationCenter defaultCenter] removeObserver:self]; [[NSNotificationCenter defaultCenter] removeObserver:self];
} }
- (instancetype)init { - (instancetype)init {
self = [super init]; self = [super init];
[[NSNotificationCenter defaultCenter] addObserver:self [[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleNotificationReceived:) selector:@selector(handleNotificationReceived:)
name:FCMNotificationReceived name:FCMNotificationReceived
object:nil]; object:nil];
[[NSNotificationCenter defaultCenter] [[NSNotificationCenter defaultCenter]
addObserver:self selector:@selector(sendDataMessageFailure:) addObserver:self selector:@selector(sendDataMessageFailure:)
name:FIRMessagingSendErrorNotification object:nil]; name:FIRMessagingSendErrorNotification object:nil];
[[NSNotificationCenter defaultCenter] [[NSNotificationCenter defaultCenter]
addObserver:self selector:@selector(sendDataMessageSuccess:) addObserver:self selector:@selector(sendDataMessageSuccess:)
name:FIRMessagingSendSuccessNotification object:nil]; name:FIRMessagingSendSuccessNotification object:nil];
[[NSNotificationCenter defaultCenter] [[NSNotificationCenter defaultCenter]
addObserver:self selector:@selector(connectionStateChanged:) addObserver:self selector:@selector(connectionStateChanged:)
name:FIRMessagingConnectionStateChangedNotification object:nil]; name:FIRMessagingConnectionStateChangedNotification object:nil];
// For iOS 10 data message (sent via FCM) // For iOS 10 data message (sent via FCM)
dispatch_async(dispatch_get_main_queue(), ^{ dispatch_async(dispatch_get_main_queue(), ^{
[[FIRMessaging messaging] setDelegate:self]; [[FIRMessaging messaging] setDelegate:self];
}); });
return self; return self;
} }
RCT_EXPORT_METHOD(enableDirectChannel) RCT_EXPORT_METHOD(enableDirectChannel)
{ {
[[FIRMessaging messaging] setShouldEstablishDirectChannel:@YES]; [[FIRMessaging messaging] setShouldEstablishDirectChannel:@YES];
} }
RCT_EXPORT_METHOD(isDirectChannelEstablished:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) RCT_EXPORT_METHOD(isDirectChannelEstablished:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
{ {
resolve([[FIRMessaging messaging] isDirectChannelEstablished] ? @YES: @NO); resolve([[FIRMessaging messaging] isDirectChannelEstablished] ? @YES: @NO);
} }
RCT_EXPORT_METHOD(getInitialNotification:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) RCT_EXPORT_METHOD(getInitialNotification:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
{ {
UILocalNotification *localUserInfo = _bridge.launchOptions[UIApplicationLaunchOptionsLocalNotificationKey]; UILocalNotification *localUserInfo = self.bridge.launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];
if (localUserInfo) { if (localUserInfo) {
resolve([[localUserInfo userInfo] copy]); resolve([[localUserInfo userInfo] copy]);
} else { } else {
resolve([_bridge.launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey] copy]); resolve([self.bridge.launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey] copy]);
} }
} }
RCT_EXPORT_METHOD(getAPNSToken:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) RCT_EXPORT_METHOD(getAPNSToken:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
{ {
resolve([FIRMessaging messaging].APNSToken); resolve([FIRMessaging messaging].APNSToken);
} }
RCT_EXPORT_METHOD(getFCMToken:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) RCT_EXPORT_METHOD(getFCMToken:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
{ {
resolve([FIRMessaging messaging].FCMToken); resolve([FIRMessaging messaging].FCMToken);
} }
- (void)messaging:(nonnull FIRMessaging *)messaging didRefreshRegistrationToken:(nonnull NSString *)fcmToken { - (void)messaging:(nonnull FIRMessaging *)messaging didRefreshRegistrationToken:(nonnull NSString *)fcmToken {
[self sendEventWithName:FCMTokenRefreshed body:fcmToken]; [self sendEventWithName:FCMTokenRefreshed body:fcmToken];
} }
RCT_EXPORT_METHOD(requestPermissions:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) RCT_EXPORT_METHOD(requestPermissions:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
{ {
if (RCTRunningInAppExtension()) { if (RCTRunningInAppExtension()) {
return; return;
}
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_9_x_Max) {
UIUserNotificationType allNotificationTypes =
(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);
UIApplication *app = RCTSharedApplication();
if ([app respondsToSelector:@selector(registerUserNotificationSettings:)]) {
//iOS 8 or later
UIUserNotificationSettings *notificationSettings =
[UIUserNotificationSettings settingsForTypes:(NSUInteger)allNotificationTypes categories:nil];
[app registerUserNotificationSettings:notificationSettings];
} }
} else { if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_9_x_Max) {
// iOS 10 or later UIUserNotificationType allNotificationTypes =
(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);
UIApplication *app = RCTSharedApplication();
if ([app respondsToSelector:@selector(registerUserNotificationSettings:)]) {
//iOS 8 or later
UIUserNotificationSettings *notificationSettings =
[UIUserNotificationSettings settingsForTypes:(NSUInteger)allNotificationTypes categories:nil];
[app registerUserNotificationSettings:notificationSettings];
}
} else {
// iOS 10 or later
#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 #if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
UNAuthorizationOptions authOptions = UNAuthorizationOptions authOptions =
UNAuthorizationOptionAlert UNAuthorizationOptionAlert
| UNAuthorizationOptionSound | UNAuthorizationOptionSound
| UNAuthorizationOptionBadge; | UNAuthorizationOptionBadge;
[[UNUserNotificationCenter currentNotificationCenter] [[UNUserNotificationCenter currentNotificationCenter]
requestAuthorizationWithOptions:authOptions requestAuthorizationWithOptions:authOptions
completionHandler:^(BOOL granted, NSError * _Nullable error) { completionHandler:^(BOOL granted, NSError * _Nullable error) {
if(granted){ if(granted){
resolve(nil); resolve(nil);
} else{ } else{
reject(@"notification_error", @"Failed to grand permission", error); reject(@"notification_error", @"Failed to grand permission", error);
} }
} }
]; ];
#endif #endif
} }
[[UIApplication sharedApplication] registerForRemoteNotifications]; [[UIApplication sharedApplication] registerForRemoteNotifications];
} }
RCT_EXPORT_METHOD(subscribeToTopic: (NSString*) topic) RCT_EXPORT_METHOD(subscribeToTopic: (NSString*) topic)
{ {
[[FIRMessaging messaging] subscribeToTopic:topic]; [[FIRMessaging messaging] subscribeToTopic:topic];
} }
RCT_EXPORT_METHOD(unsubscribeFromTopic: (NSString*) topic) RCT_EXPORT_METHOD(unsubscribeFromTopic: (NSString*) topic)
{ {
[[FIRMessaging messaging] unsubscribeFromTopic:topic]; [[FIRMessaging messaging] unsubscribeFromTopic:topic];
} }
// Receive data message on iOS 10 devices. // Receive data message on iOS 10 devices.
- (void)applicationReceivedRemoteMessage:(FIRMessagingRemoteMessage *)remoteMessage { - (void)applicationReceivedRemoteMessage:(FIRMessagingRemoteMessage *)remoteMessage {
[self sendEventWithName:FCMNotificationReceived body:[remoteMessage appData]]; [self sendEventWithName:FCMNotificationReceived body:[remoteMessage appData]];
} }
RCT_EXPORT_METHOD(presentLocalNotification:(id)data resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) RCT_EXPORT_METHOD(presentLocalNotification:(id)data resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
{ {
if([UNUserNotificationCenter currentNotificationCenter] != nil){ if([UNUserNotificationCenter currentNotificationCenter] != nil){
UNNotificationRequest* request = [RCTConvert UNNotificationRequest:data]; UNNotificationRequest* request = [RCTConvert UNNotificationRequest:data];
[[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) { [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
if (!error) { if (!error) {
resolve(nil);
}else{
reject(@"notification_error", @"Failed to present local notificaton", error);
}
}];
}else{
UILocalNotification* notif = [RCTConvert UILocalNotification:data];
[RCTSharedApplication() presentLocalNotificationNow:notif];
resolve(nil); resolve(nil);
}else{ }
reject(@"notification_error", @"Failed to present local notificaton", error);
}
}];
}else{
UILocalNotification* notif = [RCTConvert UILocalNotification:data];
[RCTSharedApplication() presentLocalNotificationNow:notif];
resolve(nil);
}
} }
RCT_EXPORT_METHOD(scheduleLocalNotification:(id)data resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) RCT_EXPORT_METHOD(scheduleLocalNotification:(id)data resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
{ {
if([UNUserNotificationCenter currentNotificationCenter] != nil){ if([UNUserNotificationCenter currentNotificationCenter] != nil){
UNNotificationRequest* request = [RCTConvert UNNotificationRequest:data]; UNNotificationRequest* request = [RCTConvert UNNotificationRequest:data];
[[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) { [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
if (!error) { if (!error) {
resolve(nil);
}else{
reject(@"notification_error", @"Failed to present local notificaton", error);
}
}];
}else{
UILocalNotification* notif = [RCTConvert UILocalNotification:data];
[RCTSharedApplication() scheduleLocalNotification:notif];
resolve(nil); resolve(nil);
}else{ }
reject(@"notification_error", @"Failed to present local notificaton", error);
}
}];
}else{
UILocalNotification* notif = [RCTConvert UILocalNotification:data];
[RCTSharedApplication() scheduleLocalNotification:notif];
resolve(nil);
}
} }
RCT_EXPORT_METHOD(removeDeliveredNotification:(NSString*) notificationId) RCT_EXPORT_METHOD(removeDeliveredNotification:(NSString*) notificationId)
{ {
if([UNUserNotificationCenter currentNotificationCenter] != nil){ if([UNUserNotificationCenter currentNotificationCenter] != nil){
[[UNUserNotificationCenter currentNotificationCenter] removeDeliveredNotificationsWithIdentifiers:@[notificationId]]; [[UNUserNotificationCenter currentNotificationCenter] removeDeliveredNotificationsWithIdentifiers:@[notificationId]];
} }
} }
RCT_EXPORT_METHOD(removeAllDeliveredNotifications) RCT_EXPORT_METHOD(removeAllDeliveredNotifications)
{ {
if([UNUserNotificationCenter currentNotificationCenter] != nil){ if([UNUserNotificationCenter currentNotificationCenter] != nil){
[[UNUserNotificationCenter currentNotificationCenter] removeAllDeliveredNotifications]; [[UNUserNotificationCenter currentNotificationCenter] removeAllDeliveredNotifications];
} else { } else {
[RCTSharedApplication() setApplicationIconBadgeNumber: 0]; [RCTSharedApplication() setApplicationIconBadgeNumber: 0];
} }
} }
RCT_EXPORT_METHOD(cancelAllLocalNotifications) RCT_EXPORT_METHOD(cancelAllLocalNotifications)
{ {
if([UNUserNotificationCenter currentNotificationCenter] != nil){ if([UNUserNotificationCenter currentNotificationCenter] != nil){
[[UNUserNotificationCenter currentNotificationCenter] removeAllPendingNotificationRequests]; [[UNUserNotificationCenter currentNotificationCenter] removeAllPendingNotificationRequests];
} else { } else {
[RCTSharedApplication() cancelAllLocalNotifications]; [RCTSharedApplication() cancelAllLocalNotifications];
} }
} }
RCT_EXPORT_METHOD(cancelLocalNotification:(NSString*) notificationId) RCT_EXPORT_METHOD(cancelLocalNotification:(NSString*) notificationId)
{ {
if([UNUserNotificationCenter currentNotificationCenter] != nil){ if([UNUserNotificationCenter currentNotificationCenter] != nil){
[[UNUserNotificationCenter currentNotificationCenter] removePendingNotificationRequestsWithIdentifiers:@[notificationId]]; [[UNUserNotificationCenter currentNotificationCenter] removePendingNotificationRequestsWithIdentifiers:@[notificationId]];
}else { }else {
for (UILocalNotification *notification in [UIApplication sharedApplication].scheduledLocalNotifications) { for (UILocalNotification *notification in [UIApplication sharedApplication].scheduledLocalNotifications) {
NSDictionary<NSString *, id> *notificationInfo = notification.userInfo; NSDictionary<NSString *, id> *notificationInfo = notification.userInfo;
if([notificationId isEqualToString:[notificationInfo valueForKey:@"id"]]){ if([notificationId isEqualToString:[notificationInfo valueForKey:@"id"]]){
[[UIApplication sharedApplication] cancelLocalNotification:notification]; [[UIApplication sharedApplication] cancelLocalNotification:notification];
} }
}
} }
}
} }
RCT_EXPORT_METHOD(getScheduledLocalNotifications:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) RCT_EXPORT_METHOD(getScheduledLocalNotifications:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
{ {
if([UNUserNotificationCenter currentNotificationCenter] != nil){ if([UNUserNotificationCenter currentNotificationCenter] != nil){
[[UNUserNotificationCenter currentNotificationCenter] getPendingNotificationRequestsWithCompletionHandler:^(NSArray<UNNotificationRequest *> * _Nonnull requests) { [[UNUserNotificationCenter currentNotificationCenter] getPendingNotificationRequestsWithCompletionHandler:^(NSArray<UNNotificationRequest *> * _Nonnull requests) {
NSMutableArray* list = [[NSMutableArray alloc] init]; NSMutableArray* list = [[NSMutableArray alloc] init];
for(UNNotificationRequest * notif in requests){ for(UNNotificationRequest * notif in requests){
UNNotificationContent *content = notif.content; UNNotificationContent *content = notif.content;
[list addObject:content.userInfo]; [list addObject:content.userInfo];
} }
resolve(list); resolve(list);
}]; }];
}else{ }else{
NSMutableArray* list = [[NSMutableArray alloc] init]; NSMutableArray* list = [[NSMutableArray alloc] init];
for(UILocalNotification * notif in [RCTSharedApplication() scheduledLocalNotifications]){ for(UILocalNotification * notif in [RCTSharedApplication() scheduledLocalNotifications]){
[list addObject:notif.userInfo]; [list addObject:notif.userInfo];
}
resolve(list);
} }
resolve(list);
}
} }
RCT_EXPORT_METHOD(setBadgeNumber: (NSInteger*) number) RCT_EXPORT_METHOD(setBadgeNumber: (NSInteger*) number)
{ {
[RCTSharedApplication() setApplicationIconBadgeNumber:number]; [RCTSharedApplication() setApplicationIconBadgeNumber:number];
} }
RCT_EXPORT_METHOD(getBadgeNumber: (RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) RCT_EXPORT_METHOD(getBadgeNumber: (RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
{ {
resolve(@([RCTSharedApplication() applicationIconBadgeNumber])); resolve(@([RCTSharedApplication() applicationIconBadgeNumber]));
} }
RCT_EXPORT_METHOD(send:(NSString*)senderId withPayload:(NSDictionary *)message) RCT_EXPORT_METHOD(send:(NSString*)senderId withPayload:(NSDictionary *)message)
{ {
NSMutableDictionary * mMessage = [message mutableCopy]; NSMutableDictionary * mMessage = [message mutableCopy];
NSMutableDictionary * upstreamMessage = [[NSMutableDictionary alloc] init]; NSMutableDictionary * upstreamMessage = [[NSMutableDictionary alloc] init];
for (NSString* key in mMessage) { for (NSString* key in mMessage) {
upstreamMessage[key] = [NSString stringWithFormat:@"%@", [mMessage valueForKey:key]]; upstreamMessage[key] = [NSString stringWithFormat:@"%@", [mMessage valueForKey:key]];
} }
NSDictionary *imMessage = [NSDictionary dictionaryWithDictionary:upstreamMessage]; NSDictionary *imMessage = [NSDictionary dictionaryWithDictionary:upstreamMessage];
int64_t ttl = 3600; int64_t ttl = 3600;
NSString * receiver = [NSString stringWithFormat:@"%@@gcm.googleapis.com", senderId]; NSString * receiver = [NSString stringWithFormat:@"%@@gcm.googleapis.com", senderId];
NSUUID *uuid = [NSUUID UUID]; NSUUID *uuid = [NSUUID UUID];
NSString * messageID = [uuid UUIDString]; NSString * messageID = [uuid UUIDString];
[[FIRMessaging messaging]sendMessage:imMessage to:receiver withMessageID:messageID timeToLive:ttl]; [[FIRMessaging messaging]sendMessage:imMessage to:receiver withMessageID:messageID timeToLive:ttl];
} }
RCT_EXPORT_METHOD(finishRemoteNotification: (NSString *)completionHandlerId fetchResult:(UIBackgroundFetchResult)result){ RCT_EXPORT_METHOD(finishRemoteNotification: (NSString *)completionHandlerId fetchResult:(UIBackgroundFetchResult)result){
RCTRemoteNotificationCallback completionHandler = self.notificationCallbacks[completionHandlerId]; RCTRemoteNotificationCallback completionHandler = self.notificationCallbacks[completionHandlerId];
if (!completionHandler) { if (!completionHandler) {
RCTLogError(@"There is no completion handler with completionHandlerId: %@", completionHandlerId); RCTLogError(@"There is no completion handler with completionHandlerId: %@", completionHandlerId);
return; return;
} }
completionHandler(result); completionHandler(result);
[self.notificationCallbacks removeObjectForKey:completionHandlerId]; [self.notificationCallbacks removeObjectForKey:completionHandlerId];
} }
RCT_EXPORT_METHOD(finishWillPresentNotification: (NSString *)completionHandlerId fetchResult:(UNNotificationPresentationOptions)result){ RCT_EXPORT_METHOD(finishWillPresentNotification: (NSString *)completionHandlerId fetchResult:(UNNotificationPresentationOptions)result){
RCTWillPresentNotificationCallback completionHandler = self.notificationCallbacks[completionHandlerId]; RCTWillPresentNotificationCallback completionHandler = self.notificationCallbacks[completionHandlerId];
if (!completionHandler) { if (!completionHandler) {
RCTLogError(@"There is no completion handler with completionHandlerId: %@", completionHandlerId); RCTLogError(@"There is no completion handler with completionHandlerId: %@", completionHandlerId);
return; return;
} }
completionHandler(result); completionHandler(result);
[self.notificationCallbacks removeObjectForKey:completionHandlerId]; [self.notificationCallbacks removeObjectForKey:completionHandlerId];
} }
RCT_EXPORT_METHOD(finishNotificationResponse: (NSString *)completionHandlerId){ RCT_EXPORT_METHOD(finishNotificationResponse: (NSString *)completionHandlerId){
RCTNotificationResponseCallback completionHandler = self.notificationCallbacks[completionHandlerId]; RCTNotificationResponseCallback completionHandler = self.notificationCallbacks[completionHandlerId];
if (!completionHandler) { if (!completionHandler) {
RCTLogError(@"There is no completion handler with completionHandlerId: %@", completionHandlerId); RCTLogError(@"There is no completion handler with completionHandlerId: %@", completionHandlerId);
return; return;
} }
completionHandler(); completionHandler();
[self.notificationCallbacks removeObjectForKey:completionHandlerId]; [self.notificationCallbacks removeObjectForKey:completionHandlerId];
} }
- (void)handleNotificationReceived:(NSNotification *)notification - (void)handleNotificationReceived:(NSNotification *)notification
{ {
id completionHandler = notification.userInfo[@"completionHandler"]; id completionHandler = notification.userInfo[@"completionHandler"];
NSMutableDictionary* data = notification.userInfo[@"data"]; NSMutableDictionary* data = notification.userInfo[@"data"];
if(completionHandler != nil){ if(completionHandler != nil){
NSString *completionHandlerId = [[NSUUID UUID] UUIDString]; NSString *completionHandlerId = [[NSUUID UUID] UUIDString];
if (!self.notificationCallbacks) { if (!self.notificationCallbacks) {
// Lazy initialization // Lazy initialization
self.notificationCallbacks = [NSMutableDictionary dictionary]; self.notificationCallbacks = [NSMutableDictionary dictionary];
}
self.notificationCallbacks[completionHandlerId] = completionHandler;
data[@"_completionHandlerId"] = completionHandlerId;
} }
self.notificationCallbacks[completionHandlerId] = completionHandler;
data[@"_completionHandlerId"] = completionHandlerId; [self sendEventWithName:FCMNotificationReceived body:data];
}
[self sendEventWithName:FCMNotificationReceived body:data];
} }
- (void)sendDataMessageFailure:(NSNotification *)notification - (void)sendDataMessageFailure:(NSNotification *)notification
{ {
NSString *messageID = (NSString *)notification.userInfo[@"messageID"]; NSString *messageID = (NSString *)notification.userInfo[@"messageID"];
NSLog(@"sendDataMessageFailure: %@", messageID); NSLog(@"sendDataMessageFailure: %@", messageID);
} }
- (void)sendDataMessageSuccess:(NSNotification *)notification - (void)sendDataMessageSuccess:(NSNotification *)notification
{ {
NSString *messageID = (NSString *)notification.userInfo[@"messageID"]; NSString *messageID = (NSString *)notification.userInfo[@"messageID"];
NSLog(@"sendDataMessageSuccess: %@", messageID); NSLog(@"sendDataMessageSuccess: %@", messageID);
} }
- (void)connectionStateChanged:(NSNotification *)notification - (void)connectionStateChanged:(NSNotification *)notification
{ {
[self sendEventWithName:FCMDirectChannelConnectionChanged body:[FIRMessaging messaging].isDirectChannelEstablished ? @YES: @NO]; [self sendEventWithName:FCMDirectChannelConnectionChanged body:[FIRMessaging messaging].isDirectChannelEstablished ? @YES: @NO];
NSLog(@"connectionStateChanged: %@", [FIRMessaging messaging].isDirectChannelEstablished ? @"connected": @"disconnected"); NSLog(@"connectionStateChanged: %@", [FIRMessaging messaging].isDirectChannelEstablished ? @"connected": @"disconnected");
} }
@end @end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment