Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PushNotifications] Register for Remote Notifications and Extend AppDelegate #1019

Closed
wants to merge 2 commits into from
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
6 changes: 6 additions & 0 deletions Examples/SampleApp/SampleApp.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
13B07FC11A68108700A75B9A /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 13B07FB71A68108700A75B9A /* main.m */; };
146834051AC3E58100842450 /* libReact.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 146834041AC3E56700842450 /* libReact.a */; };
832341BD1AAA6AB300B99B32 /* libRCTText.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 832341B51AAA6A8300B99B32 /* libRCTText.a */; };
8375F9A91AF06BAB001DA3F7 /* AppDelegate+notification.m in Sources */ = {isa = PBXBuildFile; fileRef = 8375F9A71AF06BAB001DA3F7 /* AppDelegate+notification.m */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -127,6 +128,8 @@
146833FF1AC3E56700842450 /* React.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = React.xcodeproj; path = ../../React/React.xcodeproj; sourceTree = "<group>"; };
78C398B01ACF4ADC00677621 /* RCTLinking.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = RCTLinking.xcodeproj; path = ../../Libraries/LinkingIOS/RCTLinking.xcodeproj; sourceTree = "<group>"; };
832341B01AAA6A8300B99B32 /* RCTText.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = RCTText.xcodeproj; path = ../../Libraries/Text/RCTText.xcodeproj; sourceTree = "<group>"; };
8375F9A71AF06BAB001DA3F7 /* AppDelegate+notification.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "AppDelegate+notification.m"; path = "iOS/AppDelegate+notification.m"; sourceTree = "<group>"; };
8375F9A81AF06BAB001DA3F7 /* AppDelegate+notification.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "AppDelegate+notification.h"; path = "iOS/AppDelegate+notification.h"; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -233,6 +236,8 @@
13B07FAE1A68108700A75B9A /* SampleApp */ = {
isa = PBXGroup;
children = (
8375F9A71AF06BAB001DA3F7 /* AppDelegate+notification.m */,
8375F9A81AF06BAB001DA3F7 /* AppDelegate+notification.h */,
008F07F21AC5B25A0029DE68 /* main.jsbundle */,
13B07FAF1A68108700A75B9A /* AppDelegate.h */,
13B07FB01A68108700A75B9A /* AppDelegate.m */,
Expand Down Expand Up @@ -528,6 +533,7 @@
files = (
13B07FBC1A68108700A75B9A /* AppDelegate.m in Sources */,
13B07FC11A68108700A75B9A /* main.m in Sources */,
8375F9A91AF06BAB001DA3F7 /* AppDelegate+notification.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
23 changes: 23 additions & 0 deletions Examples/SampleApp/iOS/AppDelegate+notification.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* 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.
*/

#if __has_include("RCTPushNotificationManager.h")

#import "AppDelegate.h"

@interface AppDelegate (notification)

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken;
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error;
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo;
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings;

@end

#endif
36 changes: 36 additions & 0 deletions Examples/SampleApp/iOS/AppDelegate+notification.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* 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.
*/

#if __has_include("RCTPushNotificationManager.h")

#import "AppDelegate+notification.h"
#import "RCTPushNotificationManager.h"

@implementation AppDelegate (notification)

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
[[RCTPushNotificationManager class] application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings];
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)notification {
[[RCTPushNotificationManager class] application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)notification];
}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
[[RCTPushNotificationManager class] application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken];
}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
[[RCTPushNotificationManager class] application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error];
}

@end

#endif
16 changes: 15 additions & 1 deletion Libraries/PushNotificationIOS/PushNotificationIOS.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ var _initialNotification = RCTPushNotificationManager &&
RCTPushNotificationManager.initialNotification;

var DEVICE_NOTIF_EVENT = 'remoteNotificationReceived';
var REGISTERED_FOR_REMOTE = 'remoteNotificationRegistered';
var REGISTERED_FOR_REMOTE_ERROR = 'remoteNotificationRegisteredError';

