简体   繁体   English

推送通知无法快速运行

[英]Push Notifications Not Working Swift

So when my app is not currently open and it receives a push notification, the "didrecieveremotenotification" works perfectly fine when the banner appears and you press it. 因此,当我的应用当前未打开且收到推送通知时,横幅出现并按下时,“ didrecieveremotenotification”效果很好。 But when the app is open, no banner at all appears. 但是,当应用程序打开时,根本不会显示任何横幅。 I want the push notification to work when the app is open aswell. 我还希望在打开应用程序时也可以使用推送通知。

I tried using UNNotificationCenter but lost push notifications in general. 我尝试使用UNNotificationCenter,但总体上丢失了推送通知。

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    UNUserNotificationCenter.current().delegate = self
    UNUserNotificationCenter.current().requestAuthorization(options: [.badge, .sound, .alert], completionHandler: {(granted, error) in
        if (granted) {
            UIApplication.shared.registerForRemoteNotifications()
        } else{
            print("Notification permissions not granted")
        }
    })
 }


//Called when a notification is delivered to a foreground app.
public func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Swift.Void) {
    completionHandler([.sound, .alert, .badge])
}

//Called to let your app know which action was selected by the user for a given notification.
public func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Swift.Void) {
    completionHandler()
}

Update: UNUserNotificationCenter.current().requestAuthorization runs 更新: UNUserNotificationCenter.current().requestAuthorization运行

func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error)

when permission is granted. 授予权限时。

however when I don't use UNNotificationCenter and use old method it doesn't fail. 但是,当我不使用UNNotificationCenter并使用旧方法时,它不会失败。

Why would one method fail to register for remote notifications but the other not? 为什么一种方法无法注册远程通知,而另一种却不能呢?

Update 2: Was working in simulator lol, that's why it was failing. 更新2:在模拟器中大声笑,这就是为什么它失败了。 However now when I run the UNUserNotificationCenterDelegate methods only the 但是,现在当我运行UNUserNotificationCenterDelegate方法时,仅

public func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive method works and not the public func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive方法起作用,而不是

public func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent

Update 3: I've ran out of possible things to try. 更新3:我已经用尽所有可能的尝试。 What would be stopping the code from firing willPresent but not didReceive . 什么将阻止代码触发willPresentdidReceive This is brutal. 这是残酷的。 Has anyone ever had this issue before? 以前有没有人遇到过这个问题?

Update 4: SOLVED. 更新4:已解决。 The service I'm using for the push notifications requires the user to be disconnected from the service to send the push notification. 我用于推送通知的服务要求用户与服务断开连接以发送推送通知。 It automatically disconnects you when you're in the background, which is why that was working and it wasn't working in the foreground. 当您在后台时,它会自动断开与您的连接,这就是为什么该方法起作用而它在前台不起作用的原因。

Weird way for them to configure that, I'll probably send an email. 对于他们来说,这很奇怪,我可能会发送一封电子邮件。

Implement UNUserNotificationCenterDelegate delegate methods to get notification (tray at top) while app is in foreground. 在应用程序处于前台时,实现UNUserNotificationCenterDelegate委托方法以获取通知(托盘在顶部)。 But it will only work with IOS 10. 但它仅适用于IOS 10。

In your didFinishLaunchingWithOptions method set UNUserNotificationCenterDelegate delegate like this. 在您的didFinishLaunchingWithOptions方法中,像这样设置UNUserNotificationCenterDelegate委托。

UNUserNotificationCenter.current().delegate = self

Implement Delegate methods... 实现委托方法...

//Called when a notification is delivered to a foreground app.
public func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Swift.Void) {
    completionHandler([.sound, .alert, .badge])
}

//Called to let your app know which action was selected by the user for a given notification.
public func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Swift.Void) {
    completionHandler()
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM