This repository was archived by the owner on Aug 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 474
Port Chat Push iOS snippets to Conversations #924
Merged
jefflinwood
merged 1 commit into
master
from
DEVED-5467_add_conversations_ios_push_snippets
Jul 27, 2021
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions |
2 changes: 2 additions & 0 deletions
2
conversations/push-ios/didfinishlaunching/didfinishlaunching.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| func application(_ application: UIApplication, didFinishLaunchingWithOptions | ||
| launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| { | ||
| "title": "Did Finish Launching", | ||
| "type": "mobile" | ||
| } |
20 changes: 20 additions & 0 deletions
20
conversations/push-ios/didreceiveremote/didreceiveremote.m
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| // Do not forget to set up a delegate for UNUserNotificationCenter | ||
| - (void)userNotificationCenter:(UNUserNotificationCenter *)center | ||
| didReceiveNotificationResponse:(UNNotificationResponse *)response | ||
| withCompletionHandler:(void (^)(void))completionHandler { | ||
| NSDictionary *userInfo = response.notification.request.content.userInfo; | ||
| // If your application supports multiple types of push notifications, | ||
| // you may wish to limit which ones you send to the TwilioConversationsClient here | ||
| if (self.conversationsClient) { | ||
| // If your reference to the Conversations client exists and is initialized, | ||
| // send the notification to it | ||
| [self.conversationsClient handleNotification:userInfo completion:^(TCHResult *result) { | ||
| if (![result isSuccessful]) { | ||
| // Handling of notification was not successful, retry? | ||
| } | ||
| }]; | ||
| } else { | ||
| // Store the notification for later handling | ||
| self.receivedNotification = userInfo; | ||
| } | ||
| } |
18 changes: 18 additions & 0 deletions
18
conversations/push-ios/didreceiveremote/didreceiveremote.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| // Do not forget to set up a delegate for UNUserNotificationCenter | ||
| func userNotificationCenter(_ center: UNUserNotificationCenter, | ||
| didReceive response: UNNotificationResponse, | ||
| withCompletionHandler completionHandler: @escaping () -> Void) { | ||
| let userInfo = response.notification.request.content.userInfo | ||
| if let conversationsClient = conversationsClient, conversationsClient.user != nil { | ||
| // If your reference to the Conversations client exists | ||
| // and is initialized, send the notification to it | ||
| conversationsClient.handleNotification(userInfo) { (result) in | ||
| if !result.isSuccessful() { | ||
| // Handling of notification was not successful, retry? | ||
| } | ||
| } | ||
| } else { | ||
| // Store the notification for later handling | ||
| receivedNotification = userInfo | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| { | ||
| "title": "Did Receive Notification", | ||
| "type": "mobile" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| { | ||
| "title": "Register Notifications", | ||
| "type": "mobile" | ||
| } |
17 changes: 17 additions & 0 deletions
17
conversations/push-ios/registernotifications/registernotifications.m
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| if (self.updatedPushToken) { | ||
| [self.conversationsClient registerWithNotificationToken:self.updatedPushToken | ||
| completion:^(TCHResult *result) { | ||
| if (![result isSuccessful]) { | ||
| // try registration again or verify token | ||
| } | ||
| }]; | ||
| } | ||
|
|
||
| if (self.receivedNotification) { | ||
| [self.conversationsClient handleNotification:self.receivedNotification | ||
| completion:^(TCHResult *result) { | ||
| if (![result isSuccessful]) { | ||
| // Handling of notification was not successful, retry? | ||
| } | ||
| }]; | ||
| } | ||
15 changes: 15 additions & 0 deletions
15
conversations/push-ios/registernotifications/registernotifications.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| if let updatedPushToken = updatedPushToken { | ||
| conversationsClient.register(withNotificationToken: updatedPushToken) { (result) in | ||
| if !result.isSuccessful() { | ||
| // try registration again or verify token | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if let receivedNotification = receivedNotification { | ||
| conversationsClient.handleNotification(receivedNotification) { (result) in | ||
| if !result.isSuccessful() { | ||
| // Handling of notification was not successful, retry? | ||
jefflinwood marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| { | ||
| "title": "Store Registration", | ||
| "type": "mobile" | ||
| } |
17 changes: 17 additions & 0 deletions
17
conversations/push-ios/remotenotifications/remotenotifications.m
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| - (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken { | ||
| if (self.conversationsClient && self.conversationsClient.user) { | ||
| [self.TwilioConversationsClient registerWithNotificationToken:deviceToken | ||
| completion:^(TCHResult *result) { | ||
| if (![result isSuccessful]) { | ||
| // try registration again or verify token | ||
| } | ||
| }]; | ||
| } else { | ||
| self.updatedPushToken = deviceToken; | ||
| } | ||
| } | ||
|
|
||
| - (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error { | ||
| NSLog(@"Failed to get token, error: %@", error); | ||
| self.updatedPushToken = nil; | ||
| } |
17 changes: 17 additions & 0 deletions
17
conversations/push-ios/remotenotifications/remotenotifications.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { | ||
| print("Received device token") | ||
| if let conversationsClient = conversationsClient, conversationsClient.user != nil { | ||
| conversationsClient.register(withNotificationToken: deviceToken) { (result) in | ||
| if !result.isSuccessful() { | ||
| // try registration again or verify token | ||
| } | ||
| } | ||
| } else { | ||
| updatedPushToken = deviceToken | ||
| } | ||
| } | ||
|
|
||
| func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) { | ||
| print("Failed to get token, error: %@", error) | ||
| updatedPushToken = nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| { | ||
| "title": "Notification Types", | ||
| "type": "mobile" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| UNUserNotificationCenter *currentNotificationCenter = [UNUserNotificationCenter currentNotificationCenter]; | ||
| [currentCenter requestAuthorizationWithOptions:UNAuthorizationOptionBadge | UNAuthorizationOptionAlert | UNAuthorizationOptionSound | ||
| completionHandler:^(BOOL granted, NSError *error) { | ||
| // Add here your handling of granted or not granted permissions | ||
| }]; | ||
| currentNotificationCenter.delegate = self; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| let center = UNUserNotificationCenter.current() | ||
| center.requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in | ||
| print("User allowed notifications:", granted) | ||
| if granted { | ||
| DispatchQueue.main.async { | ||
| UIApplication.shared.registerForRemoteNotifications() | ||
| } | ||
| } else { | ||
| if error { | ||
| print(error.localizedDescription) | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| { | ||
| "title": "Update Badge Count", | ||
| "type": "mobile" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| - (void)conversationsClient:(TwilioConversationsClient *)client notificationUpdatedBadgeCount:(NSUInteger)badgeCount { | ||
| [UIApplication.currentApplication setApplicationIconBadgeNumber:badgeCount]; | ||
| } |
3 changes: 3 additions & 0 deletions
3
conversations/push-ios/updatebadgecount/updatebadgecount.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| func conversationsClient(_ client: TwilioConversationsClient, notificationUpdatedBadgeCount badgeCount: UInt) { | ||
| UIApplication.shared.applicationIconBadgeNumber = Int(badgeCount) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| { | ||
| "title": "User Notification Setttings", | ||
| "type": "mobile" | ||
| } |
8 changes: 8 additions & 0 deletions
8
conversations/push-ios/usernotificationsettings/usernotificationsettings.m
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| // Add this to the didFinishLaunchingWithOptions function or a similar place | ||
| // once you get granted permissions | ||
| UNUserNotificationCenter *currentNotificationCenter = [UNUserNotificationCenter currentNotificationCenter]; | ||
| [currentNotificationCenter getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings *settings) { | ||
| if (settings.authorizationStatus == UNAuthorizationStatusAuthorized) { | ||
| [UIApplication.sharedApplication registerForRemoteNotifications]; | ||
| } | ||
| }]; |
8 changes: 8 additions & 0 deletions
8
conversations/push-ios/usernotificationsettings/usernotificationsettings.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| let center = UNUserNotificationCenter.current() | ||
| center.getNotificationSettings { (settings) in | ||
| if settings.authorizationStatus == .authorized { | ||
| DispatchQueue.main.async { | ||
| UIApplication.shared.registerForRemoteNotifications() | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| { | ||
| "type": "mobile", | ||
| "title": "Conversations Push State Variables" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| @property (nonatomic, strong) NSData *updatedPushToken; | ||
| @property (nonatomic, strong) NSDictionary *receivedNotification; | ||
| @property (nonatomic, strong) TwilioConversationsClient *conversationsClient; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| var updatedPushToken: Data? | ||
| var receivedNotification: [AnyHashable: Any]? | ||
| var conversationsClientClient: TwilioConversationsClient? |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.