简体   繁体   English

推送通知在IOS 11中不起作用

[英]Push Notification NotWorking in IOS 11

Hi iam trying to implement google firebase push notification. 嗨,iam尝试实现Google Firebase推送通知。

I am Using IOS 11 and Xcode 9.0 我正在使用IOS 11Xcode 9.0

Pod File Pod文件

target 'VQ Smart Home' do
  # Comment the next line if you're not using Swift and don't want to use dynamic frameworks
  use_frameworks!

  # Pods for VQ Smart Home
        pod 'SwiftyJSON'
        pod 'NVActivityIndicatorView'
        pod 'FirebaseInstanceID', '2.0.0'
        pod 'Firebase/Core'
        pod 'Firebase/Messaging'
end

App Delegate 应用程序委托

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        UINavigationBar.appearance().isTranslucent = true

    /************************ FireBase Notification ************************************************************************/
        FirebaseApp.configure()
        if #available(iOS 10.0, *) {
            UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
            let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
            UNUserNotificationCenter.current().requestAuthorization(
                options: authOptions,
                completionHandler: {_, _ in })
        } else {
            let settings: UIUserNotificationSettings =
                UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
            application.registerUserNotificationSettings(settings)
        }
        application.registerForRemoteNotifications()

        let token = Messaging.messaging().fcmToken
        print("*********************************************************************************************************************")
        print("FCM token: \(token ?? "")")
        print("*********************************************************************************************************************")
    /************************ FireBase Notification ************************************************************************/

        return true
    }

 // Push Notification Methods
    func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {
        print("Firebase registration token: \(fcmToken)")
    }

    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
        print(userInfo)
    }

    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
                     fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        print(userInfo)
        completionHandler(UIBackgroundFetchResult.newData)
    }

Error 错误

*********************************************************************************************************************
FCM token: dEukPynDndQ:APA91bG41IAmwarrpdqoUmYxQQ_a6Ro-13YycSXTTyrqRHDGnroJ0pI2cSS5XYIQJwL50s07zUJcaujuj7SY-F5jO_bVEkVpFFyz4jylmwAlgief65Cpl6_TSgNNXkd-eDse7p1uk_rE
*********************************************************************************************************************
- [BoringSSL] Function boringssl_context_get_peer_sct_list: line 1754 received sct extension length is less than sct data length
- [MC] Lazy loading NSBundle MobileCoreServices.framework
- [MC] Loaded MobileCoreServices.framework
- [MC] System group container for systemgroup.com.apple.configurationprofiles path is /private/var/containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles
- refreshPreferences: HangTracerEnabled: 0
- refreshPreferences: HangTracerDuration: 500
- refreshPreferences: ActivationLoggingEnabled: 0 ActivationLoggingTaskedOffByDA:0
- [Firebase/Analytics][I-ACS023007] Firebase Analytics v.40004000 started
- [Firebase/Analytics][I-ACS023008] To enable debug logging set the following application argument: -FIRAnalyticsDebugEnabled (URL)
- TIC Read Status [1:0x0]: 1:57
- TIC Read Status [1:0x0]: 1:57

I have added correct APNs key to Firebase. 我已将正确的APNs密钥添加到Firebase。 and still not receiving Push notification. 仍未收到推送通知。 can someone help me to fix this. 有人可以帮我解决这个问题吗? Tnx. Tnx。

Found a solution 找到了解决方案

Update podfile 更新podfile

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

Add this method didRegisterForRemoteNotificationsWithDeviceToken 添加此方法didRegisterForRemoteNotificationsWithDeviceToken

App Delegate 应用程序委托

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

Now try to send a push notification through firebase Ui and you will receive it in this method ( didReceiveRemoteNotification ) 现在尝试通过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)
    }

Try this one: 试试这个:

XCode 10, Swift 4, Config Push Notification 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])
    }
}

Present to UI: 呈现给用户界面:

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)
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM