首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未调用UNUserNotificationCenter didReceive

未调用UNUserNotificationCenter didReceive
EN

Stack Overflow用户
提问于 2016-11-23 15:15:53
回答 3查看 7.9K关注 0票数 4

当我发送推送通知,并且应用程序在前台时,将调用willPresent。永远不会调用didReceive。当应用程序在后台并收到推送通知时,将显示警报,但应用程序永远不会调用didReceivewillPresent

在“项目”>“功能”中,我选中了背景模式Location updatesRemote notifications。位置更新适用于不相关的代码。

我还启用了推送通知。这工作得很好,因为它们正在被接收。

我已经在我的AppDelegate中实现了UNUserNotificationCenter通知的东西,如下所示:

代码语言:javascript
复制
import UserNotifications
...

class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
...

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

    // MARK: - Push Notifications

    func registerForPushNotifications(application: UIApplication) {
        let notificationCenter = UNUserNotificationCenter.current()
        notificationCenter.delegate = self
        notificationCenter.requestAuthorization(options: [.badge, .sound, .alert], completionHandler: {(granted, error) in
            if (granted)
            {
                UIApplication.shared.registerForRemoteNotifications()
            }
            else{
                //Do stuff if unsuccessful...
                print("Unsuccessful in registering for push notifications")
            }
        })
    }

    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        //convert the deviceToken to a string
        let deviceTokenString = deviceToken.map { String(format: "%02.2hhx", arguments: [$0]) }.joined()

        let ud = UserDefaults.standard
        ud.set(deviceTokenString, forKey: "deviceToken")
    }

    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        //Handle the notification
        print("User Info = ",notification.request.content.userInfo)
        completionHandler([.alert, .badge, .sound])
    }

    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        //handle the notification
        print("Push notification: ",response.notification.request.content.userInfo)

        ... code to import the notification data into Core Data.

        completionHandler()
    }

...
}

你知道为什么didReceive没有被调用吗?

这是收到的推送通知:

代码语言:javascript
复制
[AnyHashable("aps"): {
    alert = "[name] has added you as a friend.";
    category = "NEWS_CATEGORY";
    "related_user_id" = 55;
    sound = default;
    type = "friend_request";
}]

iOS 10.1,Swift 3。

EN

回答 3

Stack Overflow用户

发布于 2020-04-23 17:14:33

我发现我需要在获得权限后设置委托。

否则,当你第一次运行应用程序时,你在获得权限之前注册了委托,didReceive函数不会被触发-这只发生在你第一次运行应用程序时,在kill并重新启动之后,它被调用得很好。

如下所示:

代码语言:javascript
复制
 UNUserNotificationCenter.current().requestAuthorization(options: authOptions, completionHandler: { (granted, error) in
        if granted {
            // Register after we get the permissions.
            UNUserNotificationCenter.current().delegate = self
        }
    })
票数 8
EN

Stack Overflow用户

发布于 2017-07-16 11:22:55

didReceive不会在您收到通知时执行,而是在用户对收到的通知执行操作时执行

来自https://developer.apple.com/documentation/usernotifications/unusernotificationcenterdelegate上的苹果文档

调用以让您的应用程序知道用户为给定通知选择了哪个操作。

这意味着当用户对接收到的通知执行操作(推送通知、向下滑动等)时,将调用此方法

票数 4
EN

Stack Overflow用户

发布于 2016-11-24 09:28:23

如果您点击通知横幅,将调用willPresent方法。

如果您希望在后台处理推送通知,而不必点击横幅,则应将{"content-available":1}附加到您的通知有效负载。这种类型的推送通知称为静默推送。这将不会像普通推送那样创建通知横幅/警报,您将不得不自己创建横幅。

当您收到带有content-available的推送通知时,会在应用后台调用didReceive委托方法。

注意:您还必须在plist文件的UIBackgroundModes密钥中添加‘远程通知’。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40758200

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档