使用Image-iOS-Swift推送通知

使用Image-iOS-Swift推送通知,ios,swift,image,push-notification,Ios,Swift,Image,Push Notification,嗨,我只想用图像显示推送通知。我使用下面的代码,我不知道我在哪里犯了错误。这花了我3个多星期的时间,我通过了很多链接,但仍然无法修复。下面是我的应用程序代理代码 AppDelegate.Swift import UIKit import UserNotifications var deviceTokenString:String = "" var badgeCount = 0 @UIApplicationMain class AppDelegate: UIResponder, UIAppli

嗨,我只想用图像显示推送通知。我使用下面的代码,我不知道我在哪里犯了错误。这花了我3个多星期的时间,我通过了很多链接,但仍然无法修复。下面是我的应用程序代理代码

AppDelegate.Swift

import UIKit
import UserNotifications

var deviceTokenString:String = ""
var badgeCount = 0

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {

var window: UIWindow?


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    // Push Notification

    if #available(iOS 10.0, *)
    {
        let center = UNUserNotificationCenter.current()
        center.requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in

            // actions based on whether notifications were authorised or not

            guard error == nil else {

                //Display Error.. Handle Error.. etc..

                return
            }

            if granted
            {

                //Do stuff here..

            }

            else {

                //Handle user denying permissions..

            }

        }

        application.registerForRemoteNotifications()
    } else {
        // Fallback on earlier versions
    }




    registerForRemoteNotification()


    // iOS 10 support
    if #available(iOS 10, *) {
        UNUserNotificationCenter.current().requestAuthorization(options:[.alert, .sound]){ (granted, error) in }
        application.registerForRemoteNotifications()
    }
        // iOS 9 support
    else if #available(iOS 9, *) {
        UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.sound, .alert], categories: nil))
        UIApplication.shared.registerForRemoteNotifications()
    }
        // iOS 8 support
    else if #available(iOS 8, *) {
        UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.sound, .alert], categories: nil))
        UIApplication.shared.registerForRemoteNotifications()
    }
        // iOS 7 support
    else {
        application.registerForRemoteNotifications(matching: [.sound, .alert])
    }

    return true
}

func registerForRemoteNotification() {
    if #available(iOS 10.0, *) {
        let center  = UNUserNotificationCenter.current()
        center.delegate = self
        center.requestAuthorization(options: [.sound, .alert]) { (granted, error) in
            if error == nil{
                UIApplication.shared.registerForRemoteNotifications()

             //   UIApplication.shared.applicationIconBadgeNumber = 5
            }
        }
    }
    else {
        UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.sound, .alert], categories: nil))
        UIApplication.shared.registerForRemoteNotifications()

      //  UIApplication.shared.applicationIconBadgeNumber = 5
    }
}

func incrementBadgeNumberBy(badgeNumberIncrement: Int)
{
    let currentBadgeNumber = UIApplication.shared.applicationIconBadgeNumber
    let updatedBadgeNumber = currentBadgeNumber + badgeNumberIncrement
    if (updatedBadgeNumber > 0)
    {
        UIApplication.shared.applicationIconBadgeNumber = updatedBadgeNumber
    }
    else
    {
        UIApplication.shared.applicationIconBadgeNumber = 0
    }
}

func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
    print("Couldn't register: \(error)")
}

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {

    deviceTokenString = deviceToken.hexString()

    //  deviceTokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)})
    print("device token: \(deviceTokenString)")


}

// Push notification received
func application(_ application: UIApplication, didReceiveRemoteNotification data: [AnyHashable : Any]) {
    // Print notification payload data

    badgeCount = badgeCount + 1
    self.incrementBadgeNumberBy(badgeNumberIncrement: badgeCount)

    print("Push notification received: \(data)")

}

