Skip to content

Commit 81d4599

Browse files
committed
Replaced UIUserNotificationSettings deprecated APIs. Supported Azure notification hub template.
1 parent 0f9cb22 commit 81d4599

16 files changed

+874
-305
lines changed

docs/ios-installation.md

Lines changed: 63 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ react-native init ReactNativeAzureNotificationHubSample
99
```
1010

1111
In addition to the standard React Native requirements, you will also need the following:
12-
* An iOS 9 (or later version)-capable device (simulator doesn't work with push notifications)
12+
* An iOS 10 (or later version)-capable device (simulator doesn't work with push notifications)
1313
* [Apple Developer Program](https://developer.apple.com/programs/) membership
1414

1515
## Install react-native-azurenotificationhub
@@ -156,34 +156,44 @@ Add the following line to your `ios/Podfile` file and run **pod install**
156156
* And then add the following code in the same file:
157157

158158
```objective-c
159-
// Required to register for notifications
160-
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
159+
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
161160
{
162-
[RCTAzureNotificationHubManager didRegisterUserNotificationSettings:notificationSettings];
161+
...
162+
163+
// Registering for local notifications
164+
[[UNUserNotificationCenter currentNotificationCenter] setDelegate:self];
165+
166+
return YES;
163167
}
164168

165-
// Required for the register event.
169+
// Invoked when the app successfully registered with Apple Push Notification service (APNs).
166170
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
167171
{
168172
[RCTAzureNotificationHubManager didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
169173
}
170174

171-
// Required for the registrationError event.
175+
// Invoked when APNs cannot successfully complete the registration process.
172176
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
173177
{
174178
[RCTAzureNotificationHubManager didFailToRegisterForRemoteNotificationsWithError:error];
175179
}
176180

177-
// Required for the notification event.
178-
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)notification
181+
// Invoked when a remote notification arrived and there is data to be fetched.
182+
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
183+
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler
179184
{
180-
[RCTAzureNotificationHubManager didReceiveRemoteNotification:notification];
185+
[RCTAzureNotificationHubManager didReceiveRemoteNotification:userInfo
186+
fetchCompletionHandler:completionHandler];
181187
}
182188

