gpt4 book ai didi

swift - 当应用程序处于前台时,在 iOS 10 上未收到推送

转载 作者:行者123 更新时间:2023-11-28 06:33:26 24 4
gpt4 key购买 nike

昨天我将我的 2 个应用程序更新到了 swift 3。

func application(_ application: UIApplication, didRegister notificationSettings: UIUserNotificationSettings) {
UIApplication.shared.registerForRemoteNotifications()
}

func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
print(error.localizedDescription)
}



func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
// Save Installation if registered successfully
}


//MARK: Recieved Notification
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
print("received a notification")

}

所以后台推送通知在两个应用程序上都能正常工作,但在我的一个应用程序中它没有收到任何前台推送,也就是说,永远不会调用 didReceiveRemoteNotification

我检查过的东西,功能中启用的推送通知

使用此代码在两个应用程序上注册推送通知

UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.badge, .alert, .sound], categories: nil))

不仅如此,我已经为 iOS 10 尝试了 UNUserNotificationCenterDelegate,但仍然没有调用它的委托(delegate)函数。

这仅在 iOS 10 手机上不起作用,iOS 8 和 9 就像魅力一样。所以我不太确定为什么当应用程序打开时它从不在 ios 10 上的 2 个 swift 3 应用程序之一中调用 didReceiveRemoteNotification

这是我试过的 iOS 10 代码

//Added in didFinishLaunchingWithOptions
if #available(iOS 10.0, *) {
let center = UNUserNotificationCenter.current()
center.delegate = self
center.requestAuthorization(options: [.sound, .alert, .badge]) { (granted, error) in
if error == nil{
UIApplication.shared.registerForRemoteNotifications()
}
}
}

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

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

最佳答案

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

func initializeNotificationServices() -> Void {

if #available(iOS 10.0, *) {

UNUserNotificationCenter.currentNotificationCenter().delegate = self
UNUserNotificationCenter.currentNotificationCenter().requestAuthorizationWithOptions([.Badge, .Sound, .Alert]) { (granted, error) in
if granted {
//self.registerCategory()
//self.scheduleNotification("test", interval: 3)
//self.scheduleNotification("test2", interval: 5)
let types : UIUserNotificationType = [.Badge, .Sound, .Alert]
let mySettings : UIUserNotificationSettings = UIUserNotificationSettings.init(forTypes: types, categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(mySettings)
UIApplication.sharedApplication().registerForRemoteNotifications()

}
}
}
else {

// Fallback on earlier versions
let settings = UIUserNotificationSettings(forTypes: [.Sound, .Alert, .Badge], categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)

// This is an asynchronous method to retrieve a Device Token
// Callbacks are in AppDelegate.swift
// Success = didRegisterForRemoteNotificationsWithDeviceToken
// Fail = didFailToRegisterForRemoteNotificationsWithError
UIApplication.sharedApplication().registerForRemoteNotifications()
}
}

@available(iOS 10.0, *)
func userNotificationCenter(center: UNUserNotificationCenter, willPresentNotification notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) {
print("willPresent")
completionHandler([.Badge, .Alert, .Sound])
}

@available(iOS 10.0, *)
func userNotificationCenter(center: UNUserNotificationCenter, didReceiveNotificationResponse response: UNNotificationResponse, withCompletionHandler completionHandler: () -> Void) {
self.didReceiveBookingAppRemoteNotification(response.notification.request.content.userInfo)
print("didReceive == >> \(response.notification.request.content.userInfo)")
completionHandler()
}


//for Lower to ios 10 version
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
self.getNotifInfo(userInfo)
}

确保您必须启用来自项目的推送通知Target => Capabilities 并且你必须添加它的框架 UserNotifications.framework

关于swift - 当应用程序处于前台时,在 iOS 10 上未收到推送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39677939/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com