Commit 9f67c534 authored by Libin Lu's avatar Libin Lu

add delayed refresh token message

parent 8076bd44
...@@ -54,13 +54,13 @@ RCT_ENUM_CONVERTER(NSCalendarUnit, ...@@ -54,13 +54,13 @@ RCT_ENUM_CONVERTER(NSCalendarUnit,
content.categoryIdentifier = [RCTConvert NSString:details[@"click_action"]]; content.categoryIdentifier = [RCTConvert NSString:details[@"click_action"]];
content.userInfo = details; content.userInfo = details;
content.badge = [RCTConvert NSNumber:details[@"badge"]]; content.badge = [RCTConvert NSNumber:details[@"badge"]];
NSDate *fireDate = [RCTConvert NSDate:details[@"fire_date"]]; NSDate *fireDate = [RCTConvert NSDate:details[@"fire_date"]];
if(fireDate == nil){ if(fireDate == nil){
return [UNNotificationRequest requestWithIdentifier:[RCTConvert NSString:details[@"id"]] content:content trigger:nil]; return [UNNotificationRequest requestWithIdentifier:[RCTConvert NSString:details[@"id"]] content:content trigger:nil];
} }
NSCalendarUnit interval = [RCTConvert NSCalendarUnit:details[@"repeat_interval"]]; NSCalendarUnit interval = [RCTConvert NSCalendarUnit:details[@"repeat_interval"]];
NSCalendarUnit unitFlags; NSCalendarUnit unitFlags;
switch (interval) { switch (interval) {
...@@ -145,11 +145,11 @@ typedef NS_ENUM(NSUInteger, UNNotificationActionType) { ...@@ -145,11 +145,11 @@ typedef NS_ENUM(NSUInteger, UNNotificationActionType) {
NSString *title = [RCTConvert NSString: details[@"title"]]; NSString *title = [RCTConvert NSString: details[@"title"]];
UNNotificationActionOptions options = [RCTConvert UNNotificationActionOptions: details[@"options"]]; UNNotificationActionOptions options = [RCTConvert UNNotificationActionOptions: details[@"options"]];
UNNotificationActionType type = [RCTConvert UNNotificationActionType:details[@"type"]]; UNNotificationActionType type = [RCTConvert UNNotificationActionType:details[@"type"]];
if (type == UNNotificationActionTypeTextInput) { if (type == UNNotificationActionTypeTextInput) {
NSString *textInputButtonTitle = [RCTConvert NSString: details[@"textInputButtonTitle"]]; NSString *textInputButtonTitle = [RCTConvert NSString: details[@"textInputButtonTitle"]];
NSString *textInputPlaceholder = [RCTConvert NSString: details[@"textInputPlaceholder"]]; NSString *textInputPlaceholder = [RCTConvert NSString: details[@"textInputPlaceholder"]];
return [UNTextInputNotificationAction actionWithIdentifier:identifier title:title options:options textInputButtonTitle:textInputButtonTitle textInputPlaceholder:textInputPlaceholder]; return [UNTextInputNotificationAction actionWithIdentifier:identifier title:title options:options textInputButtonTitle:textInputButtonTitle textInputPlaceholder:textInputPlaceholder];
} }
...@@ -187,17 +187,17 @@ RCT_MULTI_ENUM_CONVERTER(UNNotificationActionOptions, (@{ ...@@ -187,17 +187,17 @@ RCT_MULTI_ENUM_CONVERTER(UNNotificationActionOptions, (@{
for (NSDictionary *actionDict in details[@"actions"]) { for (NSDictionary *actionDict in details[@"actions"]) {
[actions addObject:[RCTConvert UNNotificationAction:actionDict]]; [actions addObject:[RCTConvert UNNotificationAction:actionDict]];
} }
NSArray<NSString *> *intentIdentifiers = [RCTConvert NSStringArray:details[@"intentIdentifiers"]]; NSArray<NSString *> *intentIdentifiers = [RCTConvert NSStringArray:details[@"intentIdentifiers"]];
NSString *hiddenPreviewsBodyPlaceholder = [RCTConvert NSString:details[@"hiddenPreviewsBodyPlaceholder"]]; NSString *hiddenPreviewsBodyPlaceholder = [RCTConvert NSString:details[@"hiddenPreviewsBodyPlaceholder"]];
UNNotificationCategoryOptions options = [RCTConvert UNNotificationCategoryOptions: details[@"options"]]; UNNotificationCategoryOptions options = [RCTConvert UNNotificationCategoryOptions: details[@"options"]];
if (hiddenPreviewsBodyPlaceholder) { if (hiddenPreviewsBodyPlaceholder) {
#if defined(__IPHONE_11_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_11_0 #if defined(__IPHONE_11_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_11_0
return [UNNotificationCategory categoryWithIdentifier:identifier actions:actions intentIdentifiers:intentIdentifiers hiddenPreviewsBodyPlaceholder:hiddenPreviewsBodyPlaceholder options:options]; return [UNNotificationCategory categoryWithIdentifier:identifier actions:actions intentIdentifiers:intentIdentifiers hiddenPreviewsBodyPlaceholder:hiddenPreviewsBodyPlaceholder options:options];
#endif #endif
} }
return [UNNotificationCategory categoryWithIdentifier:identifier actions:actions intentIdentifiers:intentIdentifiers options:options]; return [UNNotificationCategory categoryWithIdentifier:identifier actions:actions intentIdentifiers:intentIdentifiers options:options];
} }
...@@ -229,6 +229,7 @@ RCT_MULTI_ENUM_CONVERTER(UNNotificationCategoryOptions, (@{ ...@@ -229,6 +229,7 @@ RCT_MULTI_ENUM_CONVERTER(UNNotificationCategoryOptions, (@{
static bool jsHandlerRegistered; static bool jsHandlerRegistered;
static NSMutableArray* pendingNotifications; static NSMutableArray* pendingNotifications;
static NSString* refreshToken;
RCT_EXPORT_MODULE(); RCT_EXPORT_MODULE();
...@@ -262,14 +263,14 @@ RCT_EXPORT_MODULE(); ...@@ -262,14 +263,14 @@ RCT_EXPORT_MODULE();
if (response.actionIdentifier) { if (response.actionIdentifier) {
[data setValue:response.actionIdentifier forKey:@"_actionIdentifier"]; [data setValue:response.actionIdentifier forKey:@"_actionIdentifier"];
} }
if ([response isKindOfClass:UNTextInputNotificationResponse.class]) { if ([response isKindOfClass:UNTextInputNotificationResponse.class]) {
[data setValue:[(UNTextInputNotificationResponse *)response userText] forKey:@"_userText"]; [data setValue:[(UNTextInputNotificationResponse *)response userText] forKey:@"_userText"];
} }
NSDictionary *userInfo = @{@"data": data, @"completionHandler": completionHandler}; NSDictionary *userInfo = @{@"data": data, @"completionHandler": completionHandler};
[self sendNotificationEventWhenAvailable:userInfo]; [self sendNotificationEventWhenAvailable:userInfo];
} }
+ (void)willPresentNotification:(UNNotification *)notification withCompletionHandler:(nonnull RCTWillPresentNotificationCallback)completionHandler + (void)willPresentNotification:(UNNotification *)notification withCompletionHandler:(nonnull RCTWillPresentNotificationCallback)completionHandler
...@@ -281,15 +282,15 @@ RCT_EXPORT_MODULE(); ...@@ -281,15 +282,15 @@ RCT_EXPORT_MODULE();
+ (void)sendNotificationEventWhenAvailable:(NSDictionary*)data + (void)sendNotificationEventWhenAvailable:(NSDictionary*)data
{ {
if(!jsHandlerRegistered){ if(!jsHandlerRegistered){
// JS hasn't registered callback yet. hold on that // JS hasn't registered callback yet. hold on that
if(!pendingNotifications){ if(!pendingNotifications){
pendingNotifications = [NSMutableArray array]; pendingNotifications = [NSMutableArray array];
}
[pendingNotifications addObject:data];
} else {
[[NSNotificationCenter defaultCenter] postNotificationName:FCMNotificationReceived object:self userInfo:data];
} }
[pendingNotifications addObject:data];
} else {
[[NSNotificationCenter defaultCenter] postNotificationName:FCMNotificationReceived object:self userInfo:data];
}
} }
- (void)dealloc - (void)dealloc
...@@ -299,58 +300,64 @@ RCT_EXPORT_MODULE(); ...@@ -299,58 +300,64 @@ RCT_EXPORT_MODULE();
- (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];
}); });
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
if(!jsHandlerRegistered){ if(!jsHandlerRegistered){
[self sendPendingNotifications]; [self sendPendingNotifications];
} }
}); if(refreshToken != nil){
[self sendEventWithName:FCMTokenRefreshed body:refreshToken];
}
});
return self; return self;
} }
-(void) addListener:(NSString *)eventName { -(void) addListener:(NSString *)eventName {
[super addListener:eventName]; [super addListener:eventName];
if([eventName isEqualToString:FCMNotificationReceived]) { if([eventName isEqualToString:FCMNotificationReceived]) {
[self sendPendingNotifications]; [self sendPendingNotifications];
} } else if([eventName isEqualToString:FCMTokenRefreshed] && refreshToken != nil) {
[self sendEventWithName:FCMTokenRefreshed body:refreshToken];
refreshToken = nil;
}
} }
-(void) sendPendingNotifications { -(void) sendPendingNotifications {
static dispatch_once_t onceToken; static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{ dispatch_once(&onceToken, ^{
jsHandlerRegistered = true; jsHandlerRegistered = true;
for (NSDictionary* data in pendingNotifications) { for (NSDictionary* data in pendingNotifications) {
[[NSNotificationCenter defaultCenter] postNotificationName:FCMNotificationReceived object:self userInfo:data]; [[NSNotificationCenter defaultCenter] postNotificationName:FCMNotificationReceived object:self userInfo:data];
} }
[pendingNotifications removeAllObjects]; [pendingNotifications removeAllObjects];
}); });
} }
RCT_EXPORT_METHOD(enableDirectChannel) RCT_EXPORT_METHOD(enableDirectChannel)
...@@ -365,21 +372,21 @@ RCT_EXPORT_METHOD(isDirectChannelEstablished:(RCTPromiseResolveBlock)resolve rej ...@@ -365,21 +372,21 @@ RCT_EXPORT_METHOD(isDirectChannelEstablished:(RCTPromiseResolveBlock)resolve rej
RCT_EXPORT_METHOD(getInitialNotification:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) RCT_EXPORT_METHOD(getInitialNotification:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
{ {
NSDictionary* initialNotif; NSDictionary* initialNotif;
NSDictionary *localUserInfo = [[self.bridge.launchOptions[UIApplicationLaunchOptionsLocalNotificationKey] userInfo] mutableCopy]; NSDictionary *localUserInfo = [[self.bridge.launchOptions[UIApplicationLaunchOptionsLocalNotificationKey] userInfo] mutableCopy];
NSDictionary *remoteUserInfo = [self.bridge.launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey] mutableCopy]; NSDictionary *remoteUserInfo = [self.bridge.launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey] mutableCopy];
if(localUserInfo){ if(localUserInfo){
initialNotif = localUserInfo; initialNotif = localUserInfo;
} else if (remoteUserInfo) { } else if (remoteUserInfo) {
initialNotif = remoteUserInfo; initialNotif = remoteUserInfo;
} }
if (initialNotif) { if (initialNotif) {
[initialNotif setValue:@YES forKey:@"opened_from_tray"]; [initialNotif setValue:@YES forKey:@"opened_from_tray"];
resolve(initialNotif); resolve(initialNotif);
} else { } else {
resolve(nil); resolve(nil);
} }
} }
...@@ -405,14 +412,14 @@ RCT_EXPORT_METHOD(getEntityFCMToken:(RCTPromiseResolveBlock)resolve rejecter:(RC ...@@ -405,14 +412,14 @@ RCT_EXPORT_METHOD(getEntityFCMToken:(RCTPromiseResolveBlock)resolve rejecter:(RC
FIROptions *options = FIROptions.defaultOptions; FIROptions *options = FIROptions.defaultOptions;
NSString *entity = options.GCMSenderID; NSString *entity = options.GCMSenderID;
NSData * deviceToken = [FIRMessaging messaging].APNSToken; NSData * deviceToken = [FIRMessaging messaging].APNSToken;
if (deviceToken == nil) { if (deviceToken == nil) {
resolve(nil); resolve(nil);
return; return;
} }
[[FIRInstanceID instanceID]tokenWithAuthorizedEntity:entity scope:kFIRInstanceIDScopeFirebaseMessaging options:@{@"apns_token": deviceToken} handler:^(NSString * _Nullable token, NSError * _Nullable error) { [[FIRInstanceID instanceID]tokenWithAuthorizedEntity:entity scope:kFIRInstanceIDScopeFirebaseMessaging options:@{@"apns_token": deviceToken} handler:^(NSString * _Nullable token, NSError * _Nullable error) {
if (error != nil) { if (error != nil) {
reject([NSString stringWithFormat:@"%ld",error.code],error.localizedDescription,nil); reject([NSString stringWithFormat:@"%ld",error.code],error.localizedDescription,nil);
} else { } else {
...@@ -425,9 +432,9 @@ RCT_EXPORT_METHOD(deleteEntityFCMToken:(RCTPromiseResolveBlock)resolve rejecter: ...@@ -425,9 +432,9 @@ RCT_EXPORT_METHOD(deleteEntityFCMToken:(RCTPromiseResolveBlock)resolve rejecter:
{ {
FIROptions *options = FIROptions.defaultOptions;; FIROptions *options = FIROptions.defaultOptions;;
NSString *entity = options.GCMSenderID; NSString *entity = options.GCMSenderID;
[[FIRInstanceID instanceID]deleteTokenWithAuthorizedEntity:entity scope:kFIRInstanceIDScopeFirebaseMessaging handler:^(NSError * _Nullable error) { [[FIRInstanceID instanceID]deleteTokenWithAuthorizedEntity:entity scope:kFIRInstanceIDScopeFirebaseMessaging handler:^(NSError * _Nullable error) {
if (error != nil) { if (error != nil) {
reject([NSString stringWithFormat:@"%ld",error.code],error.localizedDescription,nil); reject([NSString stringWithFormat:@"%ld",error.code],error.localizedDescription,nil);
} else { } else {
...@@ -438,17 +445,18 @@ RCT_EXPORT_METHOD(deleteEntityFCMToken:(RCTPromiseResolveBlock)resolve rejecter: ...@@ -438,17 +445,18 @@ RCT_EXPORT_METHOD(deleteEntityFCMToken:(RCTPromiseResolveBlock)resolve rejecter:
RCT_EXPORT_METHOD(deleteInstanceId:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) RCT_EXPORT_METHOD(deleteInstanceId:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
{ {
[[FIRInstanceID instanceID]deleteIDWithHandler:^(NSError * _Nullable error) { [[FIRInstanceID instanceID]deleteIDWithHandler:^(NSError * _Nullable error) {
if (error != nil) { if (error != nil) {
reject([NSString stringWithFormat:@"%ld",error.code],error.localizedDescription,nil); reject([NSString stringWithFormat:@"%ld",error.code],error.localizedDescription,nil);
} else { } else {
resolve(nil); resolve(nil);
} }
}]; }];
} }
- (void)messaging:(nonnull FIRMessaging *)messaging didRefreshRegistrationToken:(nonnull NSString *)fcmToken { - (void)messaging:(nonnull FIRMessaging *)messaging didRefreshRegistrationToken:(nonnull NSString *)fcmToken {
refreshToken = fcmToken;
[self sendEventWithName:FCMTokenRefreshed body:fcmToken]; [self sendEventWithName:FCMTokenRefreshed body:fcmToken];
} }
...@@ -488,7 +496,7 @@ RCT_EXPORT_METHOD(requestPermissions:(RCTPromiseResolveBlock)resolve rejecter:(R ...@@ -488,7 +496,7 @@ RCT_EXPORT_METHOD(requestPermissions:(RCTPromiseResolveBlock)resolve rejecter:(R
]; ];
#endif #endif
} }
dispatch_async(dispatch_get_main_queue(), ^{ dispatch_async(dispatch_get_main_queue(), ^{
[[UIApplication sharedApplication] registerForRemoteNotifications]; [[UIApplication sharedApplication] registerForRemoteNotifications];
}); });
...@@ -610,12 +618,12 @@ RCT_EXPORT_METHOD(setNotificationCategories:(NSArray *)categories) ...@@ -610,12 +618,12 @@ RCT_EXPORT_METHOD(setNotificationCategories:(NSArray *)categories)
{ {
if([UNUserNotificationCenter currentNotificationCenter] != nil) { if([UNUserNotificationCenter currentNotificationCenter] != nil) {
NSMutableSet *categoriesSet = [[NSMutableSet alloc] init]; NSMutableSet *categoriesSet = [[NSMutableSet alloc] init];
for(NSDictionary *categoryDict in categories) { for(NSDictionary *categoryDict in categories) {
UNNotificationCategory *category = [RCTConvert UNNotificationCategory:categoryDict]; UNNotificationCategory *category = [RCTConvert UNNotificationCategory:categoryDict];
[categoriesSet addObject:category]; [categoriesSet addObject:category];
} }
[[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:categoriesSet]; [[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:categoriesSet];
} }
} }
...@@ -639,15 +647,15 @@ RCT_EXPORT_METHOD(send:(NSString*)senderId withPayload:(NSDictionary *)message) ...@@ -639,15 +647,15 @@ RCT_EXPORT_METHOD(send:(NSString*)senderId withPayload:(NSDictionary *)message)
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];
} }
...@@ -700,14 +708,14 @@ RCT_EXPORT_METHOD(finishNotificationResponse: (NSString *)completionHandlerId){ ...@@ -700,14 +708,14 @@ RCT_EXPORT_METHOD(finishNotificationResponse: (NSString *)completionHandlerId){
- (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);
} }
......
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