//  Notification will present call back
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    completionHandler([.alert, .sound, .badge])


    print("UserInfo: \(notification.request.content.userInfo)")

    var userinfo = NSDictionary()
    userinfo = notification.request.content.userInfo as NSDictionary

    let imgData = userinfo.value(forKey: "data")! as! NSDictionary

    let url = imgData.value(forKey: "attachment-url")

    let imgUrl = URL(string: url as! String)!

    //  1. Create Notification Content
    let content = UNMutableNotificationContent()


    //  2. Create Notification Attachment

    URLSession.shared.downloadTask(with: imgUrl)
    {(location, response, error) in

        print("location: \(location!)")

        if error == nil
        {
            if let location = location
            {
                // Move temporary file to remove .tmp extension

                let tmpDirectory = NSTemporaryDirectory()

                let tmpFile = "file://".appending(tmpDirectory).appending(imgUrl.lastPathComponent)

                print("tmpFile: \(tmpFile)")

                let tmpUrl = URL(string: tmpFile)!

                print("tmpUrl: \(tmpUrl)")

                try! FileManager.default.moveItem(at: location, to: tmpUrl)

                // Add the attachment to the notification content

                if let attachment = try? UNNotificationAttachment(identifier: "attachment", url: tmpUrl) {

                    content.attachments = [attachment]

                    print("attachment: \(content.attachments)")

                    //  3. Create Notification Request

                    let request = UNNotificationRequest.init(identifier: String.UNNotificationRequest.NormalLocalPush.rawValue,

                                                             content: content, trigger: nil)

                    content.title = "\(userinfo.value(forKeyPath: "aps.alert.title")!)"
                    content.body = "\(userinfo.value(forKeyPath: "aps.alert.body")!)"
                    content.sound = UNNotificationSound.default()
                    content.badge = (UIApplication.shared.applicationIconBadgeNumber + 1) as NSNumber;
                    content.categoryIdentifier = String.UNNotificationCategory.Normal.rawValue

                    //  4. Add to NotificationCenter

                    let center = UNUserNotificationCenter.current()

                    center.add(request)
                }
            }
        }
        else
        {
            print("Error: \(error!)")
        }
        }.resume()
}

@available(iOS 10.0, *)
//  Notification interaction response call back
func userNotificationCenter(_ center: UNUserNotificationCenter,
                            didReceive response: UNNotificationResponse,
                            withCompletionHandler completionHandler: @escaping () -> Void) {

    print("\(response.notification.request.content.userInfo)")

    var userinfo = NSDictionary()
    userinfo = response.notification.request.content.userInfo as NSDictionary

    let imgData = userinfo.value(forKey: "data")! as! NSDictionary

    let url = imgData.value(forKey: "attachment-url")

    let imgUrl = URL(string: url as! String)!

    //  1. Create Notification Content
    let content = UNMutableNotificationContent()
    content.title = "\(userinfo.value(forKeyPath: "aps.alert.title")!)"
    content.body = "\(userinfo.value(forKeyPath: "aps.alert.body")!)"
    content.sound = UNNotificationSound.default()
    content.badge = (UIApplication.shared.applicationIconBadgeNumber + 1) as NSNumber;
    content.categoryIdentifier = String.UNNotificationCategory.Normal.rawValue   //  设置通知类型标示

    //  2. Create Notification Attachment

    URLSession.shared.downloadTask(with: imgUrl) { (location, response, error) in

        if let location = location {

            // Move temporary file to remove .tmp extension

            let tmpDirectory = NSTemporaryDirectory()

            let tmpFile = "file://".appending(tmpDirectory).appending(imgUrl.lastPathComponent)

            let tmpUrl = URL(string: tmpFile)!

            try! FileManager.default.moveItem(at: location, to: tmpUrl)



            // Add the attachment to the notification content

            if let attachment = try? UNNotificationAttachment(identifier: "", url: tmpUrl) {

                content.attachments = [attachment]
            }
        }

        // Serve the notification content

        // self.contentHandler!(content)

        }.resume()

    //        if let attachement = try? UNNotificationAttachment(identifier: "attachment", url: imgUrl, options: nil)
    //        {
    //            content.attachments = [attachement]
    //        }

    //  3. Create Notification Request
    let request = UNNotificationRequest.init(identifier: String.UNNotificationRequest.NormalLocalPush.rawValue,
                                             content: content, trigger: nil)

    //  4. Add to NotificationCenter
    let center = UNUserNotificationCenter.current()
    center.add(request)

    let responseNotificationRequestIdentifier = response.notification.request.identifier

    if responseNotificationRequestIdentifier == String.UNNotificationRequest.NormalLocalPush.rawValue ||
        responseNotificationRequestIdentifier == String.UNNotificationRequest.LocalPushWithTrigger.rawValue ||
        responseNotificationRequestIdentifier == String.UNNotificationRequest.LocalPushWithCustomUI1.rawValue ||
        responseNotificationRequestIdentifier == String.UNNotificationRequest.LocalPushWithCustomUI2.rawValue {

        let actionIdentifier = response.actionIdentifier
        switch actionIdentifier {
        case String.UNNotificationAction.Accept.rawValue:

            break
        case String.UNNotificationAction.Reject.rawValue:

            break
        case String.UNNotificationAction.Input.rawValue:

            break
        case UNNotificationDismissActionIdentifier:

            break
        case UNNotificationDefaultActionIdentifier:

            break
        default:
            break
        }
    }
    completionHandler();
}


}

