Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/113.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Swift FIRMessaging在注册时发送推送通知请求_Ios_Swift_Firebase_Firebase Cloud Messaging_Firebase Notifications - Fatal编程技术网

Ios Swift FIRMessaging在注册时发送推送通知请求

Ios Swift FIRMessaging在注册时发送推送通知请求,ios,swift,firebase,firebase-cloud-messaging,firebase-notifications,Ios,Swift,Firebase,Firebase Cloud Messaging,Firebase Notifications,我想知道,在用户在我的应用程序上注册为用户后,而不是在用户默认打开我的应用程序(甚至不是用户)时,我如何用众所周知的“应用程序”提示用户向您发送推送通知 我该怎么做?我以为您只会在appDelegate.swift中配置推送通知设置?提前谢谢 您可以浏览Firebase概述的所有远程通知配置,但可以跳过提示用户“应用程序希望向您发送推送通知”的步骤。然后,当用户完成注册过程后,从iOS请求推送通知授权 下面的代码显示了将向用户显示系统提示的关键部分。可以随时打电话给他们。这些也是在应用程序启动时

我想知道,在用户在我的应用程序上注册为用户后,而不是在用户默认打开我的应用程序(甚至不是用户)时,我如何用众所周知的“应用程序”提示用户向您发送推送通知


我该怎么做?我以为您只会在appDelegate.swift中配置推送通知设置?提前谢谢

您可以浏览Firebase概述的所有远程通知配置,但可以跳过提示用户“应用程序希望向您发送推送通知”的步骤。然后,当用户完成注册过程后,从iOS请求推送通知授权

下面的代码显示了将向用户显示系统提示的关键部分。可以随时打电话给他们。这些也是在应用程序启动时需要省略的相同代码行

func turnOnNotifications() {
    if #available(iOS 10.0, *) {
        UNUserNotificationCenter
            .currentNotificationCenter()
            .requestAuthorizationWithOptions([.Alert, .Badge, .Sound]) { authorized, error in
        }
    } else {
        let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: .None)
        UIApplication.sharedApplication().registerUserNotificationSettings(settings)
    }
}

您可以浏览Firebase概述的所有远程通知配置,但可以跳过提示用户“应用程序希望向您发送推送通知”的步骤。然后,当用户完成注册过程后,从iOS请求推送通知授权

下面的代码显示了将向用户显示系统提示的关键部分。可以随时打电话给他们。这些也是在应用程序启动时需要省略的相同代码行

func turnOnNotifications() {
    if #available(iOS 10.0, *) {
        UNUserNotificationCenter
            .currentNotificationCenter()
            .requestAuthorizationWithOptions([.Alert, .Badge, .Sound]) { authorized, error in
        }
    } else {
        let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: .None)
        UIApplication.sharedApplication().registerUserNotificationSettings(settings)
    }
}

你可以在任何你想要的地方请求推送通知的权限,如果你的应用程序有一个登录页面,那么在登录后你肯定需要这样做

您需要首先将请求权限的代码放入
AppDelegate.swift
中的函数中

func registerUserNotification() {


        if #available(iOS 10.0, *) {
            let authOptions : UNAuthorizationOptions = [.alert, .badge, .sound]

            UNUserNotificationCenter.current().requestAuthorization(options: authOptions, completionHandler: { (bool, error) in

                UNUserNotificationCenter.current().delegate = self

                if (error == nil) {
                    // its required for iOS 11 to avoid getting warning about "UI API called on a background thread"
                     DispatchQueue.main.async {

                     UIApplication.shared.registerForRemoteNotifications()

                   }
                }

            })


        } else {
            if UIApplication.shared.responds(to: #selector(UIApplication.registerUserNotificationSettings(_:))) {

                let types:UIUserNotificationType = ([.alert, .badge, .sound])
                let settings:UIUserNotificationSettings = UIUserNotificationSettings(types: types, categories: nil)
                UIApplication.shared.registerUserNotificationSettings(settings)
                UIApplication.shared.registerForRemoteNotifications()

            }

        }
}
注意:也不要忘记导入用户通知,以便能够使用最新的iOS 10 SDK

在任何视图控制器中,您都需要引用应用程序委托并调用该函数:

    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    appDelegate.registerUserNotification()
更新1

您需要实现UNUserNotificationCenter的委托,如下所示,将以下代码放在AppDelegate的末尾,超出类作用域(}),这是iOS 10所必需的:

@available(iOS 10, *)
extension AppDelegate : UNUserNotificationCenterDelegate {


    // Receive displayed notifications for iOS 10 devices.
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

