Skip to content

Commit 73da7be

Browse files
committed
Update chat notifications api snippets for latest Swift Chat SDK
1 parent aaf8a00 commit 73da7be

File tree

5 files changed

+38
-35
lines changed

5 files changed

+38
-35
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
func application(application: UIApplication,
2-
didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
1+
func application(_ application: UIApplication,
2+
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
2-
// If your application supports multiple types of push notifications, you may wish to limit which ones you send to the TwilioChatClient here
3-
if let chatClient = chatClient where chatClient.userInfo != nil {
4-
// If your reference to the Chat client exists and is initialized, send the notification to it
5-
chatClient.handleNotification(userInfo) { (result) in
6-
if (!result.isSuccessful()) {
7-
// Handling of notification was not successful, retry?
1+
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
2+
print(userInfo)
3+
// If your application supports multiple types of push notifications, you may wish to limit which ones you send to the TwilioChatClient here
4+
if let chatClient = chatClient, chatClient.user != nil {
5+
// If your reference to the Chat client exists and is initialized, send the notification to it
6+
chatClient.handleNotification(userInfo) { (result) in
7+
if (!result.isSuccessful()) {
8+
// Handling of notification was not successful, retry?
9+
}
810
}
9-
}
10-
} else {
11-
// Store the notification for later handling
12-
receivedNotification = userInfo
13-
}
11+
} else {
12+
// Store the notification for later handling
13+
receivedNotification = userInfo
14+
}
1415
}
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
if let updatedPushToken = updatedPushToken {
2-
chatClient.registerWithToken(updatedPushToken) { (result) in
3-
if (!result.isSuccessful()) {
2+
chatClient.register(withNotificationToken: updatedPushToken) { (result) in
3+
if (!result.isSuccessful()) {
44
// try registration again or verify token
5-
}
65
}
6+
}
77
}
88

99
if let receivedNotification = receivedNotification {
10-
chatClient.handleNotification(receivedNotification) { (result) in
11-
if (!result.isSuccessful()) {
10+
chatClient.handleNotification(receivedNotification) { (result) in
11+
if (!result.isSuccessful()) {
1212
// Handling of notification was not successful, retry?
13-
}
1413
}
14+
}
1515
}
Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
2-
if let chatClient = chatClient where chatClient.user != nil {
3-
chatClient.registerWithNotificationToken(deviceToken) { (result) in
4-
if (!result.isSuccessful()) {
5-
// try registration again or verify token
6-
}
1+
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
2+
print("Received device token")
3+
if let chatClient = chatClient, chatClient.user != nil {
4+
chatClient.register(withNotificationToken: deviceToken) { (result) in
5+
if (!result.isSuccessful()) {
6+
// try registration again or verify token
7+
}
78
}
89
} else {
910
updatedPushToken = deviceToken
1011
}
1112
}
1213

13-
func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
14-
NSLog("Failed to get token, error: %@", error)
14+
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
15+
print("Failed to get token, error: %@", error)
1516
updatedPushToken = nil
16-
}
17+
}
Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
UIApplication.sharedApplication().registerUserNotificationSettings(
2-
UIUserNotificationSettings(
3-
forTypes: [.Sound, .Alert, .Badge],
4-
categories: nil
5-
)
6-
)
1+
let center = UNUserNotificationCenter.current()
2+
center.requestAuthorization(options: [.alert, .badge, .sound]) {
3+
(granted, error) in
4+
print("User allowed notifications:", granted)
5+
}
6+
UIApplication.shared.registerForRemoteNotifications()
7+

0 commit comments

Comments
 (0)