繁体   English   中英

Firebase云消息传递AppDelegate错误

[英]Firebase Cloud Messaging AppDelegate Error

这是Firebase文档中的代码。

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, .Badge, .Sound], categories: nil)
        application.registerUserNotificationSettings(settings)
    }

我没有修改任何代码行。 但是Xcode说“使用未声明的类型UNAuthorizationOptionsUNUserNotificationCenterFIRMessagingDelegate

我还有一条线。

NotificationCenter.defaultCenter.addObserver(self,selector: #selector(self.tokenRefreshNotification),name: kFIRInstanceIDTokenRefreshNotification,object: nil)

它说“AppDelegate类型的值没有成员tokenRefreshNotification

我刚从firebase文档中复制并粘贴了我的代码,但是有错误!

Firebase文档中的示例已过时。 这是最新的Xcode 9和Swift 3的代码:

import Firebase
import UserNotifications

class AppDelegate: UIResponder, UIApplicationDelegate {


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


        FIRApp.configure()

        if #available(iOS 10.0, *) {
            let authOptions : UNAuthorizationOptions = [.alert, .badge, .sound]
            UNUserNotificationCenter.current().requestAuthorization(
                options: authOptions,
                completionHandler: {_,_ in })

            // For iOS 10 display notification (sent via APNS)
            UNUserNotificationCenter.current().delegate = self
            // For iOS 10 data message (sent via FCM)
             Messaging.messaging().delegate = self                
        }

        application.registerForRemoteNotifications()

        return true
    }
}


@available(iOS 10, *)
extension AppDelegate : UNUserNotificationCenterDelegate {

