2017-03-09 15:26:28 +00:00
|
|
|
#import "RNFirebaseMessaging.h"
|
2017-05-31 08:54:02 +00:00
|
|
|
|
|
|
|
#if __has_include(<FirebaseMessaging/FirebaseMessaging.h>)
|
2018-02-02 08:40:48 +00:00
|
|
|
@import UserNotifications;
|
2017-05-30 07:26:40 +00:00
|
|
|
#import "RNFirebaseEvents.h"
|
2017-10-26 10:55:07 +00:00
|
|
|
#import "RNFirebaseUtil.h"
|
2017-05-31 08:54:02 +00:00
|
|
|
#import <FirebaseMessaging/FirebaseMessaging.h>
|
2017-10-03 08:56:47 +00:00
|
|
|
#import <FirebaseInstanceID/FIRInstanceID.h>
|
2017-03-09 15:26:28 +00:00
|
|
|
|
2017-03-21 15:11:07 +00:00
|
|
|
#import <React/RCTEventDispatcher.h>
|
2017-05-05 08:46:22 +00:00
|
|
|
#import <React/RCTConvert.h>
|
2017-03-21 15:11:07 +00:00
|
|
|
#import <React/RCTUtils.h>
|
|
|
|
|
2018-02-02 12:05:51 +00:00
|
|
|
#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
|
|
|
|
@import UserNotifications;
|
2018-02-14 09:07:29 +00:00
|
|
|
#endif
|
2017-03-09 15:26:28 +00:00
|
|
|
|
|
|
|
@implementation RNFirebaseMessaging
|
|
|
|
|
2018-02-02 12:05:51 +00:00
|
|
|
static RNFirebaseMessaging *theRNFirebaseMessaging = nil;
|
2018-04-13 13:22:17 +00:00
|
|
|
static bool jsReady = FALSE;
|
|
|
|
static NSString* initialToken = nil;
|
2018-05-16 16:37:58 +00:00
|
|
|
static NSMutableArray* pendingMessages = nil;
|
2018-02-02 12:05:51 +00:00
|
|
|
|
|
|
|
+ (nonnull instancetype)instance {
|
|
|
|
return theRNFirebaseMessaging;
|
|
|
|
}
|
|
|
|
|
2017-03-21 15:11:07 +00:00
|
|
|
RCT_EXPORT_MODULE()
|
|
|
|
|
2017-05-30 07:26:40 +00:00
|
|
|
- (id)init {
|
|
|
|
self = [super init];
|
|
|
|
if (self != nil) {
|
2017-05-30 10:44:06 +00:00
|
|
|
NSLog(@"Setting up RNFirebaseMessaging instance");
|
2018-02-02 12:05:51 +00:00
|
|
|
[self configure];
|
2017-03-15 12:57:36 +00:00
|
|
|
}
|
2017-05-30 07:26:40 +00:00
|
|
|
return self;
|
2017-03-09 15:26:28 +00:00
|
|
|
}
|
|
|
|
|
2018-02-02 12:05:51 +00:00
|
|
|
- (void)configure {
|
|
|
|
// Set as delegate for FIRMessaging
|
|
|
|
[FIRMessaging messaging].delegate = self;
|
2018-02-09 09:23:03 +00:00
|
|
|
|
2017-05-30 07:26:40 +00:00
|
|
|
// Establish Firebase managed data channel
|
|
|
|
[FIRMessaging messaging].shouldEstablishDirectChannel = YES;
|
2018-02-18 17:09:13 +00:00
|
|
|
|
2018-02-02 12:05:51 +00:00
|
|
|
// Set static instance for use from AppDelegate
|
2018-02-05 09:18:53 +00:00
|
|
|
theRNFirebaseMessaging = self;
|
2017-03-21 15:11:07 +00:00
|
|
|
}
|
|
|
|
|
2018-02-05 17:45:08 +00:00
|
|
|
// *******************************************************
|
|
|
|
// ** Start AppDelegate methods
|
|
|
|
// ** iOS 8/9 Only
|
|
|
|
// *******************************************************
|
2018-02-02 12:05:51 +00:00
|
|
|
|
2018-02-05 17:45:08 +00:00
|
|
|
// Listen for permission response
|
|
|
|
- (void) didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
|
|
|
|
if (notificationSettings.types == UIUserNotificationTypeNone) {
|
|
|
|
if (_permissionRejecter) {
|
|
|
|
_permissionRejecter(@"messaging/permission_error", @"Failed to grant permission", nil);
|
|
|
|
}
|
|
|
|
} else if (_permissionResolver) {
|
|
|
|
_permissionResolver(nil);
|
|
|
|
}
|
|
|
|
_permissionRejecter = nil;
|
|
|
|
_permissionResolver = nil;
|
|
|
|
}
|
|
|
|
|
2018-02-22 15:52:24 +00:00
|
|
|
// Listen for FCM data messages that arrive as a remote notification
|
|
|
|
- (void)didReceiveRemoteNotification:(nonnull NSDictionary *)userInfo {
|
|
|
|
NSDictionary *message = [self parseUserInfo:userInfo];
|
2018-04-13 13:22:17 +00:00
|
|
|
[self sendJSEvent:self name:MESSAGING_MESSAGE_RECEIVED body:message];
|
2018-02-22 15:52:24 +00:00
|
|
|
}
|
|
|
|
|
2018-02-05 17:45:08 +00:00
|
|
|
// *******************************************************
|
|
|
|
// ** Finish AppDelegate methods
|
|
|
|
// *******************************************************
|
|
|
|
|
|
|
|
|
|
|
|
// *******************************************************
|
|
|
|
// ** Start FIRMessagingDelegate methods
|
|
|
|
// ** iOS 8+
|
|
|
|
// *******************************************************
|
2018-02-02 12:05:51 +00:00
|
|
|
|
|
|
|
// Listen for FCM tokens
|
|
|
|
- (void)messaging:(FIRMessaging *)messaging didReceiveRegistrationToken:(NSString *)fcmToken {
|
|
|
|
NSLog(@"Received new FCM token: %@", fcmToken);
|
2018-04-13 13:22:17 +00:00
|
|
|
[self sendJSEvent:self name:MESSAGING_TOKEN_REFRESHED body:fcmToken];
|
2018-02-02 12:05:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Listen for data messages in the foreground
|
|
|
|
- (void)applicationReceivedRemoteMessage:(nonnull FIRMessagingRemoteMessage *)remoteMessage {
|
2018-02-22 15:52:24 +00:00
|
|
|
NSDictionary *message = [self parseFIRMessagingRemoteMessage:remoteMessage];
|
2018-04-13 13:22:17 +00:00
|
|
|
[self sendJSEvent:self name:MESSAGING_MESSAGE_RECEIVED body:message];
|
2018-02-22 15:52:24 +00:00
|
|
|
}
|
2018-02-09 09:23:03 +00:00
|
|
|
|
2018-02-22 15:52:24 +00:00
|
|
|
// Receive data messages on iOS 10+ directly from FCM (bypassing APNs) when the app is in the foreground.
|
|
|
|
// To enable direct data messages, you can set [Messaging messaging].shouldEstablishDirectChannel to YES.
|
|
|
|
- (void)messaging:(nonnull FIRMessaging *)messaging
|
|
|
|
didReceiveMessage:(nonnull FIRMessagingRemoteMessage *)remoteMessage {
|
|
|
|
NSDictionary *message = [self parseFIRMessagingRemoteMessage:remoteMessage];
|
2018-04-13 13:22:17 +00:00
|
|
|
[self sendJSEvent:self name:MESSAGING_MESSAGE_RECEIVED body:message];
|
2018-02-02 12:05:51 +00:00
|
|
|
}
|
2018-02-09 09:23:03 +00:00
|
|
|
|
2018-02-05 17:45:08 +00:00
|
|
|
// *******************************************************
|
|
|
|
// ** Finish FIRMessagingDelegate methods
|
|
|
|
// *******************************************************
|
2018-02-05 09:18:53 +00:00
|
|
|
|
2017-05-30 07:26:40 +00:00
|
|
|
// ** Start React Module methods **
|
|
|
|
RCT_EXPORT_METHOD(getToken:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
|
2018-02-02 08:40:48 +00:00
|
|
|
resolve([[FIRInstanceID instanceID] token]);
|
2017-09-26 14:44:15 +00:00
|
|
|
}
|
|
|
|
|
2018-02-02 08:40:48 +00:00
|
|
|
RCT_EXPORT_METHOD(requestPermission:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
|
2017-03-21 15:11:07 +00:00
|
|
|
if (RCTRunningInAppExtension()) {
|
2018-02-05 17:45:08 +00:00
|
|
|
reject(@"messaging/request-permission-unavailable", @"requestPermission is not supported in App Extensions", nil);
|
2017-03-21 15:11:07 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-02-09 09:23:03 +00:00
|
|
|
|
2017-03-21 15:11:07 +00:00
|
|
|
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_9_x_Max) {
|
2018-02-02 08:40:48 +00:00
|
|
|
UIUserNotificationType types = (UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);
|
2018-02-05 19:07:00 +00:00
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
[RCTSharedApplication() registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:types categories:nil]];
|
|
|
|
// We set the promise for usage by the AppDelegate callback which listens
|
|
|
|
// for the result of the permission request
|
2018-04-13 13:22:17 +00:00
|
|
|
self.permissionRejecter = reject;
|
|
|
|
self.permissionResolver = resolve;
|
2018-02-05 19:07:00 +00:00
|
|
|
});
|
2017-03-15 12:57:36 +00:00
|
|
|
} else {
|
2018-04-13 13:22:17 +00:00
|
|
|
if (@available(iOS 10.0, *)) {
|
2018-02-02 08:40:48 +00:00
|
|
|
// For iOS 10 display notification (sent via APNS)
|
|
|
|
UNAuthorizationOptions authOptions = UNAuthorizationOptionAlert | UNAuthorizationOptionSound | UNAuthorizationOptionBadge;
|
|
|
|
[[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:authOptions completionHandler:^(BOOL granted, NSError * _Nullable error) {
|
|
|
|
if (granted) {
|
2018-02-05 17:45:08 +00:00
|
|
|
resolve(nil);
|
2018-02-02 08:40:48 +00:00
|
|
|
} else {
|
2018-02-05 17:45:08 +00:00
|
|
|
reject(@"messaging/permission_error", @"Failed to grant permission", error);
|
2018-02-02 08:40:48 +00:00
|
|
|
}
|
|
|
|
}];
|
2018-04-13 13:22:17 +00:00
|
|
|
}
|
2017-03-15 12:57:36 +00:00
|
|
|
}
|
2018-02-09 09:23:03 +00:00
|
|
|
|
2017-10-24 08:51:21 +00:00
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
[RCTSharedApplication() registerForRemoteNotifications];
|
|
|
|
});
|
2017-03-09 15:26:28 +00:00
|
|
|
}
|
2018-02-05 09:18:53 +00:00
|
|
|
|
2018-02-02 08:40:48 +00:00
|
|
|
// Non Web SDK methods
|
2018-03-28 10:02:39 +00:00
|
|
|
RCT_EXPORT_METHOD(hasPermission:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
|
2018-02-05 18:04:10 +00:00
|
|
|
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_9_x_Max) {
|
2018-02-05 19:07:00 +00:00
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
resolve(@([RCTSharedApplication() currentUserNotificationSettings].types != UIUserNotificationTypeNone));
|
|
|
|
});
|
2018-02-05 18:04:10 +00:00
|
|
|
} else {
|
2018-04-13 13:22:17 +00:00
|
|
|
if (@available(iOS 10.0, *)) {
|
2018-02-05 18:04:10 +00:00
|
|
|
[[UNUserNotificationCenter currentNotificationCenter] getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
|
|
|
|
resolve(@(settings.alertSetting == UNNotificationSettingEnabled));
|
|
|
|
}];
|
2018-04-13 13:22:17 +00:00
|
|
|
}
|
2018-02-05 18:04:10 +00:00
|
|
|
}
|
|
|
|
}
|
2018-02-09 09:23:03 +00:00
|
|
|
|
|
|
|
|
2018-03-28 10:02:39 +00:00
|
|
|
RCT_EXPORT_METHOD(sendMessage:(NSDictionary *) message
|
2018-02-05 15:16:07 +00:00
|
|
|
resolve:(RCTPromiseResolveBlock) resolve
|
|
|
|
reject:(RCTPromiseRejectBlock) reject) {
|
|
|
|
if (!message[@"to"]) {
|
|
|
|
reject(@"messaging/invalid-message", @"The supplied message is missing a 'to' field", nil);
|
|
|
|
}
|
|
|
|
NSString *to = message[@"to"];
|
|
|
|
NSString *messageId = message[@"messageId"];
|
|
|
|
NSNumber *ttl = message[@"ttl"];
|
|
|
|
NSDictionary *data = message[@"data"];
|
2018-02-09 09:23:03 +00:00
|
|
|
|
2018-02-05 15:16:07 +00:00
|
|
|
[[FIRMessaging messaging] sendMessage:data to:to withMessageID:messageId timeToLive:[ttl intValue]];
|
2018-02-23 16:14:19 +00:00
|
|
|
|
|
|
|
// TODO: Listen for send success / errors
|
|
|
|
resolve(nil);
|
2018-02-05 15:16:07 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 10:02:39 +00:00
|
|
|
RCT_EXPORT_METHOD(subscribeToTopic:(NSString*) topic
|
|
|
|
resolve:(RCTPromiseResolveBlock) resolve
|
|
|
|
reject:(RCTPromiseRejectBlock) reject) {
|
2018-02-02 08:40:48 +00:00
|
|
|
[[FIRMessaging messaging] subscribeToTopic:topic];
|
2018-03-28 10:02:39 +00:00
|
|
|
resolve(nil);
|
2017-03-21 15:11:07 +00:00
|
|
|
}
|
2017-03-09 15:26:28 +00:00
|
|
|
|
2018-03-28 10:02:39 +00:00
|
|
|
RCT_EXPORT_METHOD(unsubscribeFromTopic: (NSString*) topic
|
|
|
|
resolve:(RCTPromiseResolveBlock) resolve
|
|
|
|
reject:(RCTPromiseRejectBlock) reject) {
|
2018-02-02 08:40:48 +00:00
|
|
|
[[FIRMessaging messaging] unsubscribeFromTopic:topic];
|
2018-03-28 10:02:39 +00:00
|
|
|
resolve(nil);
|
2017-03-21 15:11:07 +00:00
|
|
|
}
|
2018-02-05 09:18:53 +00:00
|
|
|
|
2018-04-13 13:22:17 +00:00
|
|
|
RCT_EXPORT_METHOD(jsInitialised:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
|
|
|
|
jsReady = TRUE;
|
|
|
|
resolve(nil);
|
|
|
|
if (initialToken) {
|
|
|
|
[self sendJSEvent:self name:MESSAGING_TOKEN_REFRESHED body:initialToken];
|
|
|
|
}
|
2018-05-16 16:37:58 +00:00
|
|
|
if (pendingMessages) {
|
|
|
|
for (id message in pendingMessages) {
|
|
|
|
[RNFirebaseUtil sendJSEvent:self name:MESSAGING_MESSAGE_RECEIVED body:message];
|
|
|
|
}
|
2018-05-17 07:27:23 +00:00
|
|
|
pendingMessages = nil;
|
2018-05-16 16:37:58 +00:00
|
|
|
}
|
2018-04-13 13:22:17 +00:00
|
|
|
}
|
|
|
|
|
2018-02-02 17:16:55 +00:00
|
|
|
// ** Start internals **
|
2018-02-05 09:18:53 +00:00
|
|
|
|
2018-04-13 13:22:17 +00:00
|
|
|
// Because of the time delay between the app starting and the bridge being initialised
|
|
|
|
// we catch any events that are received before the JS is ready to receive them
|
|
|
|
- (void)sendJSEvent:(RCTEventEmitter *)emitter name:(NSString *)name body:(id)body {
|
|
|
|
if (emitter.bridge && jsReady) {
|
|
|
|
[RNFirebaseUtil sendJSEvent:emitter name:name body:body];
|
|
|
|
} else {
|
|
|
|
if ([name isEqualToString:MESSAGING_TOKEN_REFRESHED]) {
|
|
|
|
initialToken = body;
|
2018-05-16 16:37:58 +00:00
|
|
|
} else if ([name isEqualToString:MESSAGING_MESSAGE_RECEIVED]) {
|
|
|
|
if (!pendingMessages) {
|
|
|
|
pendingMessages = [[NSMutableArray alloc] init];
|
|
|
|
}
|
|
|
|
[pendingMessages addObject:body];
|
2018-04-13 13:22:17 +00:00
|
|
|
} else {
|
2018-05-16 16:37:58 +00:00
|
|
|
NSLog(@"Received unexpected message type");
|
2018-04-13 13:22:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-22 15:52:24 +00:00
|
|
|
- (NSDictionary*)parseFIRMessagingRemoteMessage:(FIRMessagingRemoteMessage *)remoteMessage {
|
2018-02-02 17:16:55 +00:00
|
|
|
NSDictionary *appData = remoteMessage.appData;
|
2018-02-09 09:23:03 +00:00
|
|
|
|
2018-02-02 17:16:55 +00:00
|
|
|
NSMutableDictionary *message = [[NSMutableDictionary alloc] init];
|
|
|
|
NSMutableDictionary *data = [[NSMutableDictionary alloc] init];
|
|
|
|
for (id k1 in appData) {
|
|
|
|
if ([k1 isEqualToString:@"collapse_key"]) {
|
|
|
|
message[@"collapseKey"] = appData[@"collapse_key"];
|
|
|
|
} else if ([k1 isEqualToString:@"from"]) {
|
|
|
|
message[@"from"] = appData[k1];
|
|
|
|
} else if ([k1 isEqualToString:@"notification"]) {
|
2018-02-22 15:52:24 +00:00
|
|
|
// Ignore for messages
|
2018-02-02 17:16:55 +00:00
|
|
|
} else {
|
|
|
|
// Assume custom data key
|
|
|
|
data[k1] = appData[k1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
message[@"data"] = data;
|
2018-02-09 09:23:03 +00:00
|
|
|
|
2018-02-02 17:16:55 +00:00
|
|
|
return message;
|
|
|
|
}
|
2018-02-09 09:23:03 +00:00
|
|
|
|
2018-02-22 15:52:24 +00:00
|
|
|
- (NSDictionary*)parseUserInfo:(NSDictionary *)userInfo {
|
|
|
|
NSMutableDictionary *message = [[NSMutableDictionary alloc] init];
|
|
|
|
NSMutableDictionary *data = [[NSMutableDictionary alloc] init];
|
|
|
|
|
|
|
|
for (id k1 in userInfo) {
|
|
|
|
if ([k1 isEqualToString:@"aps"]) {
|
|
|
|
// Ignore notification section
|
|
|
|
} else if ([k1 isEqualToString:@"gcm.message_id"]) {
|
|
|
|
message[@"messageId"] = userInfo[k1];
|
|
|
|
} else if ([k1 isEqualToString:@"google.c.a.ts"]) {
|
|
|
|
message[@"sentTime"] = userInfo[k1];
|
|
|
|
} else if ([k1 isEqualToString:@"gcm.n.e"]
|
|
|
|
|| [k1 isEqualToString:@"gcm.notification.sound2"]
|
|
|
|
|| [k1 isEqualToString:@"google.c.a.c_id"]
|
|
|
|
|| [k1 isEqualToString:@"google.c.a.c_l"]
|
|
|
|
|| [k1 isEqualToString:@"google.c.a.e"]
|
|
|
|
|| [k1 isEqualToString:@"google.c.a.udt"]) {
|
|
|
|
// Ignore known keys
|
|
|
|
} else {
|
|
|
|
// Assume custom data
|
|
|
|
data[k1] = userInfo[k1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
message[@"data"] = data;
|
|
|
|
|
|
|
|
return message;
|
|
|
|
}
|
|
|
|
|
2017-05-30 07:26:40 +00:00
|
|
|
- (NSArray<NSString *> *)supportedEvents {
|
2018-02-18 17:09:13 +00:00
|
|
|
return @[MESSAGING_MESSAGE_RECEIVED, MESSAGING_TOKEN_REFRESHED];
|
2017-03-09 15:26:28 +00:00
|
|
|
}
|
|
|
|
|
2017-10-12 08:18:01 +00:00
|
|
|
+ (BOOL)requiresMainQueueSetup
|
|
|
|
{
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
2017-03-09 15:26:28 +00:00
|
|
|
@end
|
2018-02-05 09:18:53 +00:00
|
|
|
|
2017-05-30 12:20:42 +00:00
|
|
|
#else
|
2018-02-05 09:18:53 +00:00
|
|
|
@implementation RNFirebaseMessaging
|
|
|
|
@end
|
2017-05-30 12:20:42 +00:00
|
|
|
#endif
|
2018-02-23 15:34:00 +00:00
|
|
|
|