Commit 4e4018b4 authored by Libin Lu's avatar Libin Lu

use constants

parent 0c7584e0
...@@ -16,6 +16,13 @@ export const WillPresentNotificationResult = { ...@@ -16,6 +16,13 @@ export const WillPresentNotificationResult = {
None: 'UNNotificationPresentationOptionNone' None: 'UNNotificationPresentationOptionNone'
} }
export const NotificationType = {
Remote: 'remote_notification',
NotificationResponse: 'notification_response',
WillPresent: 'will_present_notification',
Local: 'local_notification'
}
const RNFIRMessaging = NativeModules.RNFIRMessaging; const RNFIRMessaging = NativeModules.RNFIRMessaging;
const FCM = {}; const FCM = {};
...@@ -87,18 +94,18 @@ function finish(result){ ...@@ -87,18 +94,18 @@ function finish(result){
} }
if(!this._finishCalled && this._completionHandlerId){ if(!this._finishCalled && this._completionHandlerId){
this._finishCalled = true; this._finishCalled = true;
switch(this.notification_event_type){ switch(this._notificationType){
case 'remote_notification': case NotificationType.Remote:
result = result || RemoteNotificationResult.NoData; result = result || RemoteNotificationResult.NoData;
if(!Object.values(RemoteNotificationResult).includes(result)){ if(!Object.values(RemoteNotificationResult).includes(result)){
throw new Error(`Invalid RemoteNotificationResult, use import {RemoteNotificationResult} from 'react-native-fcm' to avoid typo`); throw new Error(`Invalid RemoteNotificationResult, use import {RemoteNotificationResult} from 'react-native-fcm' to avoid typo`);
} }
RNFIRMessaging.finishRemoteNotification(this._completionHandlerId, result); RNFIRMessaging.finishRemoteNotification(this._completionHandlerId, result);
return; return;
case 'notification_response': case NotificationType.NotificationResponse:
RNFIRMessaging.finishNotificationResponse(this._completionHandlerId); RNFIRMessaging.finishNotificationResponse(this._completionHandlerId);
return; return;
case 'will_present_notification': case NotificationType.WillPresent:
result = result || (this.show_in_foreground ? WillPresentNotificationResult.All : WillPresentNotificationResult.None); result = result || (this.show_in_foreground ? WillPresentNotificationResult.All : WillPresentNotificationResult.None);
if(!Object.values(WillPresentNotificationResult).includes(result)){ if(!Object.values(WillPresentNotificationResult).includes(result)){
throw new Error(`Invalid WillPresentNotificationResult, make sure you use import {WillPresentNotificationResult} from 'react-native-fcm' to avoid typo`); throw new Error(`Invalid WillPresentNotificationResult, make sure you use import {WillPresentNotificationResult} from 'react-native-fcm' to avoid typo`);
......
...@@ -144,21 +144,21 @@ RCT_EXPORT_MODULE() ...@@ -144,21 +144,21 @@ RCT_EXPORT_MODULE()
+ (void)didReceiveRemoteNotification:(nonnull NSDictionary *)userInfo fetchCompletionHandler:(RCTRemoteNotificationCallback)completionHandler { + (void)didReceiveRemoteNotification:(nonnull NSDictionary *)userInfo fetchCompletionHandler:(RCTRemoteNotificationCallback)completionHandler {
NSMutableDictionary* data = [[NSMutableDictionary alloc] initWithDictionary: userInfo]; NSMutableDictionary* data = [[NSMutableDictionary alloc] initWithDictionary: userInfo];
[data setValue:@"remote_notification" forKey:@"notification_event_type"]; [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:@"notification_event_type"]; [data setValue:@"local_notification" forKey:@"_notificationType"];
[[NSNotificationCenter defaultCenter] postNotificationName:FCMNotificationReceived object:self userInfo:data]; [[NSNotificationCenter defaultCenter] postNotificationName:FCMNotificationReceived object:self userInfo:data];
} }
+ (void)didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(RCTNotificationResponseCallback)completionHandler + (void)didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(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:@"notification_event_type"]; [data setValue:@"notification_response" forKey:@"_notificationType"];
[data setValue:@YES forKey:@"opened_from_tray"]; [data setValue:@YES 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}];
} }
...@@ -166,7 +166,7 @@ RCT_EXPORT_MODULE() ...@@ -166,7 +166,7 @@ RCT_EXPORT_MODULE()
+ (void)willPresentNotification:(UNNotification *)notification withCompletionHandler:(RCTWillPresentNotificationCallback)completionHandler + (void)willPresentNotification:(UNNotification *)notification withCompletionHandler:(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:@"notification_event_type"]; [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}];
} }
......
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