首页 > 解决方案 > 当用户点击 iOS 本地通知上的查看按钮时,我将如何设置视图?

问题描述

我想在我的 iOS 应用程序上设置我的本地通知,以便当用户向左滑动然后点击查看时,通知上的消息会显示在出现的视图中。我想这样做是因为如果消息很长,则会将其截断,以便通知中仅显示消息的第一部分。

这是我的代码。它的工作原理和我知道的一样多。当用户点击 View 按钮时,它只显示一个空白视图。

let content = UNMutableNotificationContent()
content.categoryIdentifier = "HELLO"
content.title = "Hello World!"
content.body = "Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda."

if #available(iOS 12.0, *) {
    content.summaryArgument = "summaryArgument"
    content.summaryArgumentCount = 5
} else {
    // Fallback on earlier versions
}

var dateComponents = DateComponents()
dateComponents.second = 0
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
// Create the request
let uuidString = UUID().uuidString
let request = UNNotificationRequest(identifier: uuidString,
                                    content: content, trigger: trigger)

// Schedule the request with the system.
center.add(request) { (error) in
    if error != nil {
        print("error=", error?.localizedDescription as Any)
    }
}

标签: iosnotificationsunusernotificationcenterusernotificationsunnotificationrequest

解决方案


如果问题是标准警报的正文太长,请使用 UNNotificationContentExtension 提供辅助接口。这样,作为警报的一部分,用户可以在那里看到整个身体。

如果问题是您希望在用户点击 View 时在应用程序中收到通知,请将自己设置为 UNUserNotificationCenterDelegate 并实施userNotificationCenter(_:didReceive:withCompletionHandler:)


推荐阅读