首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Swift 5 iOS12 -推送通知不工作

Swift 5 iOS12 -推送通知不工作
EN

Stack Overflow用户
提问于 2019-07-27 22:25:54
回答 1查看 1.3K关注 0票数 0

我用Firebase发送了一个通知,但是通知没有发到手机上。我想代码出了点问题。你能检查一下问题出在哪里吗?我正在通过Firebase发送云消息,但它不工作,没有通知。我的应用程序已安装在apple Store中。我正确加载了证书。

代码语言:javascript
复制
extension AppDelegate: UNUserNotificationCenterDelegate {

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

        completionHandler()
    }

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

    }

    func application(_ application: UIApplication,
                     didReceiveRemoteNotification notification: [AnyHashable : Any],
                     fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        if Auth.auth().canHandleNotification(notification) {
            completionHandler(UIBackgroundFetchResult.noData)
            return
        }
        // This notification is not auth related, developer should handle it.
    }


    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        // Pass device token to auth
        Auth.auth().setAPNSToken(deviceToken, type: AuthAPNSTokenType.sandbox)

        // Further handling of the device token if needed by the app
        // ...
    }

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

    }

    /// Register for push notifications
    func registerForPushNotification(){

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

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


extension AppDelegate: MessagingDelegate {

    func application(received remoteMessage: MessagingRemoteMessage) {


    }

    func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {

        print(fcmToken)

    }
}
class AppDelegate: UIResponder, UIApplicationDelegate, UITabBarControllerDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        //UIApplication.shared.isIdleTimerDisabled = true
        UIApplication.shared.setMinimumBackgroundFetchInterval(1800)

        Messaging.messaging().delegate = self
        FirebaseApp.configure()
        Fabric.sharedSDK().debug = true
        Fabric.with([Crashlytics.self()])

        self.registerForPushNotification()
}
    func application(_ application : UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool
    {
        if FUIAuth.defaultAuthUI()?.handleOpen(url, sourceApplication: sourceApplication) ?? false {
            return true
        }
        // other URL handling goes here.
        return false

    }
EN

回答 1

Stack Overflow用户

发布于 2019-07-28 00:37:49

我会关注通过Firebase Cloud Messaging发送和接收推送通知的this tutorial。设备可能需要服务器密钥和令牌来发送/接收本教程向您展示如何提供的通知。请确保不要将服务器密钥直接硬编码到您的代码中。你应该把它放入一个全局常量文件中,然后从那里获取它。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57233032

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档