use*_*978 3 notifications ios swift
我试图在一段时间后(例如 10 秒)发送本地通知。我想将图像添加到xcassets的通知中,但imageURL始终如此nil。
我尝试创建一个路径,然后从该路径创建一个 URL。我还尝试使用forResource参数 =创建一个 url nil,以便它找到任何"png"扩展名为 - 的图像nil。
let content = UNMutableNotificationContent()
content.title = "Notification title"
content.subtitle = "notification subtitle"
content.body = "notification body"
let imageName = "delete"
guard let imageURL = Bundle.main.url(forResource: imageName, withExtension: "png") else {return}
let attachment = try! UNNotificationAttachment(identifier: imageName, url: imageURL, options: .none)
content.attachments = [attachment]
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 10, repeats: false)
let request = UNNotificationRequest(identifier: "notification.id.01", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
Run Code Online (Sandbox Code Playgroud)
我添加了print语句来查看会发生什么,并且我已确认我的方法在该guard语句处返回。
图像可以位于两个位置之一:资产目录或捆绑包。
\n\n当你说
\n\nguard let imageURL = Bundle.main.url(forResource: imageName, withExtension: "png") else {return}\nRun Code Online (Sandbox Code Playgroud)\n\n您正在寻找捆绑包中的图像。
\n\n但你自己告诉我们,那不是图像所在的地方!它位于资产目录中。
\n\n这就是为什么在捆绑包中寻找它失败的原因。它不在那里\xe2\x80\x99t。
\n| 归档时间: |
|
| 查看次数: |
3709 次 |
| 最近记录: |