extension Data
{
func hexString() -> String
{
    return self.reduce("") { string, byte in
        string + String(format: "%02X", byte)
    }
}
}
import Foundation

extension String {

enum UNNotificationAction : String {
    case Accept
    case Reject
    case Input
}

enum UNNotificationCategory : String {
    case Normal
    case Cheer
    case CheerText
}

enum UNNotificationRequest : String {
    case NormalLocalPush
    case LocalPushWithTrigger
    case LocalPushWithCustomUI1
    case LocalPushWithCustomUI2
}
}

extension URL {

enum ResourceType : String {
    case Local
    case Local1
    case Remote
    case AttachmentRemote
}

static func resource(type :ResourceType) -> URL
{
    switch type {
    case .Local:
        return Bundle.main.url(forResource: "cheer", withExtension: "png")!
    case .Local1:
        return Bundle.main.url(forResource: "hahaha", withExtension: "gif")!
    case .Remote:
        return URL(string: "http://ww1.sinaimg.cn/large/65312d9agw1f59leskkcij20cs0csmym.jpg")!
    case .AttachmentRemote:
        return URL(string: "https://assets-cdn.github.com/images/modules/open_graph/github-mark.png")!
    }
    }
}

extension URLSession {

class func downloadImage(atURL url: URL, withCompletionHandler completionHandler: @escaping (Data?, NSError?) -> Void) {
    let dataTask = URLSession.shared.dataTask(with: url) { (data: Data?, response: URLResponse?, error: Error?) in
        completionHandler(data, error as NSError?)
    }
    dataTask.resume()
}
}
下面是我用于自定义推送通知的扩展代码

扩展。swift

import UIKit
import UserNotifications

var deviceTokenString:String = ""
var badgeCount = 0

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {

var window: UIWindow?


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    // Push Notification

    if #available(iOS 10.0, *)
    {
        let center = UNUserNotificationCenter.current()
        center.requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in

            // actions based on whether notifications were authorised or not

            guard error == nil else {

                //Display Error.. Handle Error.. etc..

                return
            }

            if granted
            {

                //Do stuff here..

            }

            else {

                //Handle user denying permissions..

            }

        }

        application.registerForRemoteNotifications()
    } else {
        // Fallback on earlier versions
    }




    registerForRemoteNotification()


    // iOS 10 support
    if #available(iOS 10, *) {
        UNUserNotificationCenter.current().requestAuthorization(options:[.alert, .sound]){ (granted, error) in }
        application.registerForRemoteNotifications()
    }
        // iOS 9 support
    else if #available(iOS 9, *) {
        UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.sound, .alert], categories: nil))
        UIApplication.shared.registerForRemoteNotifications()
    }
        // iOS 8 support
    else if #available(iOS 8, *) {
        UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.sound, .alert], categories: nil))
        UIApplication.shared.registerForRemoteNotifications()
    }
        // iOS 7 support
    else {
        application.registerForRemoteNotifications(matching: [.sound, .alert])
    }

    return true
}

func registerForRemoteNotification() {
    if #available(iOS 10.0, *) {
        let center  = UNUserNotificationCenter.current()
        center.delegate = self
        center.requestAuthorization(options: [.sound, .alert]) { (granted, error) in
            if error == nil{
                UIApplication.shared.registerForRemoteNotifications()

             //   UIApplication.shared.applicationIconBadgeNumber = 5
            }
        }
    }
    else {
        UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.sound, .alert], categories: nil))
        UIApplication.shared.registerForRemoteNotifications()

      //  UIApplication.shared.applicationIconBadgeNumber = 5
    }
}

