程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios10,Swift 3和Firebase推送通知(FCM)大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决ios10,Swift 3和Firebase推送通知(FCM)?

开发过程中遇到ios10,Swift 3和Firebase推送通知(FCM)的问题如何解决?下面主要结合日常开发的经验,给出你关于ios10,Swift 3和Firebase推送通知(FCM)的解决方法建议,希望对你解决ios10,Swift 3和Firebase推送通知(FCM)有所启发或帮助;

在 方法 ,添加以下代码:

func application(application: UIApplication, dIDRegisterForRemoteNotificationsWithDeviCEToken deviCEToken: NSData) {
    let tokenChars = UnsafePointer<CChar>(deviCEToken.bytes)
    var tokenString = ""

    for i in 0..<deviCEToken.length {
        tokenString += String(format: "%02.2hhx", arguments: [tokenChars[i]])
    }

    FIRInstancEID.instancEID().setAPNSToken(deviCEToken, type: FIRInstancEIDAPNSTokenType.UnkNown)

    print("tokenString: \(tokenString)")
}

并且不要忘记在 *

解决方法

我正在努力显示从FCM通知控制台发送到设备的推送通知。我可以看到设备正在接收通知,因为我可以看到发送“ test8”的消息

Connected to FCm.
%@ [AnyHashable("notification"): {
    body = test8;
    e = 1;
},

但是,无论我的应用程序位于前台还是后台都没有关系,但我不会显示通知。

我已在info.plist中添加了“必需的后台模式-
应用程序下载内容以响应推送通知”。我的证书是正确的,并且生成令牌没有问题。我的应用程序正在接收通知,但未显示它们。

import UIKit
import UserNotifications
import Firebase
import FirebaseInstancEID
import Firebasemessaging

@UIApplicationMain
class AppDelegate: UIResponder,UIApplicationDelegate {

    var window: UIWindow?

    func application(application: UIApplication,didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

        // [START register_for_notifications]
        if #available(iOS 10.0,*) {
            let authOptions : UNAuthorizationOptions = [.Alert,.badge,.Sound]
            UNUserNotificationCenter.currentNotificationCenter().requestAuthorizationWithOptions(
                authOptions,completionHandler: {_,_ in })

            // For iOS 10 display notification (sent via APNS)
            UNUserNotificationCenter.currentNotificationCenter().delegate = self
            // For iOS 10 data message (sent via FCM)
            FIRmessaging.messaging().remotemessageDelegate = self

        } else {
            let setTings: UIUserNotificationSetTings =
                UIUserNotificationSetTings(forTypes: [.Alert,.Sound],categories: nil)
            application.registerUserNotificationSetTings(setTings)
            application.registerForRemoteNotifications()
        }

        application.registerForRemoteNotifications()

        // [END register_for_notifications]

        FIRApp.configure()

        // Add observer for InstancEID token refresh callBACk.
        NsnotificationCenter.defaultCenter().addObserver(self,SELEctor: #SELEctor(self.tokenrefreshNotification),name: kFIRInstancEIDTokenrefreshNotification,object: nil)

        return true
    }

    // [START receive_message]
    func application(application: UIApplication,didReceiveRemoteNotification userInfo: [NSObject : AnyObject],fetchCompletionHandler completionHandler: (UIBACkgroundFetchResult) -> Void) {
        // If you are receiving a notification message while your app is in the BACkground,// this callBACk will not be fired till the user taps on the notification launching the application.
        // TODO: Handle data of notification

        // Print message ID.
        print("message ID: \(userInfo["gcm.message_id"]!)")

        // Print full message.
        print("%@",userInfo)
    }
    // [END receive_message]

    // [START refresh_token]
    func tokenrefreshNotification(notification: Nsnotification) {
        if let refreshedToken = FIRInstancEID.instancEID().token() {
            print("InstancEID token: \(refreshedToken)")
        }

        // Connect to FCM since connection may have failed when attempted before having a token.
        connectToFcm()
    }
    // [END refresh_token]

    // [START connect_to_fcm]
    func connectToFcm() {
        FIRmessaging.messaging().connectWithCompletion { (error) in
            if (error != nil) {
                print("Unable to connect with FCm. \(error)")
            } else {
                print("Connected to FCm.")
            }
        }
    }
    // [END connect_to_fcm]

    func applicationDidBecomeActive(application: UIApplication) {
        connectToFcm()
    }

    // [START disconnect_from_fcm]
    func applicationDidEnterBACkground(application: UIApplication) {
        FIRmessaging.messaging().disconnect()
        print("Disconnected from FCm.")
    }
    // [END disconnect_from_fcm]
}

// [START ios_10_message_handling]
@available(iOS 10,*)
extension AppDelegate : UNUserNotificationCenterDelegate {

    // Receive displayed notifications for iOS 10 devices.
    func userNotificationCenter(center: UNUserNotificationCenter,willPresentNotification notification: UNNotification,withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) {
        let userInfo = notification.request.content.userInfo
        // Print message ID.
        print("message ID: \(userInfo["gcm.message_id"]!)")
        // Print full message.
        print("%@",userInfo)
    }
}

extension AppDelegate : FIRmessagingDelegate {
    // Receive data message on iOS 10 devices.
    func applicationReceivedRemotemessage(remotemessage: FIRmessagingRemotemessagE) {
        print("%@",remotemessage.apPDAta)
    }
}
// [END ios_10_message_handling]

我一直在尝试自己研究和解决此问题,但遇到了问题。任何帮助或建议,将不胜感激。

大佬总结

以上是大佬教程为你收集整理的ios10,Swift 3和Firebase推送通知(FCM)全部内容,希望文章能够帮你解决ios10,Swift 3和Firebase推送通知(FCM)所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。