Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 60 additions & 17 deletions Libraries/PushNotificationIOS/PushNotificationIOS.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var _initialNotification = RCTPushNotificationManager &&
RCTPushNotificationManager.initialNotification;

var DEVICE_NOTIF_EVENT = 'remoteNotificationReceived';
var NOTIF_REGISTER_EVENT = 'remoteNotificationsRegistered';

/**
* Handle push notifications for your app, including permission handling and
Expand Down Expand Up @@ -50,30 +51,72 @@ class PushNotificationIOS {
}

/**
* Attaches a listener to remote notifications while the app is running in the
* foreground or the background.
* Attaches a listener to remote notification events while the app is running
* in the foreground or the background.
*
* The handler will get be invoked with an instance of `PushNotificationIOS`
* Valid events are:
*
* - `notification` : Fired when a remote notification is received. The
* handler will be invoked with an instance of `PushNotificationIOS`.
* - `register`: Fired when the user registers for remote notifications. The
* handler will be invoked with a hex string representing the deviceToken.
*/
static addEventListener(type: string, handler: Function) {
invariant(
type === 'notification',
'PushNotificationIOS only supports `notification` events'
);
_notifHandlers[handler] = RCTDeviceEventEmitter.addListener(
DEVICE_NOTIF_EVENT,
(notifData) => {
handler(new PushNotificationIOS(notifData));
}
type === 'notification' || type === 'register',
'PushNotificationIOS only supports `notification` and `register` events'
);
if (type === 'notification') {
_notifHandlers[handler] = RCTDeviceEventEmitter.addListener(
DEVICE_NOTIF_EVENT,
(notifData) => {
handler(new PushNotificationIOS(notifData));
}
);
} else if (type === 'register') {
_notifHandlers[handler] = RCTDeviceEventEmitter.addListener(
NOTIF_REGISTER_EVENT,
(registrationInfo) => {
handler(registrationInfo.deviceToken);
}
);
}
}

/**
* Requests all notification permissions from iOS, prompting the user's
* dialog box.
* Requests notification permissions from iOS, prompting the user's
* dialog box. By default, it will request all notification permissions, but
* a subset of these can be requested by passing a map of requested
* permissions.
* The following permissions are supported:
*
* - `alert`
* - `badge`
* - `sound`
*
* If a map is provided to the method, only the permissions with truthy values
* will be requested.
*/
static requestPermissions() {
RCTPushNotificationManager.requestPermissions();
static requestPermissions(permissions?: {
alert?: boolean,
badge?: boolean,
sound?: boolean
}) {
var requestedPermissions = {};
if (permissions) {
requestedPermissions = {
alert: !!permissions.alert,
badge: !!permissions.badge,
sound: !!permissions.sound
};
} else {
requestedPermissions = {
alert: true,
badge: true,
sound: true
};
}
RCTPushNotificationManager.requestPermissions(requestedPermissions);
}

/**
Expand All @@ -98,8 +141,8 @@ class PushNotificationIOS {
*/
static removeEventListener(type: string, handler: Function) {
invariant(
type === 'notification',
'PushNotificationIOS only supports `notification` events'
type === 'notification' || type === 'register',
'PushNotificationIOS only supports `notification` and `register` events'
);
if (!_notifHandlers[handler]) {
return;
Expand Down
1 change: 1 addition & 0 deletions Libraries/PushNotificationIOS/RCTPushNotificationManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
@interface RCTPushNotificationManager : NSObject <RCTBridgeModule>

+ (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings;
+ (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken;
+ (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)notification;

@end
67 changes: 53 additions & 14 deletions Libraries/PushNotificationIOS/RCTPushNotificationManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,16 @@
#import "RCTBridge.h"
#import "RCTEventDispatcher.h"

#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_8_0

#define UIUserNotificationTypeAlert UIRemoteNotificationTypeAlert
#define UIUserNotificationTypeBadge UIRemoteNotificationTypeBadge
#define UIUserNotificationTypeSound UIRemoteNotificationTypeSound

#endif

NSString *const RCTRemoteNotificationReceived = @"RemoteNotificationReceived";
NSString *const RCTRemoteNotificationsRegistered = @"RemoteNotificationsRegistered";

@implementation RCTPushNotificationManager
{
Expand All @@ -30,6 +39,10 @@ - (instancetype)init
selector:@selector(handleRemoteNotificationReceived:)
name:RCTRemoteNotificationReceived
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleRemoteNotificationsRegistered:)
name:RCTRemoteNotificationsRegistered
object:nil];
}
return self;
}
Expand All @@ -52,6 +65,21 @@ + (void)application:(UIApplication *)application didRegisterUserNotificationSett
}
}

