Существует недостаток хорошо структурированной информации в Интернете, хотя это очень хорошая функция, реализованная в серьезных приложениях для мессенджеров.
Вы должны начать с UNNotificationContentExtension
, чтобы показать пользовательский интерфейс для полученного Push-уведомления. Возьмите любой доступный пример в Интернете и реализуйте его так, как хотите. Обратите внимание на идентификатор пакета - это должно быть com.yourapp.yourextension
. Когда закончите, у вас будет основное приложение и виджет расширения в Xcode.
В главном приложении настройки регистрации для Push-уведомления в iOS10 образом:
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) {
(granted, error) in
guard granted else { return }
let replyAction = UNTextInputNotificationAction(identifier: "ReplyAction", title: "Reply", options: [])
let openAppAction = UNNotificationAction(identifier: "OpenAppAction", title: "Open app", options: [.foreground])
let quickReplyCategory = UNNotificationCategory(identifier: "QuickChat", actions: [replyAction, openAppAction], intentIdentifiers: [], options: [])
UNUserNotificationCenter.current().setNotificationCategories([quickReplyCategory])
UNUserNotificationCenter.current().getNotificationSettings { (settings) in
guard settings.authorizationStatus == .authorized else { return }
UIApplication.shared.registerForRemoteNotifications()
}
}
Все волшебство происходит в UNTextInputNotificationAction
пользовательских действий, которые вы добавляете в Push-уведомления обработчика.
Для завершения настройки Push-уведомления добавить эти параметры в вашем расширенияInfo.plist
: NSExtension -> NSExtensionAttributes -> UNNotificationExtensionCategory: "QuickReply"
Это все о настройке. Для того, чтобы попробовать, использовать Pusher
инструмент и настроить Push Notification таким образом:
{
"aps": {
"alert":"Trigger quick reply",
"category":"QuickReply"
}
}
По крайней мере, вы должны поймать уведомление в виджете. Это происходит в func didReceive(_ notification: UNNotification)
в вашем виджете классе:
func didReceive(_ notification: UNNotification) {
let message = notification.request.content.body
let userInfo = notification.request.content.userInfo
// populate data from received Push Notification in your widget UI...
}
И если пользователь отвечает на полученные Push Notification, виджет будет вызывать следующую функцию обратного вызова:
func didReceive(_ response: UNNotificationResponse, completionHandler completion: @escaping (UNNotificationContentExtensionResponseOption) -> Void) {
if response.actionIdentifier == "ReplyAction" {
if let textResponse = response as? UNTextInputNotificationResponse {
// Do whatever you like with user text response...
completion(.doNotDismiss)
return
}
}
completion(.dismiss)
}