Настройка уведомлений
После завершения первоначальной инициализации, необходимо добавить следующие вызовы SDK для подключения push уведомлений:
- Swift
- Objective-C
// MARK: - Remote Notifications
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Threads.threads().applicationDidRegisterForRemoteNotifications(withDeviceToken: deviceToken)
}
// MARK: - UNUserNotificationCenterDelegate
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
// Check for application launched from notification
if response.actionIdentifier == UNNotificationDefaultActionIdentifier {
let userInfo = response.notification.request.content.userInfo
if Threads.threads().isThreadsOriginPushUserInfo(userInfo) {
// Application launched from Threads notification
} else {
// Application launched from other notifications
}
}
completionHandler()
}
#pragma mark - Remote Notifications
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
[[Threads threads] applicationDidRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}
#pragma mark - UNUserNotificationCenterDelegate
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler {
// Check for application launched from notification
if ([response.actionIdentifier isEqualToString: UNNotificationDefaultActionIdentifier]) {
NSDictionary *userInfo = response.notification.request.content.userInfo;
if ([[Threads threads] isThreadsOriginPushUserInfo:userInfo]) {
// Application launched from Threads notification
} else {
// Application launched from other notifications
}
}
completionHandler();
}
Интеграция с Push Lite
примечание
Этот раздел актуален только для проектов, в которых уже используется SDK push-lite для доставки уведомлений.
Для настройки и интеграции push-lite следуйте инструкциям предоставляемым вместе с SDK
Инициализация библиотеки Threads должна идти первой
- Swift
- Objective-C
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Step 1: Configure Threads Framework
let threads = Threads.threads()
threads.registrationAtStartupDisable = SettingsBundleHelper.getRegistrationAtStartupDisable()
threads.isClientIdEncrypted = Configuration.clientIdEncrypted()
threads.isShowsNetworkActivity = true
guard let providerUid = Configuration.providerUid() else { fatalError("Set providerUid for threadsGate transportProtocol.") }
threads.configureTransportProtocol(
with: self,
webSocketURL: URL(string: Configuration.webSocketURL())!,
providerUid: providerUid,
restURL: URL(string: Configuration.restServerURL())!,
dataStoreURL: URL(string: Configuration.dataStoreURL())!
)
// Step 2: Configure PushLite Framework
pushLite = EDNAPushLite(delegate: self)
pushLite.appGroup = "group.io.edna.chatcenter.demo"
pushLite.autoRegisterForNotification = false
pushLite.logEnable = true
pushLite.start()
// Step 3: Register device for remote notifications
...
return true
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Step 1: Configure Threads Framework
Threads *threads = [Threads threads];
threads.isDebugLoggingEnabled = YES;
[threads configureTransportProtocolWithDelegate:self
providerUid:@"PROVIDER_UID"
webSocketURL: [NSURL URLWithString:@"Ссылка на WebSocket API"]
restURL: [NSURL URLWithString:@"Ссылка на REST API"]
dataStoreURL: [NSURL URLWithString:@"Ссылка на DataStore API"]];
// Step 2: Configure PushLite Framework
pushLite = [[EDNAPushLite alloc] initWithDelegate: self];
pushLite.appGroup = @"group.io.edna.chatcenter.demo";
pushLite.autoRegisterForNotification = NO;
pushLite.logEnable = YES;
[pushLite start];
// Step 3: Register device for remote notifications
...
return YES;
}
В таком же порядке передаются значения из делегатов нотификации
- Swift
- Objective-C
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Threads.threads().applicationDidRegisterForRemoteNotifications(withDeviceToken: deviceToken)
pushLite.appDelegate.didRegisterForRemoteNotifications(withDeviceToken: deviceToken)
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
pushLite.appDelegate.didFailToRegisterForRemoteNotifications(withError: error)
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
[[Threads threads] applicationDidRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
[[pushLite appDelegate] didRegisterForRemoteNotificationsWithDeviceToken: deviceToken];
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
[[Threads threads] applicationDidFailToRegisterForRemoteNotificationsWithError:error];
[[pushLite appDelegate] didFailToRegisterForRemoteNotificationsWithError: error];
}