mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-28 20:25:33 +08:00
Add an event for remote notification registration, and improve permissions request
Summary: In order to add Push support to the Parse JS SDK in React Native, we need a way to receive the APNS device token from the JS context. This adds another event to PushNotificationIOS, so that code can respond to a successful registration. Additionally, I've updated the `requestPermissions` call to accept an optional map of parameters. This way, developers can request a subset of user notification types. Closes https://github.com/facebook/react-native/pull/1304 Github Author: Andrew Imm <andrewi@fb.com> Test Plan: Imported from GitHub, without a `Test Plan:` line.
This commit is contained in:
@@ -12,7 +12,18 @@
|
||||
#import "RCTBridge.h"
|
||||
#import "RCTEventDispatcher.h"
|
||||
|
||||
#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
|
||||
|
||||
NSString *const RCTRemoteNotificationReceived = @"RemoteNotificationReceived";
|
||||
NSString *const RCTRemoteNotificationsRegistered = @"RemoteNotificationsRegistered";
|
||||
|
||||
@implementation RCTPushNotificationManager
|
||||
{
|
||||
@@ -30,6 +41,10 @@ RCT_EXPORT_MODULE()
|
||||
selector:@selector(handleRemoteNotificationReceived:)
|
||||
name:RCTRemoteNotificationReceived
|
||||
object:nil];
|
||||
[[NSNotificationCenter defaultCenter] addObserver:self
|
||||
selector:@selector(handleRemoteNotificationsRegistered:)
|
||||
name:RCTRemoteNotificationsRegistered
|
||||
object:nil];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
@@ -52,6 +67,21 @@ RCT_EXPORT_MODULE()
|
||||
}
|
||||
}
|
||||
|
||||
+ (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
|
||||
{
|
||||
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
|
||||
@@ -65,6 +95,12 @@ RCT_EXPORT_MODULE()
|
||||
body:[notification userInfo]];
|
||||
}
|
||||
|
||||
- (void)handleRemoteNotificationsRegistered:(NSNotification *)notification
|
||||
{
|
||||
[_bridge.eventDispatcher sendDeviceEventWithName:@"remoteNotificationsRegistered"
|
||||
body:[notification userInfo]];
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the application icon badge number on the home screen
|
||||
*/
|
||||
@@ -83,36 +119,35 @@ RCT_EXPORT_METHOD(getApplicationIconBadgeNumber:(RCTResponseSenderBlock)callback
|
||||
]);
|
||||
}
|
||||
|
||||
RCT_EXPORT_METHOD(requestPermissions)
|
||||
RCT_EXPORT_METHOD(requestPermissions:(NSDictionary *)permissions)
|
||||
{
|
||||
Class _UIUserNotificationSettings;
|
||||
if ((_UIUserNotificationSettings = NSClassFromString(@"UIUserNotificationSettings"))) {
|
||||
UIUserNotificationType types = UIUserNotificationTypeSound | UIUserNotificationTypeBadge | UIUserNotificationTypeAlert;
|
||||
UIUserNotificationSettings *notificationSettings = [_UIUserNotificationSettings settingsForTypes:types categories:nil];
|
||||
[[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
|
||||
UIUserNotificationType types = UIRemoteNotificationTypeNone;
|
||||
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;
|
||||
}
|
||||
|
||||
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_8_0
|
||||
|
||||
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
|
||||
UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert];
|
||||
|
||||
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_8_0
|
||||
id notificationSettings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
|
||||
[[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
|
||||
[[UIApplication sharedApplication] registerForRemoteNotifications];
|
||||
#else
|
||||
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:types];
|
||||
#endif
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
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];
|
||||
|
||||
Reference in New Issue
Block a user