在IOS 11中推送通知NotWorking

时间:2017-11-07 02:52:41

标签: ios swift firebase push-notification

嗨iam尝试实施google firebase推送通知。

我正在使用 IOS 11 Xcode 9.0

Pod文件

campaign_items

应用代表

harbor-host

错误

true

我已将正确的APN密钥添加到Firebase。仍然没有收到推送通知。有人可以帮我解决这个问题。 TNX。

2 个答案:

答案 0 :(得分:0)

找到解决方案

更新podfile

pod 'Firebase/Core', '4.0.4'
pod 'Firebase/Database', '4.0.4'
pod 'Firebase/Messaging', '4.0.4'
pod 'FirebaseInstanceID', '2.0.0'

添加此方法 didRegisterForRemoteNotificationsWithDeviceToken

应用代表

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        Messaging.messaging().apnsToken = deviceToken
    }

现在尝试通过firebase Ui发送推送通知,您将通过此方法接收它( didReceiveRemoteNotification

 func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
                     fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        print(userInfo)
        let aps = userInfo["aps"] as! NSDictionary
        print(aps["alert"]  as? String ?? "")
        completionHandler(UIBackgroundFetchResult.newData)
    }

答案 1 :(得分:0)

尝试这个:

XCode 10,Swift 4,配置推送通知

import UserNotifications

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        //This line is very import:
        UNUserNotificationCenter.current().delegate = self

        if #available(iOS 10.0, *) {
            let center = UNUserNotificationCenter.current()
            center.requestAuthorization(options: [.badge, .alert, .sound], completionHandler: {(granted, error) in
                if error == nil {
                    DispatchQueue.main.async(execute: {
                        UIApplication.shared.registerForRemoteNotifications()
                    })
                }
            })
        } else {
         UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.sound, .badge, .alert], categories: nil))
            UIApplication.shared.registerForRemoteNotifications()
        }
        return true
    }

    @available(iOS 10.0, *)
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        print("willPresent, userInfo: ", notification.request.content.userInfo)
        completionHandler([.alert, .badge, .sound])
    }
}

用户界面:

private func SendNotification(identifier: String, title: String, body: String){
        let content = UNMutableNotificationContent()
        content.title = title
        content.body = body
        content.sound = UNNotificationSound.default
        let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
        let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)
        UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
    }