2015-03-24 12:31:11 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#import "RCTPushNotificationManager.h"
|
|
|
|
|
|
|
|
#import "RCTBridge.h"
|
2015-07-14 16:07:24 +00:00
|
|
|
#import "RCTConvert.h"
|
2015-03-24 12:31:11 +00:00
|
|
|
#import "RCTEventDispatcher.h"
|
2015-06-12 18:05:01 +00:00
|
|
|
#import "RCTUtils.h"
|
2015-03-24 12:31:11 +00:00
|
|
|
|
2015-06-03 21:11:20 +00:00
|
|
|
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_8_0
|
|
|
|
|
|
|
|
#define UIUserNotificationTypeAlert UIRemoteNotificationTypeAlert
|
|
|
|
#define UIUserNotificationTypeBadge UIRemoteNotificationTypeBadge
|
|
|
|
#define UIUserNotificationTypeSound UIRemoteNotificationTypeSound
|
|
|
|
#define UIUserNotificationTypeNone UIRemoteNotificationTypeNone
|
|
|
|
#define UIUserNotificationType UIRemoteNotificationType
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2015-03-24 12:31:11 +00:00
|
|
|
NSString *const RCTRemoteNotificationReceived = @"RemoteNotificationReceived";
|
2015-06-03 21:11:20 +00:00
|
|
|
NSString *const RCTRemoteNotificationsRegistered = @"RemoteNotificationsRegistered";
|
2015-03-24 12:31:11 +00:00
|
|
|
|
2015-07-14 16:07:24 +00:00
|
|
|
@implementation RCTConvert (UILocalNotification)
|
|
|
|
|
|
|
|
+ (UILocalNotification *)UILocalNotification:(id)json
|
|
|
|
{
|
2015-11-14 18:25:00 +00:00
|
|
|
NSDictionary<NSString *, id> *details = [self NSDictionary:json];
|
2015-08-17 14:35:34 +00:00
|
|
|
UILocalNotification *notification = [UILocalNotification new];
|
2015-07-14 16:07:24 +00:00
|
|
|
notification.fireDate = [RCTConvert NSDate:details[@"fireDate"]] ?: [NSDate date];
|
|
|
|
notification.alertBody = [RCTConvert NSString:details[@"alertBody"]];
|
|
|
|
return notification;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2015-03-24 12:31:11 +00:00
|
|
|
@implementation RCTPushNotificationManager
|
|
|
|
|
2015-04-08 12:42:43 +00:00
|
|
|
RCT_EXPORT_MODULE()
|
|
|
|
|
2015-03-24 12:31:11 +00:00
|
|
|
@synthesize bridge = _bridge;
|
|
|
|
|
|
|
|
- (void)dealloc
|
|
|
|
{
|
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
|
|
|
}
|
|
|
|
|
2015-04-30 21:09:55 +00:00
|
|
|
- (void)setBridge:(RCTBridge *)bridge
|
|
|
|
{
|
|
|
|
_bridge = bridge;
|
2015-11-25 11:09:00 +00:00
|
|
|
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
selector:@selector(handleRemoteNotificationReceived:)
|
|
|
|
name:RCTRemoteNotificationReceived
|
|
|
|
object:nil];
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
selector:@selector(handleRemoteNotificationsRegistered:)
|
|
|
|
name:RCTRemoteNotificationsRegistered
|
|
|
|
object:nil];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSDictionary<NSString *, id> *)constantsToExport
|
|
|
|
{
|
|
|
|
NSDictionary<NSString *, id> *initialNotification = [_bridge.launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey] copy];
|
|
|
|
return @{@"initialNotification": RCTNullIfNil(initialNotification)};
|
2015-04-30 21:09:55 +00:00
|
|
|
}
|
|
|
|
|
2016-01-05 10:42:18 +00:00
|
|
|
+ (void)didRegisterUserNotificationSettings:(__unused UIUserNotificationSettings *)notificationSettings
|
2015-03-24 12:31:11 +00:00
|
|
|
{
|
2016-01-05 10:42:18 +00:00
|
|
|
if ([UIApplication instancesRespondToSelector:@selector(registerForRemoteNotifications)]) {
|
|
|
|
[[UIApplication sharedApplication] registerForRemoteNotifications];
|
2015-03-25 00:37:03 +00:00
|
|
|
}
|
2015-03-24 12:31:11 +00:00
|
|
|
}
|
|
|
|
|
2016-01-05 10:42:18 +00:00
|
|
|
+ (void)didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
|
2015-06-03 21:11:20 +00:00
|
|
|
{
|
|
|
|
NSMutableString *hexString = [NSMutableString string];
|
2015-06-15 14:53:45 +00:00
|
|
|
NSUInteger deviceTokenLength = deviceToken.length;
|
|
|
|
const unsigned char *bytes = deviceToken.bytes;
|
|
|
|
for (NSUInteger i = 0; i < deviceTokenLength; i++) {
|
2015-06-03 21:11:20 +00:00
|
|
|
[hexString appendFormat:@"%02x", bytes[i]];
|
|
|
|
}
|
2015-11-14 18:25:00 +00:00
|
|
|
NSDictionary<NSString *, id> *userInfo = @{
|
2015-06-03 21:11:20 +00:00
|
|
|
@"deviceToken" : [hexString copy]
|
|
|
|
};
|
|
|
|
[[NSNotificationCenter defaultCenter] postNotificationName:RCTRemoteNotificationsRegistered
|
|
|
|
object:self
|
|
|
|
userInfo:userInfo];
|
|
|
|
}
|
|
|
|
|
2016-01-05 10:42:18 +00:00
|
|
|
+ (void)didReceiveRemoteNotification:(NSDictionary *)notification
|
2015-03-24 12:31:11 +00:00
|
|
|
{
|
|
|
|
[[NSNotificationCenter defaultCenter] postNotificationName:RCTRemoteNotificationReceived
|
|
|
|
object:self
|
|
|
|
userInfo:notification];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)handleRemoteNotificationReceived:(NSNotification *)notification
|
|
|
|
{
|
|
|
|
[_bridge.eventDispatcher sendDeviceEventWithName:@"remoteNotificationReceived"
|
2015-08-24 10:14:33 +00:00
|
|
|
body:notification.userInfo];
|
2015-03-24 12:31:11 +00:00
|
|
|
}
|
|
|
|
|
2015-06-03 21:11:20 +00:00
|
|
|
- (void)handleRemoteNotificationsRegistered:(NSNotification *)notification
|
|
|
|
{
|
|
|
|
[_bridge.eventDispatcher sendDeviceEventWithName:@"remoteNotificationsRegistered"
|
2015-08-24 10:14:33 +00:00
|
|
|
body:notification.userInfo];
|
2015-06-03 21:11:20 +00:00
|
|
|
}
|
|
|
|
|
2015-03-24 12:31:11 +00:00
|
|
|
/**
|
|
|
|
* Update the application icon badge number on the home screen
|
|
|
|
*/
|
2015-04-08 15:52:48 +00:00
|
|
|
RCT_EXPORT_METHOD(setApplicationIconBadgeNumber:(NSInteger)number)
|
2015-03-24 12:31:11 +00:00
|
|
|
{
|
2015-09-22 17:43:56 +00:00
|
|
|
RCTSharedApplication().applicationIconBadgeNumber = number;
|
2015-03-24 12:31:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the current application icon badge number on the home screen
|
|
|
|
*/
|
2015-04-08 15:52:48 +00:00
|
|
|
RCT_EXPORT_METHOD(getApplicationIconBadgeNumber:(RCTResponseSenderBlock)callback)
|
2015-03-24 12:31:11 +00:00
|
|
|
{
|
|
|
|
callback(@[
|
2015-09-22 17:43:56 +00:00
|
|
|
@(RCTSharedApplication().applicationIconBadgeNumber)
|
2015-03-24 12:31:11 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2015-06-03 21:11:20 +00:00
|
|
|
RCT_EXPORT_METHOD(requestPermissions:(NSDictionary *)permissions)
|
2015-03-24 12:31:11 +00:00
|
|
|
{
|
2015-09-22 17:43:56 +00:00
|
|
|
if (RCTRunningInAppExtension()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-06-09 22:42:10 +00:00
|
|
|
UIUserNotificationType types = UIUserNotificationTypeNone;
|
2015-06-03 21:11:20 +00:00
|
|
|
if (permissions) {
|
|
|
|
if ([permissions[@"alert"] boolValue]) {
|
|
|
|
types |= UIUserNotificationTypeAlert;
|
|
|
|
}
|
|
|
|
if ([permissions[@"badge"] boolValue]) {
|
|
|
|
types |= UIUserNotificationTypeBadge;
|
|
|
|
}
|
|
|
|
if ([permissions[@"sound"] boolValue]) {
|
|
|
|
types |= UIUserNotificationTypeSound;
|
|
|
|
}
|
2015-04-10 10:10:04 +00:00
|
|
|
} else {
|
2015-06-03 21:11:20 +00:00
|
|
|
types = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;
|
|
|
|
}
|
2015-04-10 10:10:04 +00:00
|
|
|
|
2015-09-22 17:43:56 +00:00
|
|
|
UIApplication *app = RCTSharedApplication();
|
2015-10-01 01:08:54 +00:00
|
|
|
if ([app respondsToSelector:@selector(registerUserNotificationSettings:)]) {
|
|
|
|
UIUserNotificationSettings *notificationSettings = [UIUserNotificationSettings settingsForTypes:(NSUInteger)types categories:nil];
|
|
|
|
[app registerUserNotificationSettings:notificationSettings];
|
|
|
|
[app registerForRemoteNotifications];
|
|
|
|
} else {
|
|
|
|
[app registerForRemoteNotificationTypes:(NSUInteger)types];
|
|
|
|
}
|
2015-03-24 12:31:11 +00:00
|
|
|
}
|
|
|
|
|
2015-06-15 19:14:23 +00:00
|
|
|
RCT_EXPORT_METHOD(abandonPermissions)
|
|
|
|
{
|
2015-09-22 17:43:56 +00:00
|
|
|
[RCTSharedApplication() unregisterForRemoteNotifications];
|
2015-06-15 19:14:23 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 15:52:48 +00:00
|
|
|
RCT_EXPORT_METHOD(checkPermissions:(RCTResponseSenderBlock)callback)
|
2015-03-24 12:31:11 +00:00
|
|
|
{
|
2015-09-22 17:43:56 +00:00
|
|
|
if (RCTRunningInAppExtension()) {
|
2015-11-14 18:25:00 +00:00
|
|
|
NSDictionary<NSString *, NSNumber *> *permissions = @{@"alert": @NO, @"badge": @NO, @"sound": @NO};
|
2015-09-22 17:43:56 +00:00
|
|
|
callback(@[permissions]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-04-24 15:31:07 +00:00
|
|
|
NSUInteger types = 0;
|
2015-04-10 10:10:04 +00:00
|
|
|
if ([UIApplication instancesRespondToSelector:@selector(currentUserNotificationSettings)]) {
|
2015-09-22 17:43:56 +00:00
|
|
|
types = [RCTSharedApplication() currentUserNotificationSettings].types;
|
2015-04-10 10:10:04 +00:00
|
|
|
} else {
|
|
|
|
|
|
|
|
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_8_0
|
|
|
|
|
2015-09-22 17:43:56 +00:00
|
|
|
types = [RCTSharedApplication() enabledRemoteNotificationTypes];
|
2015-04-10 10:10:04 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-11-14 18:25:00 +00:00
|
|
|
NSMutableDictionary<NSString *, NSNumber *> *permissions = [NSMutableDictionary new];
|
2015-04-10 10:10:04 +00:00
|
|
|
permissions[@"alert"] = @((types & UIUserNotificationTypeAlert) > 0);
|
|
|
|
permissions[@"badge"] = @((types & UIUserNotificationTypeBadge) > 0);
|
|
|
|
permissions[@"sound"] = @((types & UIUserNotificationTypeSound) > 0);
|
2015-03-24 12:31:11 +00:00
|
|
|
|
|
|
|
callback(@[permissions]);
|
|
|
|
}
|
|
|
|
|
2015-07-14 16:07:24 +00:00
|
|
|
RCT_EXPORT_METHOD(presentLocalNotification:(UILocalNotification *)notification)
|
|
|
|
{
|
2015-09-22 17:43:56 +00:00
|
|
|
[RCTSharedApplication() presentLocalNotificationNow:notification];
|
2015-07-14 16:07:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_METHOD(scheduleLocalNotification:(UILocalNotification *)notification)
|
|
|
|
{
|
2015-09-22 17:43:56 +00:00
|
|
|
[RCTSharedApplication() scheduleLocalNotification:notification];
|
2015-07-14 16:07:24 +00:00
|
|
|
}
|
|
|
|
|
2015-10-16 17:39:38 +00:00
|
|
|
RCT_EXPORT_METHOD(cancelAllLocalNotifications)
|
|
|
|
{
|
|
|
|
[RCTSharedApplication() cancelAllLocalNotifications];
|
|
|
|
}
|
|
|
|
|
2015-03-24 12:31:11 +00:00
|
|
|
@end
|