iOS推送通知不适用于Firebase

时间:2017-12-12 09:04:12

标签: ios firebase swift3 push-notification

我使用Firebase通过我的iOS应用程序发送通知,我已按照文档中的所有步骤进行操作:

  • Apple开发者帐户配置

  • 生成CSR文件

  • 上传CSF文件

  • 准备APNs证书

  • 为推送通知配置Firebase

  • 在我的应用中构建Firebase通知

当我尝试发送通知时,Firebase,一个特定用户或所有iOS设备都没有问题。但我的设备(显然是真实设备)都没有收到通知。

我使用好的Bundle,我在我的应用程序中启用通知,并且我的AppDelegate中有代码:

import UIKit
import UserNotifications
import Firebase
import FirebaseInstanceID
import FirebaseMessaging

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, MessagingDelegate {

var window: UIWindow?

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


    UINavigationBar.appearance().barTintColor = Design.blue_es
    UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName:UIColor.white]
    UINavigationBar.appearance().tintColor = UIColor.white


    if #available(iOS 10.0, *) {
        // For iOS 10 display notification (sent via APNS)
        UNUserNotificationCenter.current().delegate = self
        let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
        UNUserNotificationCenter.current().requestAuthorization(
            options: authOptions,
            completionHandler: {_, _ in })
        // For iOS 10 data message (sent via FCM
        Messaging.messaging().remoteMessageDelegate = self
    } else {
        let settings: UIUserNotificationSettings =
            UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
        application.registerUserNotificationSettings(settings)
    }

    application.registerForRemoteNotifications()

    FirebaseApp.configure()

    return true
}
func application(received remoteMessage: MessagingRemoteMessage) {
    print(remoteMessage.appData)
}

// Called when APNs failed to register the device for push notifications
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
    // Print the error to console (you should alert the user that registration failed)
    print("APNs registration failed: \(error)")
}

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

2 个答案:

答案 0 :(得分:1)

IN Swift 3

  1. 配置Apple开发者帐户

  2. 生成CSR文件

  3. 上传您的CSR文件

  4. 准备APNs证书

  5. 为推送通知配置Firebase

  6. 构建Firebase通知应用

  7. 使用CocoaPods安装Firebase SDK

  8. 添加GoogleService-Info.plist

  9. 启用推送通知

  10. 初始化推送通知

  11. 项目的AppDelegate

    import UIKit
    import SVProgressHUD
    import UserNotifications
    import Firebase
    import FirebaseInstanceID
    import FirebaseMessaging
    
    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate,UNUserNotificationCenterDelegate, MessagingDelegate {
    
        var window: UIWindow?
    
    
        static var appDelegate:AppDelegate!
    
        func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    
    
    
            if #available(iOS 10.0, *) {
                // For iOS 10 display notification (sent via APNS)
                UNUserNotificationCenter.current().delegate = self
                let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
                UNUserNotificationCenter.current().requestAuthorization(
                    options: authOptions,
                    completionHandler: {_, _ in })
                // For iOS 10 data message (sent via FCM
                Messaging.messaging().remoteMessageDelegate = self
            } else {
                let settings: UIUserNotificationSettings =
                    UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
                application.registerUserNotificationSettings(settings)
            }
    
            application.registerForRemoteNotifications()
    
            FirebaseApp.configure()
    
            return true
    
        }
    
        func application(received remoteMessage: MessagingRemoteMessage) {
            print(remoteMessage.appData)
        }
    
        func application(application: UIApplication,
                         didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
            Messaging.messaging().apnsToken = deviceToken as Data
        }
    

    }

答案 1 :(得分:0)

我认为首先配置firebase

FirebaseApp.configure()

之后你应该调用registerForRemoteNotifications()

相关问题