        print("Userinfo \(notification.request.content.userInfo)")



    }


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

        print("Userinfo \(response.notification.request.content.userInfo)")



    }



}

你可以在任何你想要的地方请求推送通知的权限,如果你的应用程序有一个登录页面,那么在登录后你肯定需要这样做

您需要首先将请求权限的代码放入
AppDelegate.swift
中的函数中

func registerUserNotification() {


        if #available(iOS 10.0, *) {
            let authOptions : UNAuthorizationOptions = [.alert, .badge, .sound]

            UNUserNotificationCenter.current().requestAuthorization(options: authOptions, completionHandler: { (bool, error) in

                UNUserNotificationCenter.current().delegate = self

                if (error == nil) {
                    // its required for iOS 11 to avoid getting warning about "UI API called on a background thread"
                     DispatchQueue.main.async {

                     UIApplication.shared.registerForRemoteNotifications()

                   }
                }

            })


        } else {
            if UIApplication.shared.responds(to: #selector(UIApplication.registerUserNotificationSettings(_:))) {

                let types:UIUserNotificationType = ([.alert, .badge, .sound])
                let settings:UIUserNotificationSettings = UIUserNotificationSettings(types: types, categories: nil)
                UIApplication.shared.registerUserNotificationSettings(settings)
                UIApplication.shared.registerForRemoteNotifications()

            }

        }
}
注意:也不要忘记导入用户通知,以便能够使用最新的iOS 10 SDK

在任何视图控制器中,您都需要引用应用程序委托并调用该函数:

    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    appDelegate.registerUserNotification()
更新1

您需要实现UNUserNotificationCenter的委托,如下所示,将以下代码放在AppDelegate的末尾,超出类作用域(}),这是iOS 10所必需的:

@available(iOS 10, *)
extension AppDelegate : UNUserNotificationCenterDelegate {


    // Receive displayed notifications for iOS 10 devices.
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

        print("Userinfo \(notification.request.content.userInfo)")



    }


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

        print("Userinfo \(response.notification.request.content.userInfo)")



    }



}

我在那里!我检查了你的代码,似乎有很多不推荐/错误的代码。你自己试过吗?非常感谢你试图帮助我,我很感激@askaale你用的是什么swift?这是斯威夫特3!没有什么不推荐的!我也在使用Swift 3。奇怪。。其中一些错误是:“使用未声明的类型未授权选项”、“使用未解析标识符”UNUserNotificationCenter、“对成员应用程序的无效引用”等。奇怪……哦,我明白了-对不起。我现在这样做了,它删除了一些错误!但是,我仍然收到每个“应用程序”的错误。(不明确引用)。我的代码中也包含了“UNUserNotificationCenterDelegate”,以消除我以前的委托错误。您知道如何消除应用程序错误吗?@pmanning我已经更新了iOS 11支持的答案,您只需要在主线程
DispatchQueue.main.async中调用它{UIApplication.shared.registerForRemoteNotifications()}
应该没问题:)我在那里!我检查了你的代码,似乎有很多不推荐的/错误的代码。你自己尝试过吗?非常感谢你尝试帮助我,我很感激!@askaale你在使用什么swift?这是swift 3!没有什么不推荐的!我也在使用swift 3。奇怪……一些错误它们是:“使用未声明的类型未授权选项”、“使用未解析标识符”UNUserNotificationCenter、“对成员应用程序的无效引用”等。奇怪……哦,我明白了-对不起。我现在做了,并且删除了一些错误!但是,我仍然会收到每个“应用程序”的错误。(模糊引用)。我的代码中也包含了“UNUserNotificationCenterDelegate”,以消除我以前的委托错误。您知道如何消除应用程序错误吗?@pmanning我已经更新了iOS 11支持的答案,您只需要在主线程
DispatchQueue.main.async中调用它{UIApplication.shared.registerForRemoteNotifications()}
应该没问题:)我在UNUserNotificationCenter上收到一个错误,这是一个错误吗?不过,感谢您尝试帮助我!您将需要以下导入:Firebase、FirebaseMessaging和UserNotificationsYeah,是的,但是,我用上面的完整代码解决了问题。无论如何,我要感谢您r您的贡献。我很感激。我在UNUserNotificationCenter上发现一个错误,这是一个错误销售吗?谢谢您尝试帮助我!您将需要以下导入:Firebase、FirebaseMessaging和UserNotificationsYeah,我做了-但是,我用上面的完整代码解决了问题。无论如何,我要感谢您谢谢你的贡献,我很感激。