func incrementBadgeNumberBy(badgeNumberIncrement: Int)
{
    let currentBadgeNumber = UIApplication.shared.applicationIconBadgeNumber
    let updatedBadgeNumber = currentBadgeNumber + badgeNumberIncrement
    if (updatedBadgeNumber > 0)
    {
        UIApplication.shared.applicationIconBadgeNumber = updatedBadgeNumber
    }
    else
    {
        UIApplication.shared.applicationIconBadgeNumber = 0
    }
}

func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
    print("Couldn't register: \(error)")
}

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {

    deviceTokenString = deviceToken.hexString()

    //  deviceTokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)})
    print("device token: \(deviceTokenString)")


}

// Push notification received
func application(_ application: UIApplication, didReceiveRemoteNotification data: [AnyHashable : Any]) {
    // Print notification payload data

    badgeCount = badgeCount + 1
    self.incrementBadgeNumberBy(badgeNumberIncrement: badgeCount)

    print("Push notification received: \(data)")

}

//  Notification will present call back
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    completionHandler([.alert, .sound, .badge])


    print("UserInfo: \(notification.request.content.userInfo)")

    var userinfo = NSDictionary()
    userinfo = notification.request.content.userInfo as NSDictionary

    let imgData = userinfo.value(forKey: "data")! as! NSDictionary

    let url = imgData.value(forKey: "attachment-url")

    let imgUrl = URL(string: url as! String)!

    //  1. Create Notification Content
    let content = UNMutableNotificationContent()


    //  2. Create Notification Attachment

    URLSession.shared.downloadTask(with: imgUrl)
    {(location, response, error) in

        print("location: \(location!)")

        if error == nil
        {
            if let location = location
            {
                // Move temporary file to remove .tmp extension

                let tmpDirectory = NSTemporaryDirectory()

                let tmpFile = "file://".appending(tmpDirectory).appending(imgUrl.lastPathComponent)

                print("tmpFile: \(tmpFile)")

                let tmpUrl = URL(string: tmpFile)!

                print("tmpUrl: \(tmpUrl)")

                try! FileManager.default.moveItem(at: location, to: tmpUrl)

                // Add the attachment to the notification content

                if let attachment = try? UNNotificationAttachment(identifier: "attachment", url: tmpUrl) {

                    content.attachments = [attachment]

                    print("attachment: \(content.attachments)")

                    //  3. Create Notification Request

                    let request = UNNotificationRequest.init(identifier: String.UNNotificationRequest.NormalLocalPush.rawValue,

                                                             content: content, trigger: nil)

                    content.title = "\(userinfo.value(forKeyPath: "aps.alert.title")!)"
                    content.body = "\(userinfo.value(forKeyPath: "aps.alert.body")!)"
                    content.sound = UNNotificationSound.default()
                    content.badge = (UIApplication.shared.applicationIconBadgeNumber + 1) as NSNumber;
                    content.categoryIdentifier = String.UNNotificationCategory.Normal.rawValue

                    //  4. Add to NotificationCenter

                    let center = UNUserNotificationCenter.current()

                    center.add(request)
                }
            }
        }
        else
        {
            print("Error: \(error!)")
        }
        }.resume()
}

