Skip to content

Commit 92cfd84

Browse files
author
Craig Dennis
authored
Merge pull request TwilioDevEd#811 from Gray-Wind/feature/update-ios-notification-instructions
Update iOS push notification code snippets
2 parents e7a0c43 + 0b38bde commit 92cfd84

File tree

4 files changed

+69
-7
lines changed

4 files changed

+69
-7
lines changed

ip-messaging/pushiOS/didreceiveremote/didreceiveremote.m

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
1+
// For iOS 10 and later; do not forget to set up a delegate for UNUserNotificationCenter
2+
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
3+
didReceiveNotificationResponse:(UNNotificationResponse *)response
4+
withCompletionHandler:(void (^)(void))completionHandler {
5+
NSDictionary *userInfo = response.notification.request.content.userInfo;
6+
// If your application supports multiple types of push notifications, you may wish to limit which ones you send to the TwilioChatClient here
7+
if (self.chatClient) {
8+
// If your reference to the Chat client exists and is initialized, send the notification to it
9+
[self.chantClient handleNotification:userInfo completion:^(TCHResult *result) {
10+
if (![result isSuccessful]) {
11+
// Handling of notification was not successful, retry?
12+
}
13+
}];
14+
} else {
15+
// Store the notification for later handling
16+
self.receivedNotification = userInfo;
17+
}
18+
}
19+
20+
// For iOS versions before 10
121
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
222
// If your application supports multiple types of push notifications, you may wish to limit which ones you send to the TwilioChatClient here
323
if (self.chatClient && userInfo) {
@@ -11,4 +31,4 @@ - (void)application:(UIApplication *)application didReceiveRemoteNotification:(N
1131
// Store the notification for later handling
1232
self.receivedNotification = userInfo;
1333
}
14-
}
34+
}

ip-messaging/pushiOS/didreceiveremote/didreceiveremote.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
1+
// For iOS 10 and later; do not forget to set up a delegate for UNUserNotificationCenter
2+
func userNotificationCenter(_ center: UNUserNotificationCenter,
3+
didReceive response: UNNotificationResponse,
4+
withCompletionHandler completionHandler:
5+
@escaping () -> Void) {
6+
let userInfo = response.notification.request.content.userInfo
7+
if let chatClient = chatClient, chatClient.user != nil {
8+
// If your reference to the Chat client exists and is initialized, send the notification to it
9+
chatClient.handleNotification(userInfo) { (result) in
10+
if (!result.isSuccessful()) {
11+
// Handling of notification was not successful, retry?
12+
}
13+
}
14+
} else {
15+
// Store the notification for later handling
16+
receivedNotification = userInfo
17+
}
18+
}
19+
20+
// For iOS versions before 10
121
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
222
// If your application supports multiple types of push notifications, you may wish to limit which ones you send to the TwilioChatClient here
323
if let chatClient = chatClient, chatClient.user != nil {
Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
1-
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
2-
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
1+
if (@available(iOS 10.0, *)) {
2+
UNUserNotificationCenter *currentNotificationCenter = [UNUserNotificationCenter currentNotificationCenter];
3+
[currentCenter requestAuthorizationWithOptions:UNAuthorizationOptionBadge | UNAuthorizationOptionAlert | UNAuthorizationOptionSound
4+
completionHandler:^(BOOL granted, NSError *error) {
5+
// Add here your handling of granted or not granted permissions
6+
}];
7+
currentNotificationCenter.delegate = self;
38
} else {
4-
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
5-
(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
6-
}
9+
NSDictionary* localNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
10+
if (localNotification) {
11+
[self application:application didReceiveRemoteNotification:localNotification];
12+
}
13+
14+
[UIApplication.sharedApplication registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
15+
[UIApplication.sharedApplication registerForRemoteNotifications];
16+
}

ip-messaging/pushiOS/usernotificationsettings/usernotificationsettings.m

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
// For iOS 10 and later add this to the didFinishLaunchingWithOptions function or a similar place
2+
// once you get granted permissions
3+
if (@available(iOS 10.0, *)) {
4+
UNUserNotificationCenter *currentNotificationCenter = [UNUserNotificationCenter currentNotificationCenter];
5+
[currentNotificationCenter getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings *settings) {
6+
if (settings.authorizationStatus == UNAuthorizationStatusAuthorized) {
7+
[UIApplication.sharedApplication registerForRemoteNotifications];
8+
}
9+
}];
10+
}
11+
12+
// For iOS versions before 10 you should add such implementation
113
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
214
if(notificationSettings.types == UIUserNotificationTypeNone) {
315
NSLog(@"Failed to get token, error: Notifications are not allowed");
@@ -7,6 +19,6 @@ - (void)application:(UIApplication *)application didRegisterUserNotificationSett
719
self.updatedPushToken = nil;
820
}
921
} else {
10-
[[UIApplication sharedApplication] registerForRemoteNotifications];
22+
[UIApplication.sharedApplication registerForRemoteNotifications];
1123
}
1224
}

0 commit comments

Comments
 (0)