ios - 本地通知不在 swift 4 中调用 didReceiveRemoteNotifications

标签 ios swift apple-push-notifications swift4 ios11

<分区>

我有以下 appDelegate:

import UIKit
import CoreData
import UserNotifications

@available(iOS 11.0, *)
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate
{
    var window: UIWindow?

    ...

    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any])
    {
        print("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
    }
}

我使用的是 ios11 和 swift 4.1,但通知被正确触发。但是当我点击它时,我无法在应用程序中抓取它。为什么?我正在使用 xcode 9.4.1。谢谢。

该应用程序在后台运行,当点击通知时,该应用程序将打开。

最佳答案

你好@cdub 你应该使用 usernotificatioCenter

 func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

    // do code here to show notification alert in for-ground mode 
}




func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    //your notification handling code here 
    }

关于ios - 本地通知不在 swift 4 中调用 didReceiveRemoteNotifications,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51666206/

相关文章:

ios - 以编程方式在导航栏上设置图像并与 segue 一起显示

ios - IBOutlet UITextField.text 崩溃的大写字符串

ios - scrollViewWillBeginDragging : Not getting called in UICollectionview

ios - 将整数数组转换为titlesForSegmentAtIndex

ios - 开发插入与分销插入

swift - 将选定行的索引传递给另一个 ViewController

javascript - 滚动到 webView 中的 anchor

macos - 在 10.10 上,NSPopover 中的 NSViewController 通过从 swift 中的 NSStatusItem 中转出

ios - 如果用户拒绝推送通知提示的回调方法?

iOS8 - 在有效负载中添加 "sound"属性之前,静默推送通知不起作用