    // Receive displayed notifications for iOS 10 devices.

    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (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 : MessagingDelegate {
// Receive data message on iOS 10 devices.
func applicationReceivedRemoteMessage(_ remoteMessage: MessagingRemoteMessage) {
    print("%@", remoteMessage.appData)
    }
}

别忘了

导入FirebaseMessaging

导入UserNotifications

 // // Copyright (c) 2016 Google Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // #import "AppDelegate.h" #if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 @import UserNotifications; #endif @import Firebase; @import FirebaseInstanceID; @import FirebaseMessaging; // Implement UNUserNotificationCenterDelegate to receive display notification via APNS for devices // running iOS 10 and above. Implement FIRMessagingDelegate to receive data message via FCM for // devices running iOS 10 and above. #if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 @interface AppDelegate () <UNUserNotificationCenterDelegate, FIRMessagingDelegate> @end #endif // Copied from Apple's header in case it is missing in some cases (eg pre-Xcode 8 builds). #ifndef NSFoundationVersionNumber_iOS_9_x_Max #define NSFoundationVersionNumber_iOS_9_x_Max 1299 #endif @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Register for remote notifications if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_7_1) { // iOS 7.1 or earlier. Disable the deprecation warnings. #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" UIRemoteNotificationType allNotificationTypes = (UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge); [application registerForRemoteNotificationTypes:allNotificationTypes]; #pragma clang diagnostic pop } else { // iOS 8 or later // [START register_for_notifications] if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_9_x_Max) { UIUserNotificationType allNotificationTypes = (UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge); UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil]; [[UIApplication sharedApplication] registerUserNotificationSettings:settings]; } else { // iOS 10 or later #if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 UNAuthorizationOptions authOptions = UNAuthorizationOptionAlert | UNAuthorizationOptionSound | UNAuthorizationOptionBadge; [[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:authOptions completionHandler:^(BOOL granted, NSError * _Nullable error) { } ]; // For iOS 10 display notification (sent via APNS) [[UNUserNotificationCenter currentNotificationCenter] setDelegate:self]; // For iOS 10 data message (sent via FCM) [[FIRMessaging messaging] setRemoteMessageDelegate:self]; #endif } [[UIApplication sharedApplication] registerForRemoteNotifications]; // [END register_for_notifications] } // [START configure_firebase] [FIRApp configure]; // [END configure_firebase] // Add observer for InstanceID token refresh callback. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tokenRefreshNotification:) name:kFIRInstanceIDTokenRefreshNotification object:nil]; return YES; } // [START receive_message] - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { // 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. NSLog(@"Message ID: %@", userInfo[@"gcm.message_id"]); // Print full message. NSLog(@"%@", userInfo); } - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { // 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. NSLog(@"Message ID: %@", userInfo[@"gcm.message_id"]); // Print full message. NSLog(@"%@", userInfo); } // [END receive_message] // [START ios_10_message_handling] // Receive displayed notifications for iOS 10 devices. #if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 // Handle incoming notification messages while app is in the foreground. - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler { // Print message ID. NSDictionary *userInfo = notification.request.content.userInfo; NSLog(@"Message ID: %@", userInfo[@"gcm.message_id"]); // Print full message. NSLog(@"%@", userInfo); } // Handle notification messages after display notification is tapped by the user. - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler { NSDictionary *userInfo = response.notification.request.content.userInfo; NSLog(@"%@", userInfo); } #endif // [END ios_10_message_handling] // [START ios_10_data_message_handling] #if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 // Receive data message on iOS 10 devices while app is in the foreground. - (void)applicationReceivedRemoteMessage:(FIRMessagingRemoteMessage *)remoteMessage { // Print full message NSLog(@"%@", [remoteMessage appData]); } #endif // [END ios_10_data_message_handling] // [START refresh_token] - (void)tokenRefreshNotification:(NSNotification *)notification { // Note that this callback will be fired everytime a new token is generated, including the first // time. So if you need to retrieve the token as soon as it is available this is where that // should be done. NSString *refreshedToken = [[FIRInstanceID instanceID] token]; NSLog(@"InstanceID token: %@", refreshedToken); // Connect to FCM since connection may have failed when attempted before having a token. [self connectToFcm]; // TODO: If necessary send token to application server. } // [END refresh_token] // [START connect_to_fcm] - (void)connectToFcm { [[FIRMessaging messaging] connectWithCompletion:^(NSError * _Nullable error) { if (error != nil) { NSLog(@"Unable to connect to FCM. %@", error); } else { NSLog(@"Connected to FCM."); } }]; } // [END connect_to_fcm] - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error { NSLog(@"Unable to register for remote notifications: %@", error); } // This function is added here only for debugging purposes, and can be removed if swizzling is enabled. // If swizzling is disabled then this function must be implemented so that the APNs token can be paired to // the InstanceID token. - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { NSLog(@"APNs token retrieved: %@", deviceToken); // With swizzling disabled you must set the APNs token here. // [[FIRInstanceID instanceID] setAPNSToken:deviceToken type:FIRInstanceIDAPNSTokenTypeSandbox]; } - (void)applicationDidBecomeActive:(UIApplication *)application { [self connectToFcm]; } // [START disconnect_from_fcm] - (void)applicationDidEnterBackground:(UIApplication *)application { [[FIRMessaging messaging] disconnect]; NSLog(@"Disconnected from FCM"); } // [END disconnect_from_fcm] @end 

您需要将以下导入添加到您的文件中:

import Firebase
import FirebaseMessaging
import UserNotifications

您还需要一个名为tokenRefreshNotification的方法,类似于以下内容:

func tokenRefreshNotification() {
    let fcmDeviceToken = FIRInstanceID.instanceID().token()
    // TODO: Send token to server
}

对于swift 2.0,添加FIRMessagingDelegate协议

class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, FIRMessagingDelegate{}
func applicationReceivedRemoteMessage(remoteMessage: FIRMessagingRemoteMessage){}

Swift 2.3版本的Alexey的回答是:

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 , .Badge, .Sound], categories: nil)
            application.registerUserNotificationSettings(settings)
        }

        application.registerForRemoteNotifications()

和..

@available(iOS 10, *)
extension AppDelegate : UNUserNotificationCenterDelegate {

    // Receive displayed notifications for iOS 10 devices.

    func userNotificationCenter(center: UNUserNotificationCenter, willPresent 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)
    }
}

暂无
暂无

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

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