diff --git a/conversations/push-ios/didfinishlaunching/didfinishlaunching.m b/conversations/push-ios/didfinishlaunching/didfinishlaunching.m new file mode 100644 index 000000000..ee60d7aba --- /dev/null +++ b/conversations/push-ios/didfinishlaunching/didfinishlaunching.m @@ -0,0 +1 @@ +- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions diff --git a/conversations/push-ios/didfinishlaunching/didfinishlaunching.swift b/conversations/push-ios/didfinishlaunching/didfinishlaunching.swift new file mode 100644 index 000000000..8bb111a05 --- /dev/null +++ b/conversations/push-ios/didfinishlaunching/didfinishlaunching.swift @@ -0,0 +1,2 @@ +func application(_ application: UIApplication, didFinishLaunchingWithOptions + launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool diff --git a/conversations/push-ios/didfinishlaunching/meta.json b/conversations/push-ios/didfinishlaunching/meta.json new file mode 100644 index 000000000..da4ae7dfd --- /dev/null +++ b/conversations/push-ios/didfinishlaunching/meta.json @@ -0,0 +1,4 @@ +{ + "title": "Did Finish Launching", + "type": "mobile" +} diff --git a/conversations/push-ios/didreceiveremote/didreceiveremote.m b/conversations/push-ios/didreceiveremote/didreceiveremote.m new file mode 100644 index 000000000..4ced24c2a --- /dev/null +++ b/conversations/push-ios/didreceiveremote/didreceiveremote.m @@ -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; + } +} diff --git a/conversations/push-ios/didreceiveremote/didreceiveremote.swift b/conversations/push-ios/didreceiveremote/didreceiveremote.swift new file mode 100644 index 000000000..f630f6bc1 --- /dev/null +++ b/conversations/push-ios/didreceiveremote/didreceiveremote.swift @@ -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 + } +} diff --git a/conversations/push-ios/didreceiveremote/meta.json b/conversations/push-ios/didreceiveremote/meta.json new file mode 100644 index 000000000..054b21abd --- /dev/null +++ b/conversations/push-ios/didreceiveremote/meta.json @@ -0,0 +1,4 @@ +{ + "title": "Did Receive Notification", + "type": "mobile" +} diff --git a/conversations/push-ios/registernotifications/meta.json b/conversations/push-ios/registernotifications/meta.json new file mode 100644 index 000000000..22c6063ff --- /dev/null +++ b/conversations/push-ios/registernotifications/meta.json @@ -0,0 +1,4 @@ +{ + "title": "Register Notifications", + "type": "mobile" +} diff --git a/conversations/push-ios/registernotifications/registernotifications.m b/conversations/push-ios/registernotifications/registernotifications.m new file mode 100644 index 000000000..862493fb1 --- /dev/null +++ b/conversations/push-ios/registernotifications/registernotifications.m @@ -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? + } + }]; +} diff --git a/conversations/push-ios/registernotifications/registernotifications.swift b/conversations/push-ios/registernotifications/registernotifications.swift new file mode 100644 index 000000000..06cc6e1da --- /dev/null +++ b/conversations/push-ios/registernotifications/registernotifications.swift @@ -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? + } + } +} diff --git a/conversations/push-ios/remotenotifications/meta.json b/conversations/push-ios/remotenotifications/meta.json new file mode 100644 index 000000000..773bd1b2e --- /dev/null +++ b/conversations/push-ios/remotenotifications/meta.json @@ -0,0 +1,4 @@ +{ + "title": "Store Registration", + "type": "mobile" +} diff --git a/conversations/push-ios/remotenotifications/remotenotifications.m b/conversations/push-ios/remotenotifications/remotenotifications.m new file mode 100644 index 000000000..df73e7084 --- /dev/null +++ b/conversations/push-ios/remotenotifications/remotenotifications.m @@ -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; +} diff --git a/conversations/push-ios/remotenotifications/remotenotifications.swift b/conversations/push-ios/remotenotifications/remotenotifications.swift new file mode 100644 index 000000000..db9049a74 --- /dev/null +++ b/conversations/push-ios/remotenotifications/remotenotifications.swift @@ -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 +} diff --git a/conversations/push-ios/systemversion/meta.json b/conversations/push-ios/systemversion/meta.json new file mode 100644 index 000000000..727a4586e --- /dev/null +++ b/conversations/push-ios/systemversion/meta.json @@ -0,0 +1,4 @@ +{ + "title": "Notification Types", + "type": "mobile" +} diff --git a/conversations/push-ios/systemversion/systemversion.m b/conversations/push-ios/systemversion/systemversion.m new file mode 100644 index 000000000..de4b6fac0 --- /dev/null +++ b/conversations/push-ios/systemversion/systemversion.m @@ -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; diff --git a/conversations/push-ios/systemversion/systemversion.swift b/conversations/push-ios/systemversion/systemversion.swift new file mode 100644 index 000000000..df0c3ce72 --- /dev/null +++ b/conversations/push-ios/systemversion/systemversion.swift @@ -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) + } + } +} diff --git a/conversations/push-ios/updatebadgecount/meta.json b/conversations/push-ios/updatebadgecount/meta.json new file mode 100644 index 000000000..6219d1a4f --- /dev/null +++ b/conversations/push-ios/updatebadgecount/meta.json @@ -0,0 +1,4 @@ +{ + "title": "Update Badge Count", + "type": "mobile" +} diff --git a/conversations/push-ios/updatebadgecount/updatebadgecount.m b/conversations/push-ios/updatebadgecount/updatebadgecount.m new file mode 100644 index 000000000..ab3d86ea7 --- /dev/null +++ b/conversations/push-ios/updatebadgecount/updatebadgecount.m @@ -0,0 +1,3 @@ +- (void)conversationsClient:(TwilioConversationsClient *)client notificationUpdatedBadgeCount:(NSUInteger)badgeCount { + [UIApplication.currentApplication setApplicationIconBadgeNumber:badgeCount]; +} diff --git a/conversations/push-ios/updatebadgecount/updatebadgecount.swift b/conversations/push-ios/updatebadgecount/updatebadgecount.swift new file mode 100644 index 000000000..ce3c01809 --- /dev/null +++ b/conversations/push-ios/updatebadgecount/updatebadgecount.swift @@ -0,0 +1,3 @@ +func conversationsClient(_ client: TwilioConversationsClient, notificationUpdatedBadgeCount badgeCount: UInt) { + UIApplication.shared.applicationIconBadgeNumber = Int(badgeCount) +} diff --git a/conversations/push-ios/usernotificationsettings/meta.json b/conversations/push-ios/usernotificationsettings/meta.json new file mode 100644 index 000000000..b2bc847c2 --- /dev/null +++ b/conversations/push-ios/usernotificationsettings/meta.json @@ -0,0 +1,4 @@ +{ + "title": "User Notification Setttings", + "type": "mobile" +} diff --git a/conversations/push-ios/usernotificationsettings/usernotificationsettings.m b/conversations/push-ios/usernotificationsettings/usernotificationsettings.m new file mode 100644 index 000000000..82b678e30 --- /dev/null +++ b/conversations/push-ios/usernotificationsettings/usernotificationsettings.m @@ -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]; + } +}]; diff --git a/conversations/push-ios/usernotificationsettings/usernotificationsettings.swift b/conversations/push-ios/usernotificationsettings/usernotificationsettings.swift new file mode 100644 index 000000000..c9c02df8d --- /dev/null +++ b/conversations/push-ios/usernotificationsettings/usernotificationsettings.swift @@ -0,0 +1,8 @@ +let center = UNUserNotificationCenter.current() +center.getNotificationSettings { (settings) in + if settings.authorizationStatus == .authorized { + DispatchQueue.main.async { + UIApplication.shared.registerForRemoteNotifications() + } + } +} diff --git a/conversations/push-ios/variables/meta.json b/conversations/push-ios/variables/meta.json new file mode 100644 index 000000000..f65db89fa --- /dev/null +++ b/conversations/push-ios/variables/meta.json @@ -0,0 +1,4 @@ +{ + "type": "mobile", + "title": "Conversations Push State Variables" +} diff --git a/conversations/push-ios/variables/variables.m b/conversations/push-ios/variables/variables.m new file mode 100644 index 000000000..74ee928eb --- /dev/null +++ b/conversations/push-ios/variables/variables.m @@ -0,0 +1,3 @@ +@property (nonatomic, strong) NSData *updatedPushToken; +@property (nonatomic, strong) NSDictionary *receivedNotification; +@property (nonatomic, strong) TwilioConversationsClient *conversationsClient; diff --git a/conversations/push-ios/variables/variables.swift b/conversations/push-ios/variables/variables.swift new file mode 100644 index 000000000..468189d3d --- /dev/null +++ b/conversations/push-ios/variables/variables.swift @@ -0,0 +1,3 @@ +var updatedPushToken: Data? +var receivedNotification: [AnyHashable: Any]? +var conversationsClientClient: TwilioConversationsClient?