未调用iOS 10 UNUserNotificationCenterDelegate。推送通知无效 [英] iOS 10 UNUserNotificationCenterDelegate not called. push notifications not working

查看:2913
本文介绍了未调用iOS 10 UNUserNotificationCenterDelegate。推送通知无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

撕毁我的头发,以便在iOS10中使用推送通知。当前设置:

Tearing my hair out tying to get push notifications to work in iOS10. Current setup:

func应用程序中(_ application:UIApplication,didFinishLaunchingWithOptions launchOptions:[UIApplicationLaunchOptionsKey:Any]?) - > Bool

if #available(iOS 10.0, *) {

            let center = UNUserNotificationCenter.current()
            center.delegate = self
            center.requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in

                if error == nil {
                    print("DID REQUEST THE NOTIFICATION")
                    UIApplication.shared.registerForRemoteNotifications()
                }
            }
            print("DID SET DELEGATE")
        }

func应用程序中( _ application:UIApplication,didRegisterForRemoteNotificationsWithDeviceToken deviceToken:Data)

print("DID REGISTER FOR A REMOTE NOTIFICATION AND THE TOKEN IS \(deviceToken.base64EncodedString())"           
let request = UpdatePushNotificationSubscription_Request(deviceToken: deviceToken)
updatePushNotificationSubscriptionWorker.updateSubscription(request)

我已经检查过令牌是否正确上传到后端,确实匹配。

I have checked the token is uploaded to the backend correctly and it does indeed match.

我还实施了:

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

        print("GOT A NOTIFICATION")

    }


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

        //This is for the user tapping on the notification
        print("GOT A NOTIFICATION")
    }

我为所有人设定了权利目标并启用推送:

I have set the entitlements for all targets and enabled push:

现在我试试从后端发送消息,设备什么也没收到。代表们没有被召集。不知道我在这里做错了什么。推送适用于iOS9和Android设备。任何指向我可能做错的指示?

Now when I try to send a message from the backend the device just receives nothing. Delegates are not being called. Have no idea what I'm doing wrong here. Push is working for iOS9 and android devices. Any pointers to what I might be doing wrong?

推荐答案

这个答案适用于iOS 10+,使用 UserNotifications framework。

This answer pertains to iOS 10+, using the UserNotifications framework.

您需要一个类符合 UNUserNotificationCenterDelegate 协议。如果您为此创建一个新类,或者将其添加到 AppDelegate 类中,则无关紧要。我建议创建一个专门的课程。出于这个答案的目的,让我们假设您为它创建一个 UserNotificationController 类。

You need a class to conform to the UNUserNotificationCenterDelegate protocol. It doesn't matter if you create a new class just for this, or add it on to your AppDelegate class. I'd recommend creating a dedicated class though. For the purposes of this answer, let's assume you create a UserNotificationController class for it.

该类可以拥有以下方法:

The class can have the following methods:

可选的func userNotificationCenter(_ center:UNUserNotificationCenter,willPresent通知:UNNotification,withCompletionHandler completionHandler:@escaping(UNNotificationPresentationOptions) - > Void )

可选的func userNotificationCenter(_ center:UNUserNotificationCenter,didReceive响应:UNNotificationResponse,withCompletionHandler completionHandler:@escaping() - > Void)

然后在你的 AppDelegate.application(_:didFinishLaunchingWithOptions :)中方法,您需要将 UNUserNotificationCenter.current()对象的委托设置为<的实例code> UserNotificationController class。您可能希望使用共享实例。

Then in your AppDelegate.application(_:didFinishLaunchingWithOptions:) method, you need to set the delegate on the UNUserNotificationCenter.current() object to an instance of your UserNotificationController class. You'll probably want to use a shared instance.

请求用户授权使用 UNUserNotificationCenter.requestAuthorization启用通知(选项:completionHandler: )方法,并在 completionHandler 中,检查授予的值。如果 true ,请通过调用 UIApplication.shared.registerForRemoteNotifications()注册远程通知。

Request authorization from the user to enable notifications using the UNUserNotificationCenter.requestAuthorization(options:completionHandler:) method, and in the completionHandler, check the granted value. If true, register for remote notifications by calling UIApplication.shared.registerForRemoteNotifications().