183-
// Required for the localNotification event.
184-
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
189+
// Invoked when a notification arrived while the app was running in the foreground.
190+
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
191+
willPresentNotification:(UNNotification *)notification
192+
withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
185193
{
186-
[RCTAzureNotificationHubManager didReceiveLocalNotification:notification];
194+
[RCTAzureNotificationHubManager userNotificationCenter:center
195+
willPresentNotification:notification
196+
withCompletionHandler:completionHandler];
187197
}
188198
```
189199

@@ -221,9 +231,12 @@ import {
221231

222232
const NotificationHub = require('react-native-azurenotificationhub/index.ios');
223233

224-
const connectionString = '...'; // The Notification Hub connection string
225-
const hubName = '...'; // The Notification Hub name
226-
const tags = [ ... ]; // The set of tags to subscribe to
234+
const connectionString = '...'; // The Notification Hub connection string
235+
const hubName = '...'; // The Notification Hub name
236+
const tags = [ ... ]; // The set of tags to subscribe to
237+
const template = '...'; // Notification hub templates:
238+
// https://docs.microsoft.com/en-us/azure/notification-hubs/notification-hubs-templates-cross-platform-push-messages
239+
const templateName = '...'; // The template's name
227240

228241
let remoteNotificationsDeviceToken = ''; // The device token registered with APNS
229242

@@ -267,15 +280,33 @@ export default class App extends Component {
267280
// rejects, or if the permissions were previously rejected. The promise
268281
// resolves to the current state of the permission of
269282
// {alert: boolean, badge: boolean,sound: boolean }
270-
NotificationHub.requestPermissions();
283+
NotificationHub.requestPermissions()
284+
.then((res) => console.warn(res))
285+
.catch(reason => console.warn(reason));
271286
}
272287

273288
register() {
274-
NotificationHub.register(remoteNotificationsDeviceToken, {connectionString, hubName, tags});
289+
NotificationHub.register(remoteNotificationsDeviceToken, { connectionString, hubName, tags })
290+
.then((res) => console.warn(res))
291+
.catch(reason => console.warn(reason));
292+
}
293+
294+
registerTemplate() {
295+
NotificationHub.registerTemplate(remoteNotificationsDeviceToken, { connectionString, hubName, tags, templateName, template })
296+
.then((res) => console.warn(res))
297+
.catch(reason => console.warn(reason));
275298
}
276299

277300
unregister() {
278-
NotificationHub.unregister();
301+
NotificationHub.unregister()
302+
.then((res) => console.warn(res))
303+
.catch(reason => console.warn(reason));
304+
}
305+
306+
unregisterTemplate() {
307+
NotificationHub.unregisterTemplate(templateName)
308+
.then((res) => console.warn(res))
309+
.catch(reason => console.warn(reason));
279310
}
280311

281312
render() {
@@ -295,13 +326,27 @@ export default class App extends Component {
295326
</Text>
296327
</View>
297328
</TouchableOpacity>
329+
<TouchableOpacity onPress={this.registerTemplate.bind(this)}>
330+
<View style={styles.button}>
331+
<Text style={styles.buttonText}>
332+
Register Template
333+
</Text>
334+
</View>
335+
</TouchableOpacity>
298336
<TouchableOpacity onPress={this.unregister.bind(this)}>
299337
<View style={styles.button}>
300338
<Text style={styles.buttonText}>
301339
Unregister
302340
</Text>
303341
</View>
304342
</TouchableOpacity>
343+
<TouchableOpacity onPress={this.unregisterTemplate.bind(this)}>
344+
<View style={styles.button}>
345+
<Text style={styles.buttonText}>
346+
Unregister Template
347+
</Text>
348+
</View>
349+
</TouchableOpacity>
305350
</View>
306351
);
307352
}

ios/AzureNotificationHubIOS.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -357,12 +357,20 @@ class AzureNotificationHubIOS {
357357
RCTAzureNotificationHubManager.checkPermissions(callback);
358358
}
359359

360-
static register(deviceToken, config) {
361-
RCTAzureNotificationHubManager.register(deviceToken, config);
360+
static register(deviceToken, config): Promise<string | Object> {
361+
return RCTAzureNotificationHubManager.register(deviceToken, config);
362362
}
363363

364-
static unregister() {
365-
RCTAzureNotificationHubManager.unregister();
364+
static registerTemplate(deviceToken, config): Promise<string | Object> {
365+
return RCTAzureNotificationHubManager.registerTemplate(deviceToken, config);
366+
}
367+
368+
static unregister(): Promise<string | Object> {
369+
return RCTAzureNotificationHubManager.unregister();
370+
}
371+
372+
static unregisterTemplate(templateName): Promise<string | Object> {
373+
return RCTAzureNotificationHubManager.unregisterTemplate(templateName);
366374
}
367375

368376
/**

ios/RCTAzureNotificationHubManager/RCTAzureNotificationHandler.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,6 @@
3535
// Handle registration error for Azure Notification Hub
3636
- (void)azureNotificationHubRegisteredError:(nonnull NSNotification *)notification;
3737

38-
// Handle successful registration for UIUserNotificationSettings
39-
- (void)userNotificationSettingsRegistered:(nonnull NSNotification *)notification;
40-
4138
@end
4239

4340
#endif /* RCTAzureNotificationHandler_h */

ios/RCTAzureNotificationHubManager/RCTAzureNotificationHandler.m

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
#import "RCTAzureNotificationHub.h"
1111
#import "RCTAzureNotificationHandler.h"
1212

13-
extern RCTPromiseResolveBlock requestPermissionsResolveBlock;
14-
1513
@implementation RCTAzureNotificationHandler
1614
{
1715
@private
@@ -92,24 +90,4 @@ - (void)azureNotificationHubRegisteredError:(nonnull NSNotification *)notificati
9290
body:errorDetails];
9391
}
9492

95-
// Handle successful registration for UIUserNotificationSettings
96-
- (void)userNotificationSettingsRegistered:(nonnull NSNotification *)notification
97-
{
98-
RCTPromiseResolveBlock resolve = notification.userInfo[RCTUserInfoResolveBlock];
99-
if (resolve == nil)
100-
{
101-
return;
102-
}
103-
104-
UIUserNotificationSettings *notificationSettings = notification.userInfo[RCTUserInfoNotificationSettings];
105-
NSDictionary *notificationTypes = @{
106-
RCTNotificationTypeAlert: @((notificationSettings.types & UIUserNotificationTypeAlert) > 0),
107-
RCTNotificationTypeSound: @((notificationSettings.types & UIUserNotificationTypeSound) > 0),
108-
RCTNotificationTypeBadge: @((notificationSettings.types & UIUserNotificationTypeBadge) > 0),
109-
};
110-
111-
resolve(notificationTypes);
112-
requestPermissionsResolveBlock = nil;
113-
}
114-
11593
@end

ios/RCTAzureNotificationHubManager/RCTAzureNotificationHub.h

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#ifndef RCTAzureNotificationHub_h
1111
#define RCTAzureNotificationHub_h
1212

13+
@import UserNotifications;
14+
1315
// Notification Hub events
1416
extern NSString *const RCTLocalNotificationReceived;
1517
extern NSString *const RCTRemoteNotificationReceived;
@@ -23,9 +25,10 @@ extern NSString *const RCTUserNotificationSettingsRegistered;
2325
extern NSString *const RCTConnectionStringKey;
2426
extern NSString *const RCTHubNameKey;
2527
extern NSString *const RCTTagsKey;
28+
extern NSString *const RCTTemplateNameKey;
29+
extern NSString *const RCTTemplateKey;
2630

2731
// User info
28-
extern NSString *const RCTUserInfoNotificationSettings;
2932
extern NSString *const RCTUserInfoDeviceToken;
3033
extern NSString *const RCTUserInfoRemote;
3134
extern NSString *const RCTUserInfoResolveBlock;
@@ -38,15 +41,25 @@ extern NSString *const RCTNotificationTypeBadge;
3841
extern NSString *const RCTNotificationTypeSound;
3942
extern NSString *const RCTNotificationTypeAlert;
4043

44+
// Messages
45+
extern NSString *const RCTPromiseResolveUnregiseredSuccessfully;
46+
4147
// Errors
4248
extern NSString *const RCTErrorUnableToRequestPermissions;
43-
extern NSString *const RCTErrorUnableToRequestPermissionsDetails;
49+
extern NSString *const RCTErrorUnableToRequestPermissionsAppExt;
4450
extern NSString *const RCTErrorUnableToRequestPermissionsTwice;
51+
extern NSString *const RCTErrorUnableToRequestPermissionsUserReject;
4552
extern NSString *const RCTErrorInvalidArguments;
4653
extern NSString *const RCTErrorMissingConnectionString;
4754
extern NSString *const RCTErrorMissingHubName;
55+
extern NSString *const RCTErrorMissingTemplateName;
56+
extern NSString *const RCTErrorMissingTemplate;
57+
extern NSString *const RCTErrorUnableToUnregister;
58+
extern NSString *const RCTErrorUnableToUnregisterNoRegistration;
4859

49-
// Completion type used in Azure Notification Hub's native methods
60+
// Completion types
5061
typedef void (^RCTNativeCompletion)(NSError *error);
62+
typedef void (^RCTNotificationCompletion)(BOOL granted, NSError * _Nullable error);
63+
typedef void (^RCTNotificationSettingsCompletion)(UNNotificationSettings * _Nonnull settings);
5164

5265
#endif /* RCTAzureNotificationHub_h */

ios/RCTAzureNotificationHubManager/RCTAzureNotificationHub.m

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@
2020
NSString *const RCTConnectionStringKey = @"connectionString";
2121
NSString *const RCTHubNameKey = @"hubName";
2222
NSString *const RCTTagsKey = @"tags";
23+
NSString *const RCTTemplateNameKey = @"templateName";
24+
NSString *const RCTTemplateKey = @"template";
2325

2426
// User info
25-
NSString *const RCTUserInfoNotificationSettings = @"notificationSettings";
2627
NSString *const RCTUserInfoDeviceToken = @"deviceToken";
2728
NSString *const RCTUserInfoRemote = @"remote";
2829
NSString *const RCTUserInfoResolveBlock = @"resolveBlock";
@@ -35,10 +36,18 @@
3536
NSString *const RCTNotificationTypeSound = @"sound";
3637
NSString *const RCTNotificationTypeAlert = @"alert";
3738

39+
// Messages
40+
NSString *const RCTPromiseResolveUnregiseredSuccessfully = @"Unregisted successfully";
41+
3842
// Errors
3943
NSString *const RCTErrorUnableToRequestPermissions = @"Unabled to request permissions";
40-
NSString *const RCTErrorUnableToRequestPermissionsDetails = @"Requesting push notifications is currently unavailable in an app extension";
44+
NSString *const RCTErrorUnableToRequestPermissionsAppExt = @"Requesting push notifications is currently unavailable in an app extension.";
4145
NSString *const RCTErrorUnableToRequestPermissionsTwice = @"Cannot call requestPermissions twice before the first has returned.";
46+
NSString *const RCTErrorUnableToRequestPermissionsUserReject = @"User didn't allow permissions.";
4247
NSString *const RCTErrorInvalidArguments = @"Invalid arguments";
4348
NSString *const RCTErrorMissingConnectionString = @"Connection string cannot be null.";
4449
NSString *const RCTErrorMissingHubName = @"Hub name cannot be null.";
50+
NSString *const RCTErrorMissingTemplateName = @"Template name cannot be null.";
51+
NSString *const RCTErrorMissingTemplate = @"Template cannot be null.";
52+
NSString *const RCTErrorUnableToUnregister = @"Unabled to unregister";
53+
NSString *const RCTErrorUnableToUnregisterNoRegistration = @"There is no registration.";

ios/RCTAzureNotificationHubManager/RCTAzureNotificationHubManager.h

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,26 @@
99

1010
#import "React/RCTEventEmitter.h"
1111

12-
@interface RCTAzureNotificationHubManager : RCTEventEmitter
12+
#import "RCTAzureNotificationHandler.h"
13+
14+
@import UserNotifications;
1315

14-
// Required to register for notifications, invoked from AppDelegate
15-
+ (void)didRegisterUserNotificationSettings:(nonnull UIUserNotificationSettings *)notificationSettings;
16+
@interface RCTAzureNotificationHubManager : RCTEventEmitter
1617

17-
// Required for the register event, invoked from AppDelegate
18+
// Invoked from AppDelegate when the app successfully registered with Apple Push Notification service (APNs).
1819
+ (void)didRegisterForRemoteNotificationsWithDeviceToken:(nonnull NSData *)deviceToken;
1920

20-
// Required for the notification event, invoked from AppDelegate
21-
+ (void)didReceiveRemoteNotification:(nonnull NSDictionary *)notification;
21+
// Invoked from AppDelegate when APNs cannot successfully complete the registration process.
22+
+ (void)didFailToRegisterForRemoteNotificationsWithError:(nonnull NSError *)error;
2223

23-
// Required for the localNotification event, invoked from AppDelegate
24-
+ (void)didReceiveLocalNotification:(nonnull UILocalNotification *)notification;
24+
// Invoked from AppDelegate when a remote notification arrived and there is data to be fetched.
25+
+ (void)didReceiveRemoteNotification:(nonnull NSDictionary *)userInfo
26+
fetchCompletionHandler:(void (__unused ^_Nonnull)(UIBackgroundFetchResult result))completionHandler;
2527

26-
// Required for the registrationError event, invoked from AppDelegate
27-
+ (void)didFailToRegisterForRemoteNotificationsWithError:(nonnull NSError *)error;
28+
// Invoked from AppDelegate when a notification arrived while the app was running in the foreground.
29+
+ (void)userNotificationCenter:(nonnull __unused UNUserNotificationCenter *)center
30+
willPresentNotification:(nonnull UNNotification *)notification
31+
withCompletionHandler:(void (__unused ^_Nonnull)(UNNotificationPresentationOptions options))completionHandler;
2832

2933
// Set application icon badge number
3034
- (void)setApplicationIconBadgeNumber:(NSInteger)number;
@@ -65,11 +69,25 @@
6569
// Register with Azure Notification Hub
6670
- (void)register:(nonnull NSString *)deviceToken
6771
config:(nonnull NSDictionary *)config
68-
resolver:(nonnull RCTPromiseResolveBlock)resolve
72+
resolver:(nonnull __unused RCTPromiseResolveBlock)resolve
6973
rejecter:(nonnull RCTPromiseRejectBlock)reject;
7074

75+
// Register template
76+
- (void)registerTemplate:(nonnull NSString *)deviceToken
77+
config:(nonnull NSDictionary *)config
78+
resolver:(nonnull __unused RCTPromiseResolveBlock)resolve
79+
rejecter:(nonnull RCTPromiseRejectBlock)reject;
80+
7181
// Unregister with Azure Notification Hub
7282
- (void)unregister:(nonnull RCTPromiseResolveBlock)resolve
7383
rejecter:(nonnull RCTPromiseRejectBlock)reject;
7484

85+
// Unregister template
86+
- (void)unregisterTemplate:(nonnull NSString *)templateName
87+
resolver:(nonnull RCTPromiseResolveBlock)resolve
88+
rejecter:(nonnull RCTPromiseRejectBlock)reject;
89+
90+
// Set notification handler
91+
- (void)setNotificationHandler:(nonnull RCTAzureNotificationHandler *)handler;
92+
7593
@end

0 commit comments

Comments
 (0)