@available(iOS 10.0, *)
//  Notification interaction response call back
func userNotificationCenter(_ center: UNUserNotificationCenter,
                            didReceive response: UNNotificationResponse,
                            withCompletionHandler completionHandler: @escaping () -> Void) {

    print("\(response.notification.request.content.userInfo)")

    var userinfo = NSDictionary()
    userinfo = response.notification.request.content.userInfo as NSDictionary

    let imgData = userinfo.value(forKey: "data")! as! NSDictionary

    let url = imgData.value(forKey: "attachment-url")

    let imgUrl = URL(string: url as! String)!

    //  1. Create Notification Content
    let content = UNMutableNotificationContent()
    content.title = "\(userinfo.value(forKeyPath: "aps.alert.title")!)"
    content.body = "\(userinfo.value(forKeyPath: "aps.alert.body")!)"
    content.sound = UNNotificationSound.default()
    content.badge = (UIApplication.shared.applicationIconBadgeNumber + 1) as NSNumber;
    content.categoryIdentifier = String.UNNotificationCategory.Normal.rawValue   //  设置通知类型标示

    //  2. Create Notification Attachment

    URLSession.shared.downloadTask(with: imgUrl) { (location, response, error) in

        if let location = location {

            // Move temporary file to remove .tmp extension

            let tmpDirectory = NSTemporaryDirectory()

            let tmpFile = "file://".appending(tmpDirectory).appending(imgUrl.lastPathComponent)

            let tmpUrl = URL(string: tmpFile)!

            try! FileManager.default.moveItem(at: location, to: tmpUrl)



            // Add the attachment to the notification content

            if let attachment = try? UNNotificationAttachment(identifier: "", url: tmpUrl) {

                content.attachments = [attachment]
            }
        }

        // Serve the notification content

        // self.contentHandler!(content)

        }.resume()

    //        if let attachement = try? UNNotificationAttachment(identifier: "attachment", url: imgUrl, options: nil)
    //        {
    //            content.attachments = [attachement]
    //        }

    //  3. Create Notification Request
    let request = UNNotificationRequest.init(identifier: String.UNNotificationRequest.NormalLocalPush.rawValue,
                                             content: content, trigger: nil)

    //  4. Add to NotificationCenter
    let center = UNUserNotificationCenter.current()
    center.add(request)

    let responseNotificationRequestIdentifier = response.notification.request.identifier

    if responseNotificationRequestIdentifier == String.UNNotificationRequest.NormalLocalPush.rawValue ||
        responseNotificationRequestIdentifier == String.UNNotificationRequest.LocalPushWithTrigger.rawValue ||
        responseNotificationRequestIdentifier == String.UNNotificationRequest.LocalPushWithCustomUI1.rawValue ||
        responseNotificationRequestIdentifier == String.UNNotificationRequest.LocalPushWithCustomUI2.rawValue {

        let actionIdentifier = response.actionIdentifier
        switch actionIdentifier {
        case String.UNNotificationAction.Accept.rawValue:

            break
        case String.UNNotificationAction.Reject.rawValue:

            break
        case String.UNNotificationAction.Input.rawValue:

            break
        case UNNotificationDismissActionIdentifier:

            break
        case UNNotificationDefaultActionIdentifier:

            break
        default:
            break
        }
    }
    completionHandler();
}


}

extension Data
{
func hexString() -> String
{
    return self.reduce("") { string, byte in
        string + String(format: "%02X", byte)
    }
}
}
import Foundation

extension String {

enum UNNotificationAction : String {
    case Accept
    case Reject
    case Input
}

enum UNNotificationCategory : String {
    case Normal
    case Cheer
    case CheerText
}

enum UNNotificationRequest : String {
    case NormalLocalPush
    case LocalPushWithTrigger
    case LocalPushWithCustomUI1
    case LocalPushWithCustomUI2
}
}

extension URL {

enum ResourceType : String {
    case Local
    case Local1
    case Remote
    case AttachmentRemote
}

static func resource(type :ResourceType) -> URL
{
    switch type {
    case .Local:
        return Bundle.main.url(forResource: "cheer", withExtension: "png")!
    case .Local1:
        return Bundle.main.url(forResource: "hahaha", withExtension: "gif")!
    case .Remote:
        return URL(string: "http://ww1.sinaimg.cn/large/65312d9agw1f59leskkcij20cs0csmym.jpg")!
    case .AttachmentRemote:
        return URL(string: "https://assets-cdn.github.com/images/modules/open_graph/github-mark.png")!
    }
    }
}

extension URLSession {

class func downloadImage(atURL url: URL, withCompletionHandler completionHandler: @escaping (Data?, NSError?) -> Void) {
    let dataTask = URLSession.shared.dataTask(with: url) { (data: Data?, response: URLResponse?, error: Error?) in
        completionHandler(data, error as NSError?)
    }
    dataTask.resume()
}
}
我的Api回应是

[AnyHashable("aps"):
 {
alert =     {
    body = test;
    title = "N-Gal";
};
"mutable-content" = 1;
sound = default;
}, 
AnyHashable("data"): 
{
"attachment-url" = "https://www.n-gal.com/image/cache/catalog/HomeBanner/Banners/1172X450-N-Gal-Footwear-Banner-100x100.jpg";
}]

此代码基于教程。请给我一个解决方案来解决这个问题。。。提前谢谢

从您的代码片段中,我断定您所说的是远程通知。这是一个重要的区别。如果您想“丰富”远程通知(例如添加图像),则需要
unnotificationservice扩展名

对于本地通知,应用程序在创建 通知的其余内容向远程服务器添加附件 通知,使用通知服务扩展来修改 发送前通知内容。更多信息 关于实现通知服务扩展,请参阅

