Swift

IOS 发送 带图片的 本地消息

// ローカル通知
            let content = UNMutableNotificationContent()
            content.title = "通知"
            content.body = "This is a message"
            content.sound = .default
            
            // 加载本地的一张图片作为附件
            if let url = Bundle.main.url(forResource: "icon-76", withExtension: "png") {
                if let attch = try? UNNotificationAttachment(identifier: "identifierAttachment", url: url, options: nil) {
                    content.attachments = [attch]
                }

            }
            // トリガー
            let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 0.1, repeats: false)
            let requestIdentifier = "com.localNotification." + String(Date().timeIntervalSince1970)

            // 通知リクエスト
            let request = UNNotificationRequest(identifier: requestIdentifier,
                content: content, trigger: trigger)

            // 通知メッセージを送信
            UNUserNotificationCenter.current().add(request) { error in
                if error == nil {
                    print("Time Interval Notification scheduled: \(requestIdentifier)")
                } else {
                    print("Time error: \(String(describing: error))")
                }
            }