Commit 13863605 authored by yogevbd's avatar yogevbd

Fix pushKit

parent f0f0b0e0
...@@ -33,7 +33,7 @@ ...@@ -33,7 +33,7 @@
} }
- (void)registerPushKit { - (void)registerPushKit {
[[RNNotifications sharedInstance] initializePushKit]; [RNNotifications startMonitorPushKitNotifications];
} }
- (void)getBadgesCount:(RCTResponseSenderBlock)callback { - (void)getBadgesCount:(RCTResponseSenderBlock)callback {
......
...@@ -5,6 +5,7 @@ static NSString* const RNRegistrationFailed = @"remoteNotificationsR ...@@ -5,6 +5,7 @@ static NSString* const RNRegistrationFailed = @"remoteNotificationsR
static NSString* const RNPushKitRegistered = @"pushKitRegistered"; static NSString* const RNPushKitRegistered = @"pushKitRegistered";
static NSString* const RNNotificationReceivedForeground = @"notificationReceivedForeground"; static NSString* const RNNotificationReceivedForeground = @"notificationReceivedForeground";
static NSString* const RNNotificationOpened = @"notificationOpened"; static NSString* const RNNotificationOpened = @"notificationOpened";
static NSString* const RNPushKitNotificationReceived = @"pushKitNotificationReceived";
@interface RNEventEmitter : RCTEventEmitter <RCTBridgeModule> @interface RNEventEmitter : RCTEventEmitter <RCTBridgeModule>
......
...@@ -9,7 +9,8 @@ RCT_EXPORT_MODULE(); ...@@ -9,7 +9,8 @@ RCT_EXPORT_MODULE();
RNRegistrationFailed, RNRegistrationFailed,
RNPushKitRegistered, RNPushKitRegistered,
RNNotificationReceivedForeground, RNNotificationReceivedForeground,
RNNotificationOpened]; RNNotificationOpened,
RNPushKitNotificationReceived];
} }
- (instancetype)init { - (instancetype)init {
......
...@@ -8,14 +8,14 @@ ...@@ -8,14 +8,14 @@
+ (instancetype)sharedInstance; + (instancetype)sharedInstance;
- (void)initialize; + (void)startMonitorNotifications;
- (void)initializePushKit; + (void)startMonitorPushKitNotifications;
+ (void)didRegisterForRemoteNotificationsWithDeviceToken:(id)deviceToken;
+ (void)didFailToRegisterForRemoteNotificationsWithError:(NSError *)error;
- (void)didRegisterForRemoteNotificationsWithDeviceToken:(id)deviceToken;
- (void)didFailToRegisterForRemoteNotificationsWithError:(NSError *)error;
- (void)finishHandleNotificationKey:(NSString *)notificationKey presentingOptions:(UNNotificationPresentationOptions)presentingOptions; - (void)finishHandleNotificationKey:(NSString *)notificationKey presentingOptions:(UNNotificationPresentationOptions)presentingOptions;
- (void)finishHandleActionKey:(NSString *)actionKey; - (void)finishHandleActionKey:(NSString *)actionKey;
//- (void)setBadgeForNotification:(NSDictionary *)notification;
@end @end
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
- (instancetype)init { - (instancetype)init {
self = [super init]; self = [super init];
_store = [RNNotificationsStore new]; _store = [RNNotificationsStore new];
_notificationEventHandler = [[RNNotificationEventHandler alloc] initWithStore:_store];
return self; return self;
} }
...@@ -31,12 +32,27 @@ ...@@ -31,12 +32,27 @@
return sharedInstance; return sharedInstance;
} }
- (void)initialize { + (void)startMonitorNotifications {
_notificationEventHandler = [[RNNotificationEventHandler alloc] initWithStore:_store]; [[self sharedInstance] startMonitorNotifications];
}
+ (void)startMonitorPushKitNotifications {
[[self sharedInstance] startMonitorPushKitNotifications];
}
+ (void)didRegisterForRemoteNotificationsWithDeviceToken:(id)deviceToken {
[[self sharedInstance] didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}
+ (void)didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
[[self sharedInstance] didFailToRegisterForRemoteNotificationsWithError:error];
}
- (void)startMonitorNotifications {
_notificationCenterListener = [[RNNotificationCenterListener alloc] initWithNotificationEventHandler:_notificationEventHandler]; _notificationCenterListener = [[RNNotificationCenterListener alloc] initWithNotificationEventHandler:_notificationEventHandler];
} }
- (void)initializePushKit { - (void)startMonitorPushKitNotifications {
_pushKitEventHandler = [RNPushKitEventHandler new]; _pushKitEventHandler = [RNPushKitEventHandler new];
_pushKit = [[RNPushKit alloc] initWithEventHandler:_pushKitEventHandler]; _pushKit = [[RNPushKit alloc] initWithEventHandler:_pushKitEventHandler];
} }
...@@ -49,12 +65,6 @@ ...@@ -49,12 +65,6 @@
[_notificationEventHandler didFailToRegisterForRemoteNotificationsWithError:error]; [_notificationEventHandler didFailToRegisterForRemoteNotificationsWithError:error];
} }
- (void)setBadgeForNotification:(NSDictionary *)notification {
if ([[notification objectForKey:@"aps"] objectForKey:@"badge"]){
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:[[[notification objectForKey:@"aps"] objectForKey:@"badge"] intValue]];
}
}
- (void)setInitialNotification:(NSDictionary *)notification { - (void)setInitialNotification:(NSDictionary *)notification {
[_store setInitialNotification:notification]; [_store setInitialNotification:notification];
} }
......
...@@ -5,4 +5,6 @@ ...@@ -5,4 +5,6 @@
- (void)registeredWithToken:(NSString *)token; - (void)registeredWithToken:(NSString *)token;
- (void)didReceiveIncomingPushWithPayload:(NSDictionary *)payload;
@end @end
...@@ -7,4 +7,8 @@ ...@@ -7,4 +7,8 @@
[RNEventEmitter sendEvent:RNPushKitRegistered body:@{@"pushKitToken": token}]; [RNEventEmitter sendEvent:RNPushKitRegistered body:@{@"pushKitToken": token}];
} }
- (void)didReceiveIncomingPushWithPayload:(NSDictionary *)payload {
[RNEventEmitter sendEvent:RNPushKitNotificationReceived body:payload];
}
@end @end
...@@ -20,7 +20,7 @@ ...@@ -20,7 +20,7 @@
} }
- (void)pushRegistry:(PKPushRegistry *)registry didReceiveIncomingPushWithPayload:(PKPushPayload *)payload forType:(NSString *)type { - (void)pushRegistry:(PKPushRegistry *)registry didReceiveIncomingPushWithPayload:(PKPushPayload *)payload forType:(NSString *)type {
// [_pushKitEventHandler didOpenNotificationPayload:payload.dictionaryPayload]; [_pushKitEventHandler didReceiveIncomingPushWithPayload:payload.dictionaryPayload];
} }
@end @end
...@@ -150,22 +150,7 @@ Notification **actions** allow the user to interact with a given notification. ...@@ -150,22 +150,7 @@ Notification **actions** allow the user to interact with a given notification.
Notification **categories** allow you to group multiple actions together, and to connect the actions with the push notification itself. Notification **categories** allow you to group multiple actions together, and to connect the actions with the push notification itself.
In order to support interactive notifications, firstly add the following methods to `appDelegate.m` file: Follow the basic workflow of adding interactive notifications to your app:
```objective-c
// Required for the notification actions.
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification withResponseInfo:(NSDictionary *)responseInfo completionHandler:(void (^)())completionHandler
{
[RNNotifications handleActionWithIdentifier:identifier forLocalNotification:notification withResponseInfo:responseInfo completionHandler:completionHandler];
}
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo withResponseInfo:(NSDictionary *)responseInfo completionHandler:(void (^)())completionHandler
{
[RNNotifications handleActionWithIdentifier:identifier forRemoteNotification:userInfo withResponseInfo:responseInfo completionHandler:completionHandler];
}
```
Then, follow the basic workflow of adding interactive notifications to your app:
1. Config the actions. 1. Config the actions.
2. Group actions together into categories. 2. Group actions together into categories.
...@@ -187,25 +172,13 @@ let upvoteAction = new NotificationAction({ ...@@ -187,25 +172,13 @@ let upvoteAction = new NotificationAction({
buttonTitle: 'title', buttonTitle: 'title',
placeholder: 'placeholder text' placeholder: 'placeholder text'
} }
}, (action, completed) => {
console.log("ACTION RECEIVED");
console.log(JSON.stringify(action));
// You must call to completed(), otherwise the action will not be triggered
completed();
}); });
let replyAction = new NotificationAction({ let replyAction = new NotificationAction({
activationMode: "background", activationMode: "background",
title: "Reply", title: "Reply",
behavior: "textInput",
authenticationRequired: true, authenticationRequired: true,
identifier: "REPLY_ACTION" identifier: "REPLY_ACTION"
}, (action, completed) => {
console.log("ACTION RECEIVED");
console.log(action);
completed();
}); });
``` ```
...@@ -216,8 +189,7 @@ We will group `upvote` action and `reply` action into a single category: `EXAMPL ...@@ -216,8 +189,7 @@ We will group `upvote` action and `reply` action into a single category: `EXAMPL
```javascript ```javascript
let exampleCategory = new NotificationCategory({ let exampleCategory = new NotificationCategory({
identifier: "EXAMPLE_CATEGORY", identifier: "EXAMPLE_CATEGORY",
actions: [upvoteAction, replyAction], actions: [upvoteAction, replyAction]
context: "default"
}); });
``` ```
...@@ -249,9 +221,7 @@ The [example app](https://github.com/wix/react-native-notifications/tree/master/ ...@@ -249,9 +221,7 @@ The [example app](https://github.com/wix/react-native-notifications/tree/master/
- `activationMode` - Indicating whether the app should activate to the foreground or background. - `activationMode` - Indicating whether the app should activate to the foreground or background.
- `foreground` (default) - Activate the app and put it in the foreground. - `foreground` (default) - Activate the app and put it in the foreground.
- `background` - Activate the app and put it in the background. If the app is already in the foreground, it remains in the foreground. - `background` - Activate the app and put it in the background. If the app is already in the foreground, it remains in the foreground.
- `behavior` - Indicating additional behavior that the action supports. - `textInput` - `TextInput` payload, when supplied, the system will present text input in this action.
- `default` - No additional behavior.
- `textInput` - When button is tapped, the action opens a text input. the text will be delivered to your action callback.
- `destructive` - A Boolean value indicating whether the action is destructive. When the value of this property is `true`, the system displays the corresponding button differently to indicate that the action is destructive. - `destructive` - A Boolean value indicating whether the action is destructive. When the value of this property is `true`, the system displays the corresponding button differently to indicate that the action is destructive.
- `authenticationRequired` - A Boolean value indicating whether the user must unlock the device before the action is performed. - `authenticationRequired` - A Boolean value indicating whether the user must unlock the device before the action is performed.
...@@ -259,9 +229,11 @@ The [example app](https://github.com/wix/react-native-notifications/tree/master/ ...@@ -259,9 +229,11 @@ The [example app](https://github.com/wix/react-native-notifications/tree/master/
- `identifier` - The name of the action group (must be unique). - `identifier` - The name of the action group (must be unique).
- `actions` - An array of `NotificationAction` objects, which related to this category. - `actions` - An array of `NotificationAction` objects, which related to this category.
- `context` - Indicating the amount of space available for displaying actions in a notification.
- `default` (default) - Displayes up to 4 actions (full UI). ### `TextInput` Payload
- `minimal` - Displays up tp 2 actions (minimal UI).
- `buttonTitle` - Title of the `send` button.
- `placeholder` - Placeholder for the `textInput`.
#### Get and set application icon badges count (iOS only) #### Get and set application icon badges count (iOS only)
......
...@@ -6,7 +6,6 @@ ...@@ -6,7 +6,6 @@
When a push notification is received by the device, the application can be in one of the following states: When a push notification is received by the device, the application can be in one of the following states:
1. **Forground:** When the app is running and is used by the user right now; in this case, a `notificationReceivedForeground` event will be fired. 1. **Forground:** When the app is running and is used by the user right now; in this case, a `notificationReceivedForeground` event will be fired.
2. **Background:** When the app is running in a background state; in this case, a `notificationReceivedBackground` event will be fired.
Finally, when a notification is _opened_ by the device user (i.e. tapped-on), a `notificationOpened` event is fired. Finally, when a notification is _opened_ by the device user (i.e. tapped-on), a `notificationOpened` event is fired.
......
...@@ -18,7 +18,6 @@ let upvoteAction = new NotificationAction({ ...@@ -18,7 +18,6 @@ let upvoteAction = new NotificationAction({
let replyAction = new NotificationAction({ let replyAction = new NotificationAction({
activationMode: 'background', activationMode: 'background',
title: 'Reply', title: 'Reply',
behavior: 'textInput',
authenticationRequired: true, authenticationRequired: true,
textInput: { textInput: {
buttonTitle: 'Reply now', buttonTitle: 'Reply now',
...@@ -45,6 +44,7 @@ class NotificationsExampleApp extends Component { ...@@ -45,6 +44,7 @@ class NotificationsExampleApp extends Component {
NotificationsIOS.addEventListener('notificationReceivedForeground', this.onNotificationReceivedForeground.bind(this)); NotificationsIOS.addEventListener('notificationReceivedForeground', this.onNotificationReceivedForeground.bind(this));
NotificationsIOS.addEventListener('notificationOpened', this.onNotificationOpened.bind(this)); NotificationsIOS.addEventListener('notificationOpened', this.onNotificationOpened.bind(this));
NotificationsIOS.addEventListener('pushKitNotificationReceived', this.onPushKitNotificationReceived.bind(this));
} }
onPushRegistered(deviceToken) { onPushRegistered(deviceToken) {
...@@ -59,6 +59,10 @@ class NotificationsExampleApp extends Component { ...@@ -59,6 +59,10 @@ class NotificationsExampleApp extends Component {
console.log('PushKit Token Received: ' + deviceToken); console.log('PushKit Token Received: ' + deviceToken);
} }
onPushKitNotificationReceived(notification) {
console.log('PushKit notification Received: ' + JSON.stringify(notification));
}
onNotificationReceivedForeground(notification, completion) { onNotificationReceivedForeground(notification, completion) {
console.log('Notification Received Foreground: ' + JSON.stringify(notification)); console.log('Notification Received Foreground: ' + JSON.stringify(notification));
this.setState({ this.setState({
...@@ -98,8 +102,7 @@ class NotificationsExampleApp extends Component { ...@@ -98,8 +102,7 @@ class NotificationsExampleApp extends Component {
requestPermissions() { requestPermissions() {
let cat = new NotificationCategory({ let cat = new NotificationCategory({
identifier: 'SOME_CATEGORY', identifier: 'SOME_CATEGORY',
actions: [upvoteAction, replyAction], actions: [upvoteAction, replyAction]
context: 'default'
}); });
NotificationsIOS.requestPermissions([cat]); NotificationsIOS.requestPermissions([cat]);
} }
......
...@@ -25,22 +25,17 @@ ...@@ -25,22 +25,17 @@
self.window.rootViewController = rootViewController; self.window.rootViewController = rootViewController;
[self.window makeKeyAndVisible]; [self.window makeKeyAndVisible];
[[RNNotifications sharedInstance] initialize]; [RNNotifications startMonitorNotifications];
return YES; return YES;
} }
// Required to register for notifications
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
// [[RNNotifications sharedInstance] didRegisterUserNotificationSettings:notificationSettings];
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
[[RNNotifications sharedInstance] didRegisterForRemoteNotificationsWithDeviceToken:deviceToken]; [RNNotifications didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
} }
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error { - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
[[RNNotifications sharedInstance] didFailToRegisterForRemoteNotificationsWithError:error]; [RNNotifications didFailToRegisterForRemoteNotificationsWithError:error];
} }
@end @end
...@@ -13,13 +13,16 @@ export const DEVICE_REMOTE_NOTIFICATIONS_REGISTRATION_FAILED_EVENT = 'remoteNoti ...@@ -13,13 +13,16 @@ export const DEVICE_REMOTE_NOTIFICATIONS_REGISTRATION_FAILED_EVENT = 'remoteNoti
export const DEVICE_PUSH_KIT_REGISTERED_EVENT = 'pushKitRegistered'; export const DEVICE_PUSH_KIT_REGISTERED_EVENT = 'pushKitRegistered';
export const DEVICE_NOTIFICATION_RECEIVED_FOREGROUND_EVENT = 'notificationReceivedForeground'; export const DEVICE_NOTIFICATION_RECEIVED_FOREGROUND_EVENT = 'notificationReceivedForeground';
export const DEVICE_NOTIFICATION_OPENED_EVENT = 'notificationOpened'; export const DEVICE_NOTIFICATION_OPENED_EVENT = 'notificationOpened';
export const DEVICE_PUSH_KIT_NOTIFICATION_RECEIVED_EVENT = 'pushKitNotificationReceived';
const _exportedEvents = [ const _exportedEvents = [
DEVICE_REMOTE_NOTIFICATIONS_REGISTERED_EVENT, DEVICE_REMOTE_NOTIFICATIONS_REGISTERED_EVENT,
DEVICE_REMOTE_NOTIFICATIONS_REGISTRATION_FAILED_EVENT, DEVICE_REMOTE_NOTIFICATIONS_REGISTRATION_FAILED_EVENT,
DEVICE_PUSH_KIT_REGISTERED_EVENT, DEVICE_PUSH_KIT_REGISTERED_EVENT,
DEVICE_NOTIFICATION_RECEIVED_FOREGROUND_EVENT, DEVICE_NOTIFICATION_RECEIVED_FOREGROUND_EVENT,
DEVICE_NOTIFICATION_OPENED_EVENT DEVICE_NOTIFICATION_OPENED_EVENT,
DEVICE_PUSH_KIT_NOTIFICATION_RECEIVED_EVENT
]; ];
const _notificationHandlers = new Map(); const _notificationHandlers = new Map();
const _actionHandlers = new Map(); const _actionHandlers = new Map();
...@@ -50,6 +53,7 @@ export default class NotificationsIOS { ...@@ -50,6 +53,7 @@ export default class NotificationsIOS {
* - `remoteNotificationsRegistered` : Fired when the user registers for remote notifications. The handler will be invoked with a hex string representing the deviceToken. * - `remoteNotificationsRegistered` : Fired when the user registers for remote notifications. The handler will be invoked with a hex string representing the deviceToken.
* - `notificationReceivedForeground` : Fired when a notification (local / remote) is received when app is on foreground state. * - `notificationReceivedForeground` : Fired when a notification (local / remote) is received when app is on foreground state.
* - `notificationOpened`: Fired when a notification (local / remote) is opened. * - `notificationOpened`: Fired when a notification (local / remote) is opened.
* - `pushKitNotificationReceived` : Fired when a pushKit notification received when app is both on foreground and background state.
*/ */
static addEventListener(type: string, handler: Function) { static addEventListener(type: string, handler: Function) {
if (_exportedEvents.indexOf(type) !== -1) { if (_exportedEvents.indexOf(type) !== -1) {
...@@ -84,6 +88,11 @@ export default class NotificationsIOS { ...@@ -84,6 +88,11 @@ export default class NotificationsIOS {
NativeRNNotifications.finishHandlingAction(identifier); NativeRNNotifications.finishHandlingAction(identifier);
}, action) }, action)
); );
} else if (type === DEVICE_PUSH_KIT_NOTIFICATION_RECEIVED_EVENT) {
listener = DeviceEventEmitter.addListener(
type,
(payload) => handler(new IOSNotification(payload))
);
} }
_notificationHandlers.set(handler, listener); _notificationHandlers.set(handler, listener);
......
...@@ -10,26 +10,26 @@ export default class IOSNotification { ...@@ -10,26 +10,26 @@ export default class IOSNotification {
constructor(notification: Object) { constructor(notification: Object) {
this._data = {}; this._data = {};
if (notification.userInfo.aps && if (notification.aps &&
notification.userInfo.aps['content-available'] && notification.aps['content-available'] &&
notification.userInfo.aps['content-available'] === 1 && notification.aps['content-available'] === 1 &&
!notification.userInfo.aps.alert && !notification.aps.alert &&
!notification.userInfo.aps.sound && !notification.aps.sound &&
notification.managedAps) { notification.managedAps) {
// managed notification // managed notification
this._alert = notification.managedAps.alert; this._alert = notification.managedAps.alert;
this._sound = notification.managedAps.sound; this._sound = notification.managedAps.sound;
this._badge = notification.userInfo.aps.badge; this._badge = notification.aps.badge;
this._category = notification.managedAps.category; this._category = notification.managedAps.category;
this._type = 'managed'; this._type = 'managed';
this._thread = notification.userInfo.aps['thread-id']; this._thread = notification.aps['thread-id'];
} else if ( } else if (
notification.userInfo.aps && notification.aps &&
notification.userInfo.aps.alert) { notification.aps.alert) {
// regular notification // regular notification
this._alert = notification.userInfo.aps.alert; this._alert = notification.aps.alert;
this._sound = notification.userInfo.aps.sound; this._sound = notification.aps.sound;
this._badge = notification.userInfo.aps.badge; this._badge = notification.aps.badge;
this._category = notification.category; this._category = notification.category;
this._type = 'regular'; this._type = 'regular';
this._thread = notification.thread; this._thread = notification.thread;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment