2017-03-09 15:26:28 +00:00
|
|
|
#import "RNFirebaseMessaging.h"
|
2017-05-31 08:54:02 +00:00
|
|
|
|
|
|
|
@import UserNotifications;
|
|
|
|
#if __has_include(<FirebaseMessaging/FirebaseMessaging.h>)
|
2017-05-30 07:26:40 +00:00
|
|
|
#import "RNFirebaseEvents.h"
|
2017-05-31 08:54:02 +00:00
|
|
|
#import <FirebaseMessaging/FirebaseMessaging.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>
|
|
|
|
|
|
|
|
@implementation RCTConvert (NSCalendarUnit)
|
|
|
|
|
|
|
|
RCT_ENUM_CONVERTER(NSCalendarUnit,
|
|
|
|
(@{
|
|
|
|
@"year": @(NSCalendarUnitYear),
|
|
|
|
@"month": @(NSCalendarUnitMonth),
|
|
|
|
@"week": @(NSCalendarUnitWeekOfYear),
|
|
|
|
@"day": @(NSCalendarUnitDay),
|
|
|
|
@"hour": @(NSCalendarUnitHour),
|
|
|
|
@"minute": @(NSCalendarUnitMinute)
|
|
|
|
}),
|
|
|
|
0,
|
|
|
|
integerValue)
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
@implementation RCTConvert (UNNotificationRequest)
|
|
|
|
|
|
|
|
+ (UNNotificationRequest *)UNNotificationRequest:(id)json
|
|
|
|
{
|
|
|
|
NSDictionary<NSString *, id> *details = [self NSDictionary:json];
|
|
|
|
UNMutableNotificationContent *content = [UNMutableNotificationContent new];
|
|
|
|
content.title =[RCTConvert NSString:details[@"title"]];
|
|
|
|
content.body =[RCTConvert NSString:details[@"body"]];
|
|
|
|
NSString* sound = [RCTConvert NSString:details[@"sound"]];
|
|
|
|
if(sound != nil){
|
|
|
|
content.sound = [UNNotificationSound soundNamed:sound];
|
|
|
|
}else{
|
|
|
|
content.sound = [UNNotificationSound defaultSound];
|
|
|
|
}
|
|
|
|
content.categoryIdentifier = [RCTConvert NSString:details[@"click_action"]];
|
|
|
|
content.userInfo = details;
|
|
|
|
content.badge = [RCTConvert NSNumber:details[@"badge"]];
|
2017-05-05 08:46:22 +00:00
|
|
|
|
2017-03-21 15:11:07 +00:00
|
|
|
NSDate *fireDate = [RCTConvert NSDate:details[@"fire_date"]];
|
2017-05-05 08:46:22 +00:00
|
|
|
|
2017-03-21 15:11:07 +00:00
|
|
|
if(fireDate == nil){
|
|
|
|
return [UNNotificationRequest requestWithIdentifier:[RCTConvert NSString:details[@"id"]] content:content trigger:nil];
|
|
|
|
}
|
2017-05-05 08:46:22 +00:00
|
|
|
|
2017-03-21 15:11:07 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
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
|
|
|
|
|
2017-03-09 15:26:28 +00:00
|
|
|
@implementation RCTConvert (UILocalNotification)
|
|
|
|
|
|
|
|
+ (UILocalNotification *)UILocalNotification:(id)json
|
|
|
|
{
|
2017-03-15 12:57:36 +00:00
|
|
|
NSDictionary<NSString *, id> *details = [self NSDictionary:json];
|
|
|
|
UILocalNotification *notification = [UILocalNotification new];
|
2017-03-21 15:11:07 +00:00
|
|
|
notification.fireDate = [RCTConvert NSDate:details[@"fire_date"]] ?: [NSDate date];
|
|
|
|
if([notification respondsToSelector:@selector(setAlertTitle:)]){
|
|
|
|
[notification setAlertTitle:[RCTConvert NSString:details[@"title"]]];
|
2017-03-15 12:57:36 +00:00
|
|
|
}
|
2017-03-21 15:11:07 +00:00
|
|
|
notification.alertBody = [RCTConvert NSString:details[@"body"]];
|
|
|
|
notification.alertAction = [RCTConvert NSString:details[@"alert_action"]];
|
|
|
|
notification.soundName = [RCTConvert NSString:details[@"sound"]] ?: UILocalNotificationDefaultSoundName;
|
|
|
|
notification.userInfo = details;
|
|
|
|
notification.category = [RCTConvert NSString:details[@"click_action"]];
|
|
|
|
notification.repeatInterval = [RCTConvert NSCalendarUnit:details[@"repeat_interval"]];
|
|
|
|
notification.applicationIconBadgeNumber = [RCTConvert NSInteger:details[@"badge"]];
|
2017-03-15 12:57:36 +00:00
|
|
|
return notification;
|
2017-03-09 15:26:28 +00:00
|
|
|
}
|
|
|
|
|
2017-03-21 15:11:07 +00:00
|
|
|
RCT_ENUM_CONVERTER(UIBackgroundFetchResult, (@{
|
|
|
|
@"UIBackgroundFetchResultNewData": @(UIBackgroundFetchResultNewData),
|
|
|
|
@"UIBackgroundFetchResultNoData": @(UIBackgroundFetchResultNoData),
|
|
|
|
@"UIBackgroundFetchResultFailed": @(UIBackgroundFetchResultFailed),
|
|
|
|
}), UIBackgroundFetchResultNoData, integerValue)
|
|
|
|
|
|
|
|
RCT_ENUM_CONVERTER(UNNotificationPresentationOptions, (@{
|
|
|
|
@"UNNotificationPresentationOptionAll": @(UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound),
|
|
|
|
@"UNNotificationPresentationOptionNone": @(UNNotificationPresentationOptionNone)}), UIBackgroundFetchResultNoData, integerValue)
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@interface RNFirebaseMessaging ()
|
|
|
|
@property (nonatomic, strong) NSMutableDictionary *notificationCallbacks;
|
2017-03-09 15:26:28 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation RNFirebaseMessaging
|
|
|
|
|
2017-03-21 15:11:07 +00:00
|
|
|
RCT_EXPORT_MODULE()
|
|
|
|
|
2017-05-30 07:26:40 +00:00
|
|
|
+ (void)didReceiveRemoteNotification:(nonnull NSDictionary *)userInfo {
|
|
|
|
NSMutableDictionary* data = [[NSMutableDictionary alloc] initWithDictionary: userInfo];
|
|
|
|
[data setValue:@"remote_notification" forKey:@"_notificationType"];
|
|
|
|
[data setValue:@(RCTSharedApplication().applicationState == UIApplicationStateInactive) forKey:@"opened_from_tray"];
|
|
|
|
[[NSNotificationCenter defaultCenter] postNotificationName:MESSAGING_NOTIFICATION_RECEIVED object:self userInfo:@{@"data": data}];
|
|
|
|
}
|
2017-03-21 15:11:07 +00:00
|
|
|
|
|
|
|
+ (void)didReceiveRemoteNotification:(nonnull NSDictionary *)userInfo fetchCompletionHandler:(nonnull RCTRemoteNotificationCallback)completionHandler {
|
|
|
|
NSMutableDictionary* data = [[NSMutableDictionary alloc] initWithDictionary: userInfo];
|
|
|
|
[data setValue:@"remote_notification" forKey:@"_notificationType"];
|
|
|
|
[data setValue:@(RCTSharedApplication().applicationState == UIApplicationStateInactive) forKey:@"opened_from_tray"];
|
2017-05-30 07:26:40 +00:00
|
|
|
[[NSNotificationCenter defaultCenter] postNotificationName:MESSAGING_NOTIFICATION_RECEIVED object:self userInfo:@{@"data": data, @"completionHandler": completionHandler}];
|
2017-03-21 15:11:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
+ (void)didReceiveLocalNotification:(UILocalNotification *)notification {
|
|
|
|
NSMutableDictionary* data = [[NSMutableDictionary alloc] initWithDictionary: notification.userInfo];
|
|
|
|
[data setValue:@"local_notification" forKey:@"_notificationType"];
|
2017-05-30 07:26:40 +00:00
|
|
|
[[NSNotificationCenter defaultCenter] postNotificationName:MESSAGING_NOTIFICATION_RECEIVED object:self userInfo:@{@"data": data}];
|
2017-03-21 15:11:07 +00:00
|
|
|
}
|
|
|
|
|
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");
|
2017-05-30 07:26:40 +00:00
|
|
|
[self initialiseMessaging];
|
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
|
|
|
}
|
|
|
|
|
2017-05-30 07:26:40 +00:00
|
|
|
- (void)initialiseMessaging {
|
|
|
|
// Establish Firebase managed data channel
|
|
|
|
[FIRMessaging messaging].shouldEstablishDirectChannel = YES;
|
2017-05-05 08:46:22 +00:00
|
|
|
|
2017-05-30 07:26:40 +00:00
|
|
|
// Set up listeners for data messages
|
2017-03-15 12:57:36 +00:00
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
2017-05-30 07:26:40 +00:00
|
|
|
selector:@selector(sendDataMessageFailure:)
|
|
|
|
name:FIRMessagingSendErrorNotification
|
2017-03-21 15:11:07 +00:00
|
|
|
object:nil];
|
2017-03-15 12:57:36 +00:00
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
2017-05-30 07:26:40 +00:00
|
|
|
selector:@selector(sendDataMessageSuccess:)
|
|
|
|
name:FIRMessagingSendSuccessNotification
|
2017-03-21 15:11:07 +00:00
|
|
|
object:nil];
|
2017-05-30 07:26:40 +00:00
|
|
|
|
|
|
|
// Set up internal listener to send notification over bridge
|
2017-03-15 12:57:36 +00:00
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
2017-05-30 07:26:40 +00:00
|
|
|
selector:@selector(handleNotificationReceived:)
|
|
|
|
name:MESSAGING_NOTIFICATION_RECEIVED
|
2017-03-21 15:11:07 +00:00
|
|
|
object:nil];
|
2017-05-05 08:46:22 +00:00
|
|
|
|
2017-05-30 07:26:40 +00:00
|
|
|
// Set this as a delegate for FIRMessaging
|
2017-03-21 15:11:07 +00:00
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
2017-05-30 07:26:40 +00:00
|
|
|
[FIRMessaging messaging].delegate = self;
|
2017-03-21 15:11:07 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-05-30 07:26:40 +00:00
|
|
|
- (void)dealloc {
|
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)handleNotificationReceived:(NSNotification *)notification {
|
|
|
|
id completionHandler = notification.userInfo[@"completionHandler"];
|
|
|
|
NSMutableDictionary* data = notification.userInfo[@"data"];
|
|
|
|
if(completionHandler != nil){
|
|
|
|
NSString *completionHandlerId = [[NSUUID UUID] UUIDString];
|
|
|
|
if (!self.notificationCallbacks) {
|
|
|
|
// Lazy initialization
|
|
|
|
self.notificationCallbacks = [NSMutableDictionary dictionary];
|
2017-03-21 15:11:07 +00:00
|
|
|
}
|
2017-05-30 07:26:40 +00:00
|
|
|
self.notificationCallbacks[completionHandlerId] = completionHandler;
|
|
|
|
data[@"_completionHandlerId"] = completionHandlerId;
|
|
|
|
}
|
|
|
|
|
|
|
|
[self sendEventWithName:MESSAGING_NOTIFICATION_RECEIVED body:data];
|
2017-03-21 15:11:07 +00:00
|
|
|
}
|
|
|
|
|
2017-05-30 07:26:40 +00:00
|
|
|
|
|
|
|
- (void)sendDataMessageFailure:(NSNotification *)notification {
|
|
|
|
NSString *messageID = (NSString *)notification.userInfo[@"messageID"];
|
|
|
|
NSLog(@"sendDataMessageFailure: %@", messageID);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)sendDataMessageSuccess:(NSNotification *)notification {
|
|
|
|
NSString *messageID = (NSString *)notification.userInfo[@"messageID"];
|
|
|
|
NSLog(@"sendDataMessageSuccess: %@", messageID);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ** Start FIRMessagingDelegate methods **
|
|
|
|
// Handle data messages in the background
|
|
|
|
- (void)applicationReceivedRemoteMessage:(FIRMessagingRemoteMessage *)remoteMessage {
|
|
|
|
[self sendEventWithName:MESSAGING_NOTIFICATION_RECEIVED body:[remoteMessage appData]];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Listen for token refreshes
|
|
|
|
- (void)messaging:(nonnull FIRMessaging *)messaging didRefreshRegistrationToken:(nonnull NSString *)fcmToken {
|
|
|
|
NSLog(@"FCM registration token: %@", fcmToken);
|
|
|
|
[self sendEventWithName:MESSAGING_TOKEN_REFRESHED body:fcmToken];
|
2017-03-21 15:11:07 +00:00
|
|
|
}
|
2017-05-30 07:26:40 +00:00
|
|
|
// ** End FIRMessagingDelegate methods **
|
2017-03-21 15:11:07 +00:00
|
|
|
|
2017-05-30 07:26:40 +00:00
|
|
|
// ** Start React Module methods **
|
2017-03-21 15:11:07 +00:00
|
|
|
RCT_EXPORT_METHOD(getInitialNotification:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject){
|
2017-05-30 07:26:40 +00:00
|
|
|
UILocalNotification *localUserInfo = [self bridge].launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];
|
2017-03-21 15:11:07 +00:00
|
|
|
if (localUserInfo) {
|
|
|
|
resolve([[localUserInfo userInfo] copy]);
|
2017-05-30 07:26:40 +00:00
|
|
|
} else {
|
|
|
|
resolve([[self bridge].launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey] copy]);
|
2017-03-21 15:11:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-30 07:26:40 +00:00
|
|
|
RCT_EXPORT_METHOD(getToken:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
|
|
|
|
resolve([FIRMessaging messaging].FCMToken);
|
2017-03-21 15:11:07 +00:00
|
|
|
}
|
|
|
|
|
2017-05-30 07:26:40 +00:00
|
|
|
RCT_EXPORT_METHOD(requestPermissions:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
|
2017-03-21 15:11:07 +00:00
|
|
|
if (RCTRunningInAppExtension()) {
|
|
|
|
return;
|
|
|
|
}
|
2017-05-30 07:26:40 +00:00
|
|
|
|
2017-03-21 15:11:07 +00:00
|
|
|
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_9_x_Max) {
|
2017-03-15 12:57:36 +00:00
|
|
|
UIUserNotificationType allNotificationTypes =
|
|
|
|
(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);
|
2017-05-30 07:26:40 +00:00
|
|
|
UIUserNotificationSettings *settings =
|
|
|
|
[UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil];
|
|
|
|
[RCTSharedApplication() registerUserNotificationSettings:settings];
|
2017-03-27 11:51:52 +00:00
|
|
|
// Unfortunately on iOS 9 or below, there's no way to tell whether the user accepted or
|
|
|
|
// rejected the permissions popup
|
|
|
|
resolve(@{@"status":@"unknown"});
|
2017-03-15 12:57:36 +00:00
|
|
|
} else {
|
|
|
|
// iOS 10 or later
|
|
|
|
#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
|
2017-05-30 07:26:40 +00:00
|
|
|
// For iOS 10 display notification (sent via APNS)
|
|
|
|
[UNUserNotificationCenter currentNotificationCenter].delegate = self;
|
2017-03-15 12:57:36 +00:00
|
|
|
UNAuthorizationOptions authOptions =
|
|
|
|
UNAuthorizationOptionAlert
|
|
|
|
| UNAuthorizationOptionSound
|
|
|
|
| UNAuthorizationOptionBadge;
|
2017-05-30 07:26:40 +00:00
|
|
|
[[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:authOptions completionHandler:^(BOOL granted, NSError * _Nullable error) {
|
|
|
|
resolve(@{@"granted":@(granted)});
|
|
|
|
}];
|
2017-03-15 12:57:36 +00:00
|
|
|
#endif
|
|
|
|
}
|
2017-05-05 08:46:22 +00:00
|
|
|
|
2017-05-15 11:00:41 +00:00
|
|
|
[RCTSharedApplication() registerForRemoteNotifications];
|
2017-03-09 15:26:28 +00:00
|
|
|
}
|
|
|
|
|
2017-05-30 07:26:40 +00:00
|
|
|
RCT_EXPORT_METHOD(subscribeToTopic: (NSString*) topic) {
|
2017-03-21 15:11:07 +00:00
|
|
|
[[FIRMessaging messaging] subscribeToTopic:topic];
|
2017-03-09 15:26:28 +00:00
|
|
|
}
|
|
|
|
|
2017-05-30 07:26:40 +00:00
|
|
|
RCT_EXPORT_METHOD(unsubscribeFromTopic: (NSString*) topic) {
|
2017-03-21 15:11:07 +00:00
|
|
|
[[FIRMessaging messaging] unsubscribeFromTopic:topic];
|
2017-03-09 15:26:28 +00:00
|
|
|
}
|
|
|
|
|
2017-05-30 07:26:40 +00:00
|
|
|
RCT_EXPORT_METHOD(createLocalNotification:(id)data resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
|
2017-03-21 15:11:07 +00:00
|
|
|
if([UNUserNotificationCenter currentNotificationCenter] != nil){
|
|
|
|
UNNotificationRequest* request = [RCTConvert UNNotificationRequest:data];
|
|
|
|
[[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable 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);
|
|
|
|
}
|
2017-03-09 15:26:28 +00:00
|
|
|
}
|
|
|
|
|
2017-05-30 07:26:40 +00:00
|
|
|
RCT_EXPORT_METHOD(scheduleLocalNotification:(id)data resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
|
2017-03-21 15:11:07 +00:00
|
|
|
if([UNUserNotificationCenter currentNotificationCenter] != nil){
|
|
|
|
UNNotificationRequest* request = [RCTConvert UNNotificationRequest:data];
|
|
|
|
[[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable 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);
|
|
|
|
}
|
2017-03-09 15:26:28 +00:00
|
|
|
}
|
|
|
|
|
2017-05-30 07:26:40 +00:00
|
|
|
RCT_EXPORT_METHOD(removeDeliveredNotification:(NSString*) notificationId) {
|
2017-03-21 15:11:07 +00:00
|
|
|
if([UNUserNotificationCenter currentNotificationCenter] != nil){
|
|
|
|
[[UNUserNotificationCenter currentNotificationCenter] removeDeliveredNotificationsWithIdentifiers:@[notificationId]];
|
|
|
|
}
|
2017-03-09 15:26:28 +00:00
|
|
|
}
|
2017-03-21 15:11:07 +00:00
|
|
|
|
2017-05-30 07:26:40 +00:00
|
|
|
RCT_EXPORT_METHOD(removeAllDeliveredNotifications) {
|
2017-03-21 15:11:07 +00:00
|
|
|
if([UNUserNotificationCenter currentNotificationCenter] != nil){
|
|
|
|
[[UNUserNotificationCenter currentNotificationCenter] removeAllDeliveredNotifications];
|
|
|
|
} else {
|
|
|
|
[RCTSharedApplication() setApplicationIconBadgeNumber: 0];
|
|
|
|
}
|
2017-03-09 15:26:28 +00:00
|
|
|
}
|
|
|
|
|
2017-05-30 07:26:40 +00:00
|
|
|
RCT_EXPORT_METHOD(cancelAllLocalNotifications) {
|
2017-03-21 15:11:07 +00:00
|
|
|
if([UNUserNotificationCenter currentNotificationCenter] != nil){
|
|
|
|
[[UNUserNotificationCenter currentNotificationCenter] removeAllPendingNotificationRequests];
|
|
|
|
} else {
|
|
|
|
[RCTSharedApplication() cancelAllLocalNotifications];
|
|
|
|
}
|
2017-03-09 15:26:28 +00:00
|
|
|
}
|
|
|
|
|
2017-05-30 07:26:40 +00:00
|
|
|
RCT_EXPORT_METHOD(cancelLocalNotification:(NSString*) notificationId) {
|
2017-03-21 15:11:07 +00:00
|
|
|
if([UNUserNotificationCenter currentNotificationCenter] != nil){
|
|
|
|
[[UNUserNotificationCenter currentNotificationCenter] removePendingNotificationRequestsWithIdentifiers:@[notificationId]];
|
2017-05-30 07:26:40 +00:00
|
|
|
} else {
|
2017-05-15 11:00:41 +00:00
|
|
|
for (UILocalNotification *notification in RCTSharedApplication().scheduledLocalNotifications) {
|
2017-03-21 15:11:07 +00:00
|
|
|
NSDictionary<NSString *, id> *notificationInfo = notification.userInfo;
|
|
|
|
if([notificationId isEqualToString:[notificationInfo valueForKey:@"id"]]){
|
2017-05-15 11:00:41 +00:00
|
|
|
[RCTSharedApplication() cancelLocalNotification:notification];
|
2017-03-15 12:57:36 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-09 15:26:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-30 07:26:40 +00:00
|
|
|
RCT_EXPORT_METHOD(getScheduledLocalNotifications:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
|
2017-03-21 15:11:07 +00:00
|
|
|
if([UNUserNotificationCenter currentNotificationCenter] != nil){
|
|
|
|
[[UNUserNotificationCenter currentNotificationCenter] getPendingNotificationRequestsWithCompletionHandler:^(NSArray<UNNotificationRequest *> * _Nonnull requests) {
|
|
|
|
NSMutableArray* list = [[NSMutableArray alloc] init];
|
|
|
|
for(UNNotificationRequest * notif in requests){
|
|
|
|
UNMutableNotificationContent *content = notif.content;
|
|
|
|
[list addObject:content.userInfo];
|
|
|
|
}
|
|
|
|
resolve(list);
|
|
|
|
}];
|
2017-05-30 07:26:40 +00:00
|
|
|
} else {
|
2017-03-21 15:11:07 +00:00
|
|
|
NSMutableArray* list = [[NSMutableArray alloc] init];
|
|
|
|
for(UILocalNotification * notif in [RCTSharedApplication() scheduledLocalNotifications]){
|
|
|
|
[list addObject:notif.userInfo];
|
|
|
|
}
|
|
|
|
resolve(list);
|
|
|
|
}
|
2017-03-09 15:26:28 +00:00
|
|
|
}
|
|
|
|
|
2017-05-30 07:26:40 +00:00
|
|
|
RCT_EXPORT_METHOD(setBadgeNumber: (NSInteger*) number) {
|
2017-03-21 15:11:07 +00:00
|
|
|
[RCTSharedApplication() setApplicationIconBadgeNumber:number];
|
2017-03-09 15:26:28 +00:00
|
|
|
}
|
|
|
|
|
2017-05-30 07:26:40 +00:00
|
|
|
RCT_EXPORT_METHOD(getBadgeNumber: (RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
|
2017-03-21 15:11:07 +00:00
|
|
|
resolve(@([RCTSharedApplication() applicationIconBadgeNumber]));
|
2017-03-09 15:26:28 +00:00
|
|
|
}
|
|
|
|
|
2017-03-30 15:25:27 +00:00
|
|
|
RCT_EXPORT_METHOD(send:(NSDictionary *)remoteMessage) {
|
|
|
|
int64_t ttl = @([[remoteMessage valueForKey:@"ttl"] intValue]).doubleValue;
|
2017-04-03 09:33:18 +00:00
|
|
|
NSString * mId = [remoteMessage valueForKey:@"id"];
|
|
|
|
NSString * receiver = [remoteMessage valueForKey:@"sender"];
|
|
|
|
NSDictionary * data = [remoteMessage valueForKey:@"data"];
|
2017-05-30 07:26:40 +00:00
|
|
|
[[FIRMessaging messaging] sendMessage:data to:receiver withMessageID:mId timeToLive:ttl];
|
2017-03-09 15:26:28 +00:00
|
|
|
}
|
|
|
|
|
2017-05-30 07:26:40 +00:00
|
|
|
RCT_EXPORT_METHOD(finishRemoteNotification: (NSString *)completionHandlerId fetchResult:(UIBackgroundFetchResult)result) {
|
2017-03-21 15:11:07 +00:00
|
|
|
RCTRemoteNotificationCallback completionHandler = self.notificationCallbacks[completionHandlerId];
|
|
|
|
if (!completionHandler) {
|
|
|
|
RCTLogError(@"There is no completion handler with completionHandlerId: %@", completionHandlerId);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
completionHandler(result);
|
|
|
|
[self.notificationCallbacks removeObjectForKey:completionHandlerId];
|
|
|
|
}
|
2017-03-09 15:26:28 +00:00
|
|
|
|
2017-05-30 07:26:40 +00:00
|
|
|
RCT_EXPORT_METHOD(finishWillPresentNotification: (NSString *)completionHandlerId fetchResult:(UNNotificationPresentationOptions)result) {
|
2017-03-21 15:11:07 +00:00
|
|
|
RCTWillPresentNotificationCallback completionHandler = self.notificationCallbacks[completionHandlerId];
|
|
|
|
if (!completionHandler) {
|
|
|
|
RCTLogError(@"There is no completion handler with completionHandlerId: %@", completionHandlerId);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
completionHandler(result);
|
|
|
|
[self.notificationCallbacks removeObjectForKey:completionHandlerId];
|
|
|
|
}
|
2017-03-09 15:26:28 +00:00
|
|
|
|
2017-05-30 07:26:40 +00:00
|
|
|
RCT_EXPORT_METHOD(finishNotificationResponse: (NSString *)completionHandlerId) {
|
2017-03-21 15:11:07 +00:00
|
|
|
RCTNotificationResponseCallback completionHandler = self.notificationCallbacks[completionHandlerId];
|
|
|
|
if (!completionHandler) {
|
|
|
|
RCTLogError(@"There is no completion handler with completionHandlerId: %@", completionHandlerId);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
completionHandler();
|
|
|
|
[self.notificationCallbacks removeObjectForKey:completionHandlerId];
|
|
|
|
}
|
2017-03-09 15:26:28 +00:00
|
|
|
|
2017-05-30 07:26:40 +00:00
|
|
|
- (NSArray<NSString *> *)supportedEvents {
|
|
|
|
return @[MESSAGING_TOKEN_REFRESHED, MESSAGING_NOTIFICATION_RECEIVED];
|
2017-03-09 15:26:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
2017-05-30 12:20:42 +00:00
|
|
|
|
|
|
|
#else
|
|
|
|
@implementation RNFirebaseMessaging
|
|
|
|
@end
|
|
|
|
#endif
|