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"
|
|
|
|
#import "RCTEventDispatcher.h"
|
|
|
|
|
|
|
|
NSString *const RCTRemoteNotificationReceived = @"RemoteNotificationReceived";
|
|
|
|
|
|
|
|
@implementation RCTPushNotificationManager
|
|
|
|
{
|
|
|
|
NSDictionary *_initialNotification;
|
|
|
|
}
|
|
|
|
|
2015-04-08 12:42:43 +00:00
|
|
|
RCT_EXPORT_MODULE()
|
|
|
|
|
2015-03-24 12:31:11 +00:00
|
|
|
@synthesize bridge = _bridge;
|
|
|
|
|
|
|
|
- (instancetype)init
|
|
|
|
{
|
|
|
|
return [self initWithInitialNotification:nil];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (instancetype)initWithInitialNotification:(NSDictionary *)initialNotification
|
|
|
|
{
|
|
|
|
if ((self = [super init])) {
|
|
|
|
_initialNotification = [initialNotification copy];
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
selector:@selector(handleRemoteNotificationReceived:)
|
|
|
|
name:RCTRemoteNotificationReceived
|
|
|
|
object:nil];
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dealloc
|
|
|
|
{
|
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
|
|
|
|
{
|
2015-03-25 00:37:03 +00:00
|
|
|
if ([application respondsToSelector:@selector(registerForRemoteNotifications)]) {
|
|
|
|
[application registerForRemoteNotifications];
|
|
|
|
}
|
2015-03-24 12:31:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
+ (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)notification
|
|
|
|
{
|
|
|
|
[[NSNotificationCenter defaultCenter] postNotificationName:RCTRemoteNotificationReceived
|
|
|
|
object:self
|
|
|
|
userInfo:notification];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)handleRemoteNotificationReceived:(NSNotification *)notification
|
|
|
|
{
|
|
|
|
[_bridge.eventDispatcher sendDeviceEventWithName:@"remoteNotificationReceived"
|
|
|
|
body:[notification userInfo]];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
{
|
|
|
|
[UIApplication sharedApplication].applicationIconBadgeNumber = number;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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(@[
|
|
|
|
@([UIApplication sharedApplication].applicationIconBadgeNumber)
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2015-04-08 15:52:48 +00:00
|
|
|
RCT_EXPORT_METHOD(requestPermissions)
|
2015-03-24 12:31:11 +00:00
|
|
|
{
|
2015-04-10 10:10:04 +00:00
|
|
|
Class _UIUserNotificationSettings;
|
|
|
|
if ((_UIUserNotificationSettings = NSClassFromString(@"UIUserNotificationSettings"))) {
|
|
|
|
UIUserNotificationType types = UIUserNotificationTypeSound | UIUserNotificationTypeBadge | UIUserNotificationTypeAlert;
|
|
|
|
UIUserNotificationSettings *notificationSettings = [_UIUserNotificationSettings settingsForTypes:types categories:nil];
|
|
|
|
[[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
|
|
|
|
} else {
|
|
|
|
|
2015-03-25 00:37:03 +00:00
|
|
|
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_8_0
|
|
|
|
|
|
|
|
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
|
|
|
|
UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert];
|
|
|
|
|
2015-03-24 12:31:11 +00:00
|
|
|
#endif
|
2015-03-25 00:37:03 +00:00
|
|
|
|
2015-04-10 10:10:04 +00:00
|
|
|
}
|
2015-03-24 12:31:11 +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-03-25 00:37:03 +00:00
|
|
|
|
2015-04-10 10:10:04 +00:00
|
|
|
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_8_0
|
|
|
|
|
|
|
|
#define UIUserNotificationTypeAlert UIRemoteNotificationTypeAlert
|
|
|
|
#define UIUserNotificationTypeBadge UIRemoteNotificationTypeBadge
|
|
|
|
#define UIUserNotificationTypeSound UIRemoteNotificationTypeSound
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
NSUInteger types;
|
|
|
|
if ([UIApplication instancesRespondToSelector:@selector(currentUserNotificationSettings)]) {
|
|
|
|
types = [[[UIApplication sharedApplication] currentUserNotificationSettings] types];
|
|
|
|
} else {
|
|
|
|
|
|
|
|
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_8_0
|
|
|
|
|
|
|
|
types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
NSMutableDictionary *permissions = [[NSMutableDictionary alloc] init];
|
|
|
|
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]);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSDictionary *)constantsToExport
|
|
|
|
{
|
|
|
|
return @{
|
|
|
|
@"initialNotification": _initialNotification ?: [NSNull null]
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|