可以覆盖iOS上的现有推送通知

Ric*_*ect 2 notifications ios cordova

在Android中,如果您继续使用相同的通知ID,则可以覆盖现有的推送通知.

iOS在任何方面都是一样的吗?

似乎很难找到有关替换推送通知的任何信息,因为很多答案都使用静默推送通知并手动删除它们.

我使用Cordova,因此在接收推送通知时,我对后台进程的选项有限.

在iOS上,当应用程序在后台时,我无法运行代码来手动删除任何推送通知.

Has*_*sya 6

不,在iOS中,您无法覆盖已安排的远程/本地通知.

您必须安排另一个推送通知.

通过维护一些标志或检查键/值.您必须在恢复新通知时删除以前的通知.

希望这可以帮助.

更新

作为替代方案,一旦您收到推送通知,则根据您在推送通知有效负载中收到的信息.

您可以安排本地通知.

import UIKit
import PushKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate,PKPushRegistryDelegate {

var window: UIWindow?
let notificationObject = UILocalNotification()

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {


    return true
}

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {



    notificationObject.fireDate = NSDate(timeIntervalSinceNow: 1)
    notificationObject.alertBody =  "Title"
    notificationObject.alertAction = "Open"
    notificationObject.soundName = "SoundFile.mp3"
    notificationObject.category = ""
    notificationObject.userInfo = "As per payload you receive"

    UIApplication.sharedApplication().scheduleLocalNotification(notificationObjectCall)


}
Run Code Online (Sandbox Code Playgroud)

Swift 4.2代码

import UIKit
import PushKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, PKPushRegistryDelegate{
    func pushRegistry(_ registry: PKPushRegistry, didUpdate pushCredentials: PKPushCredentials, for type: PKPushType) {
        <#code#>
    }


var window: UIWindow?

let notificationObject = UILocalNotification()


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    return true
}

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {



    notificationObject.fireDate = NSDate(timeIntervalSinceNow: 1) as Date
    notificationObject.alertBody =  "Title"
    notificationObject.alertAction = "Open"
    notificationObject.soundName = "SoundFile.mp3"
    notificationObject.category = ""
    notificationObject.userInfo = "As per payload you receive"

    UIApplication.shared.scheduleLocalNotification(notificationObject)


}
Run Code Online (Sandbox Code Playgroud)

正如您在上面的代码中看到的那样,本地通知对象是全局声明的.因此,每当您收到有效负载时,该对象将一次又一次地被覆盖.

对于远程通知,您无法创建对象并覆盖它.

我不太了解cordova,但在原生iOS中,通过这种方式,您可以使用本地通知进行覆盖.

希望您了解使用了哪种技术并帮助您找到解决方案.