现在,当应用收到推送通知时,可能会发生几种不同的情况。我会尝试列出最常见的案例。

Now, when the app receives a push notification, there are several different situations that can happen. I'll try and list the most common cases here.

本地通知:

如果应用程序位于前台,应用程序将调用 UserNotificationController .userNotificationCenter(_:willPresent:withCompletionHandler:)

If the app is in the foreground, the app will call UserNotificationController .userNotificationCenter(_:willPresent:withCompletionHandler:).

如果应用程序在后台(运行与否),则在用户点击通知之前不会调用任何内容,此时应用程序将打开并调用 UserNotificationController .userNotificationCenter(_:didReceive :withCompletionHandler:)

If the app is in the background (running or not), nothing is called until the user taps the notification, at that point, the app will open and call UserNotificationController .userNotificationCenter(_:didReceive:withCompletionHandler:).

远程通知:

有效载荷的内容将影响发生的事情。有效载荷有三种情况,a)正常的警告徽章声音选项b)包括 content-available 选项(设置为 1 true )c)包括 mutable-content 选项(设置为 1 true )。另外,技术上d)你有内容可用可变内容,但这只会触发两种情况。

The content of the payload will affect what happens. There are three cases for the payload, a) just the normal alert, badge, and sound options b) including the content-available option (set to 1 or true) c) including the mutable-content option (set to 1 or true). Plus there's technically d) where you have both content-available and mutable-content, but that just triggers both cases.

对于a)只需提醒声音徽章信息:

For a) just alert, sound, badge info:

此操作与本地通知相同。

This works the same as a local notification.

对于b) content-available == true:

For b) content-available == true:

如果应用程序位于前台,则会调用 UserNotificationController .userNotificationCenter(_:willPresent:withCompletionHandler:)

If the app is in the foreground, UserNotificationController .userNotificationCenter(_:willPresent:withCompletionHandler:) is called.

如果应用程序在后台(运行与否),则调用 AppDelegate.application(_:didReceiveRemoteNotification:fetchCompletionHandler:),而不是你的 UserNotificationController 类中的方法。

If the app is in the background, (running or not), AppDelegate.application(_:didReceiveRemoteNotification:fetchCompletionHandler:) is called, not one of the methods in your UserNotificationController class.

对于c) mutable-content == true:

For c) mutable-content == true:

如果您添加了 UNNotificationServiceExtension 对你的应用程序,它将处理通知并可以修改内容。无论主应用程序的状态如何,都会发生这种情况。如果用户点击了(可选修改的)通知,则会像上面的本地通知一样处理。

If you've added a UNNotificationServiceExtension to your application, it will handle the notification and can modify the content. This happens regardless of the state of your main application. If the (optionally modified) notification is tapped by the user it is then handled like a local notification above.

没有 UNNotificationServiceExtension ,通知被视为上面的普通远程通知。

Without a UNNotificationServiceExtension, the notification is treated like a normal remote notification above.

附加说明:

使用 mutable-content 时,必须在有效负载中包含 alert info,否则系统将将其视为不可变,而不是调用 UNNotificationServiceExtension 。您修改的通知仍必须包含 alert info,否则将使用原始通知有效内容。遗憾的是,没有办法阻止通知出现给用户。

When using mutable-content, you must include alert info in the payload, or the system will treat it as immutable and not call into your UNNotificationServiceExtension. Your modified notification must still include alert info, or the original notification payload will be used. Sadly there's no way to prevent the notification from appearing to the user.

当使用 content-available 时,如果用户上次使用时强制退出应用程序,系统不会重新启动应用程序或调用 AppDelegate.application(_:didReceiveRemoteNotification:fetchCompletionHandler:)。虽然它仍然会显示任何警报,播放声音,并按照有效负载中的指示更新徽章。

When using content-available, if the user force-quit the app the last time they used it, the system will not relaunch the app or call AppDelegate.application(_:didReceiveRemoteNotification:fetchCompletionHandler:). Though it will still display any alert, play the sound, and update the badge as indicated in the payload.

这篇关于未调用iOS 10 UNUserNotificationCenterDelegate。推送通知无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