来源:。(强调矿山)

该扩展位于应用程序之外,并在用户看到远程通知之前调用。这样,您就有机会在计划发送通知之前加载所有远程资源。有关扩展生命周期的更多信息,以及它们如何与主机应用程序通信,请参阅

要在Xcode中添加扩展名,请转到
File>New>Target
并选择通知服务扩展名:

这将创建新的扩展目标并将其嵌入主机目标:

NotificationService.swift
文件中,您将找到可以开始自定义通知内容的入口点

class NotificationService: UNNotificationServiceExtension {

    var contentHandler: ((UNNotificationContent) -> Void)?
    var bestAttemptContent: UNMutableNotificationContent?

    override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
        self.contentHandler = contentHandler
        bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)

        if let bestAttemptContent = bestAttemptContent {
            // Modify the notification content here...
            bestAttemptContent.title = "\(bestAttemptContent.title) [modified]"

            contentHandler(bestAttemptContent)
        }
    }

    override func serviceExtensionTimeWillExpire() {
        // Called just before the extension will be terminated by the system.
        // Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
        if let contentHandler = contentHandler, let bestAttemptContent =  bestAttemptContent {
            contentHandler(bestAttemptContent)
        }
    }

}

请务必查看以了解更多详细信息。

从您的代码片段中,我断定您所说的是远程通知。这是一个重要的区别。如果您想“丰富”远程通知(例如添加图像),则需要
unnotificationservice扩展名

对于本地通知,应用程序在创建 通知的其余内容向远程服务器添加附件 通知,使用通知服务扩展来修改 发送前通知内容。更多信息 关于实现通知服务扩展,请参阅

来源:。(强调矿山)

该扩展位于应用程序之外,并在用户看到远程通知之前调用。这样,您就有机会在计划发送通知之前加载所有远程资源。有关扩展生命周期的更多信息,以及它们如何与主机应用程序通信,请参阅

要在Xcode中添加扩展名,请转到
File>New>Target
并选择通知服务扩展名:

这将创建新的扩展目标并将其嵌入主机目标:

NotificationService.swift
文件中,您将找到可以开始自定义通知内容的入口点

class NotificationService: UNNotificationServiceExtension {

    var contentHandler: ((UNNotificationContent) -> Void)?
    var bestAttemptContent: UNMutableNotificationContent?

    override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
        self.contentHandler = contentHandler
        bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)

        if let bestAttemptContent = bestAttemptContent {
            // Modify the notification content here...
            bestAttemptContent.title = "\(bestAttemptContent.title) [modified]"

            contentHandler(bestAttemptContent)
        }
    }

    override func serviceExtensionTimeWillExpire() {
        // Called just before the extension will be terminated by the system.
        // Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
        if let contentHandler = contentHandler, let bestAttemptContent =  bestAttemptContent {
            contentHandler(bestAttemptContent)
        }
    }

}

请务必查看以了解更多详细信息。

您能告诉我它失败的地方吗?它是否收到来自APNS的推送通知?是否可以创建没有映像的本地通知?图像下载了吗?我没有收到任何错误,我收到了推送通知,但是没有图像,图像正在下载,文件路径类似,file:///private/var/mobile/Containers/Data/Application/552F6491-A95C-463E-8924-FD8BD53A629D/tmp/NG0330-(1) -100x100.jpg你能告诉我哪里出了问题吗?它是否收到来自APNS的推送通知?是否可以创建没有映像的本地通知?图像下载了吗?我没有收到任何错误,我收到了推送通知,但是没有图像,图像正在下载,文件路径类似,file:///private/var/mobile/Containers/Data/Application/552F6491-A95C-463E-8924-FD8BD53A629D/tmp/NG0330-(1) -100x100.jpgThanks。。。。。我已经创建了一个服务扩展,但是我犯了一个小的关键错误。无论如何,谢谢你的回答。。向上投票!为了得到一个简洁的答案…我正在使用它,但我在应用程序委托类中收到了通知。如何重定向NotificationService类方法。谢谢。。。。。我已经创建了一个服务扩展,但是我犯了一个小的关键错误。无论如何,谢谢你的回答。。向上投票!为了得到一个简洁的答案…我正在使用它,但我在应用程序委托类中收到了通知。如何重定向NotificationService类方法。