+ (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I trying to use this fork, this line seems to be never called. Did I miss anything?

{
NSMutableString *hexString = [NSMutableString string];
const unsigned char *bytes = [deviceToken bytes];
for (int i = 0; i < [deviceToken length]; i++) {
[hexString appendFormat:@"%02x", bytes[i]];
}
NSDictionary *userInfo = @{
@"deviceToken" : [hexString copy]
};
[[NSNotificationCenter defaultCenter] postNotificationName:RCTRemoteNotificationsRegistered
object:self
userInfo:userInfo];
}

+ (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)notification
{
[[NSNotificationCenter defaultCenter] postNotificationName:RCTRemoteNotificationReceived
Expand All @@ -65,6 +93,12 @@ - (void)handleRemoteNotificationReceived:(NSNotification *)notification
body:[notification userInfo]];
}

- (void)handleRemoteNotificationsRegistered:(NSNotification *)notification
{
[_bridge.eventDispatcher sendDeviceEventWithName:@"remoteNotificationsRegistered"
body:[notification userInfo]];
}

/**
* Update the application icon badge number on the home screen
*/
Expand All @@ -83,19 +117,33 @@ - (void)handleRemoteNotificationReceived:(NSNotification *)notification
]);
}

RCT_EXPORT_METHOD(requestPermissions)
RCT_EXPORT_METHOD(requestPermissions:(NSDictionary *)permissions)
{
UIUserNotificationType types = UIUserNotificationTypeNone;
if (permissions) {
if ([permissions[@"alert"] boolValue]) {
types |= UIUserNotificationTypeAlert;
}
if ([permissions[@"badge"] boolValue]) {
types |= UIUserNotificationTypeBadge;
}
if ([permissions[@"sound"] boolValue]) {
types |= UIUserNotificationTypeSound;
}
} else {
types = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;
}
Class _UIUserNotificationSettings;
if ((_UIUserNotificationSettings = NSClassFromString(@"UIUserNotificationSettings"))) {
UIUserNotificationType types = UIUserNotificationTypeSound | UIUserNotificationTypeBadge | UIUserNotificationTypeAlert;
UIUserNotificationSettings *notificationSettings = [_UIUserNotificationSettings settingsForTypes:types categories:nil];
id notificationSettings = [_UIUserNotificationSettings settingsForTypes:types categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];

[[UIApplication sharedApplication] registerForRemoteNotifications];
} else {

#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_8_0

[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert];
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:types];

#endif

Expand All @@ -104,15 +152,6 @@ - (void)handleRemoteNotificationReceived:(NSNotification *)notification

RCT_EXPORT_METHOD(checkPermissions:(RCTResponseSenderBlock)callback)
{

#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_8_0

#define UIUserNotificationTypeAlert UIRemoteNotificationTypeAlert
#define UIUserNotificationTypeBadge UIRemoteNotificationTypeBadge
#define UIUserNotificationTypeSound UIRemoteNotificationTypeSound

#endif

NSUInteger types = 0;
if ([UIApplication instancesRespondToSelector:@selector(currentUserNotificationSettings)]) {
types = [[[UIApplication sharedApplication] currentUserNotificationSettings] types];
Expand Down