如何解决 'Notifications not accepted. You can turn them on later under your iOS settings' 问题?

How Can I Solve 'Notifications not accepted. You can turn them on later under your iOS settings' Issue?

我正在使用 onesignal 进行推送通知,在 ios 14 升级之前它一直运行良好。当我在 Xcode 12 上构建没有任何更改的相同应用程序时,我在控制台中收到此警告。

Notifications not accepted. You can turn them on later under your iOS settings

iOS13没问题,更新到iOS14就出问题了

AppDelegate.swift

import UIKit
import CoreData
import Firebase
import GoogleMobileAds
import OneSignal
import UserNotifications
import SDWebImageWebPCoder

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, OSPermissionObserver, OSSubscriptionObserver {
    
    var window: UIWindow?
    var shortcutItemToProcess: UIApplicationShortcutItem?
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        
        FirebaseApp.configure()
        
        GADMobileAds.sharedInstance().start(completionHandler: nil)
        
        let onesignalInitSettings = [kOSSettingsKeyAutoPrompt: false, kOSSettingsKeyInAppLaunchURL: false]
        
        OneSignal.initWithLaunchOptions(launchOptions,
                                        appId: "my key is here",
                                        handleNotificationAction: nil,
                                        settings: onesignalInitSettings)
        OneSignal.inFocusDisplayType = OSNotificationDisplayType.notification
        OneSignal.promptForPushNotifications(userResponse: { accepted in
           print("User accepted notifications: \(accepted)")
         })
        
        // Add your AppDelegate as an obsserver
        OneSignal.add(self as OSPermissionObserver)
        
        OneSignal.add(self as OSSubscriptionObserver)
        
        registerForPushNotifications()
        
        let WebPCoder = SDImageWebPCoder.shared
        SDImageCodersManager.shared.addCoder(WebPCoder)
        
        
        return true
    }
    
    
    func registerForPushNotifications() {
        
        let notificationCenter = UNUserNotificationCenter.current()
        notificationCenter.delegate = self
        
        let readAction = UNNotificationAction(identifier: "oku", title: "Haberi Oku", options: [.foreground])
        let closeAction = UNNotificationAction(identifier: "kapat", title: "Kapat", options: [])
        
        let category = UNNotificationCategory(identifier: "etkilesim", actions: [readAction, closeAction], intentIdentifiers: [], options: [])
        
        notificationCenter.setNotificationCategories([category])
    }
    
    func onOSPermissionChanged(_ stateChanges: OSPermissionStateChanges!) {
        if stateChanges.from.status == OSNotificationPermission.notDetermined {
            if stateChanges.to.status == OSNotificationPermission.authorized {
                print("Thanks for accepting notifications!")
            } else if stateChanges.to.status == OSNotificationPermission.denied {
                print("Notifications not accepted. You can turn them on later under your iOS settings.")
            }
        }
    }
    
    func onOSSubscriptionChanged(_ stateChanges: OSSubscriptionStateChanges!) {
        if !stateChanges.from.subscribed && stateChanges.to.subscribed {
            print("Subscribed for OneSignal push notifications!")
        }
    }
    
    // MARK: UISceneSession Lifecycle
    
    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        // Called when a new scene session is being created.
        // Use this method to select a configuration to create the new scene with.
        
        // Grab a reference to the shortcutItem to use in the scene
        if let shortcutItem = options.shortcutItem {
            shortcutItemToProcess = shortcutItem
        }
        
        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
    }
    
    func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
        // Called when the user discards a scene session.
        // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
        // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
    }
    

extension AppDelegate: UNUserNotificationCenterDelegate {
    
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        
        completionHandler([.alert, .sound])
    }
    
    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        
        var postId:String = ""
        var postType:String = ""
        
        if let custom = response.notification.request.content.userInfo["custom"] as? NSDictionary{
            if let a = custom["a"] as? NSDictionary{
                if let id = a["id"] as? String{
                    postId = id
                }
                if let type = a["rights"] as? String{
                    postType = type
                }
            }
        }
        
        if response.actionIdentifier == "oku" {
            if postId != ""{
                DispatchQueue.main.async(execute: {
                    NotificationCenter.default.post(name: NSNotification.Name("Detail"), object: nil, userInfo: ["id": postId, "type": postType])
                })
                completionHandler()
            }
        }else if response.actionIdentifier == "kapat" {
            print("KAPAT")
            completionHandler()
        } else {
            if postId != ""{
                DispatchQueue.main.async(execute: {
                    NotificationCenter.default.post(name: NSNotification.Name("Detail"), object: nil, userInfo: ["id": postId, "type": postType])
                })
                completionHandler()
            }
        }
    }
    
}

我解决了! :) 如果您的应用程序名称包含 non-English 个字符,请在构建设置下更改您的产品名称并重新构建它,就是这样:)

Settings

然后你可以在info.plist中更改'Bundle display name'。