/**
* Handle push notifications for your app, including permission handling and
Expand Down Expand Up @@ -72,8 +74,20 @@ class PushNotificationIOS {
* Requests all notification permissions from iOS, prompting the user's
* dialog box.
*/
static requestPermissions() {
static requestPermissions(callback) {
RCTPushNotificationManager.requestPermissions();
if (callback) {
RCTDeviceEventEmitter.addListener(
REGISTERED_FOR_REMOTE, function(token) {
callback(null, token);
}
);
RCTDeviceEventEmitter.addListener(
REGISTERED_FOR_REMOTE_ERROR, function(error) {
callback(error);
}
)
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

/* Begin PBXBuildFile section */
148699CF1ABD045300480536 /* RCTPushNotificationManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 148699CE1ABD045300480536 /* RCTPushNotificationManager.m */; };
8374243B1AEC409600FD3AAF /* AppDelegate+notification.m in Sources */ = {isa = PBXBuildFile; fileRef = 837424391AEC409600FD3AAF /* AppDelegate+notification.m */; };
/* End PBXBuildFile section */

/* Begin PBXCopyFilesBuildPhase section */
Expand All @@ -26,6 +27,8 @@
134814201AA4EA6300B7C361 /* libRCTPushNotification.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libRCTPushNotification.a; sourceTree = BUILT_PRODUCTS_DIR; };
148699CD1ABD045300480536 /* RCTPushNotificationManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RCTPushNotificationManager.h; sourceTree = "<group>"; };
148699CE1ABD045300480536 /* RCTPushNotificationManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTPushNotificationManager.m; sourceTree = "<group>"; };
837424391AEC409600FD3AAF /* AppDelegate+notification.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "AppDelegate+notification.m"; sourceTree = "<group>"; };
8374243A1AEC409600FD3AAF /* AppDelegate+notification.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "AppDelegate+notification.h"; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand All @@ -50,6 +53,8 @@
58B511D21A9E6C8500147676 = {
isa = PBXGroup;
children = (
837424391AEC409600FD3AAF /* AppDelegate+notification.m */,
8374243A1AEC409600FD3AAF /* AppDelegate+notification.h */,
148699CD1ABD045300480536 /* RCTPushNotificationManager.h */,
148699CE1ABD045300480536 /* RCTPushNotificationManager.m */,
134814211AA4EA7D00B7C361 /* Products */,
Expand Down Expand Up @@ -115,6 +120,7 @@
buildActionMask = 2147483647;
files = (
148699CF1ABD045300480536 /* RCTPushNotificationManager.m in Sources */,
8374243B1AEC409600FD3AAF /* AppDelegate+notification.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
3 changes: 2 additions & 1 deletion Libraries/PushNotificationIOS/RCTPushNotificationManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@

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

+ (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken;
+ (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error;
@end
43 changes: 43 additions & 0 deletions Libraries/PushNotificationIOS/RCTPushNotificationManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#import "RCTEventDispatcher.h"

NSString *const RCTRemoteNotificationReceived = @"RemoteNotificationReceived";
NSString *const RCTRemoteNotificationRegistered = @"RemoteNotificationRegistered";
NSString *const RCTRemoteNotificationRegisteredError = @"RemoteNotificationRegisteredError";

@implementation RCTPushNotificationManager
{
Expand All @@ -36,6 +38,14 @@ - (instancetype)initWithInitialNotification:(NSDictionary *)initialNotification
selector:@selector(handleRemoteNotificationReceived:)
name:RCTRemoteNotificationReceived
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleRemoteNotificationRegistred:)
name:RCTRemoteNotificationRegistered
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleRemoteNotificationRegistredError:)
name:RCTRemoteNotificationRegisteredError
object:nil];
}
return self;
}
Expand All @@ -59,12 +69,45 @@ + (void)application:(UIApplication *)application didReceiveRemoteNotification:(N
userInfo:notification];
}

+ (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
NSDictionary *payload = @{@"deviceToken": deviceToken};
[[NSNotificationCenter defaultCenter] postNotificationName:RCTRemoteNotificationRegistered
object:self
userInfo:payload];
}

+ (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
[[NSNotificationCenter defaultCenter] postNotificationName:RCTRemoteNotificationRegisteredError
object:self
userInfo:error.userInfo];

}

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

- (void) handleRemoteNotificationRegistred:(NSNotification *) notification {
NSDictionary *userInfo = notification.userInfo;
NSData *deviceToken = [userInfo objectForKey:@"deviceToken"];
NSString *deviceTokenStr = [[[[deviceToken description]
stringByReplacingOccurrencesOfString: @"<" withString: @""]
stringByReplacingOccurrencesOfString: @">" withString: @""]
stringByReplacingOccurrencesOfString: @" " withString: @""];
[self.bridge.eventDispatcher sendDeviceEventWithName:@"remoteNotificationRegistered"
body:deviceTokenStr];
}

- (void) handleRemoteNotificationRegistredError:(NSNotification *) notification {
NSDictionary *userInfo = notification.userInfo;
NSString *error = [userInfo objectForKey:@"NSLocalizedDescription"];
[self.bridge.eventDispatcher sendDeviceEventWithName:@"remoteNotificationRegisteredError"
body:error];
}

/**
* Update the application icon badge number on the home screen
*/
Expand Down
4 changes: 0 additions & 4 deletions docs/LinkingLibraries.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,6 @@ JavaScript? If you are just using it in JavaScript, you are good to go!
This step is not necessary for libraries that we ship with React Native with the
exception of `PushNotificationIOS` and `LinkingIOS`.

In the case of the `PushNotificationIOS` for example, you have to call a method
on the library from your `AppDelegate` every time a new push notifiation is
received.

For that we need to know the library's headers. To achieve that you have to go
to your project's file, select `Build Settings` and search for `Header Search
Paths`. There you should include the path to you library (if it has relevant
Expand Down