2016-10-04 74 views
10

這是Firebase文檔中的代碼。Firebase Cloud Messaging AppDelegate錯誤

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說:「使用未申報類型UNAuthorizationOptions,UNUserNotificationCenter,FIRMessagingDelegate

我還有一行。

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

它說「類型的AppDelegate的價值沒有成員tokenRefreshNotification

我只是複製和火力文檔粘貼我的代碼,但有錯誤!

回答

38

的例子在火力地堡文檔已經過時。下面是最近的Xcode 8和斯威夫特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) 
      FIRMessaging.messaging().remoteMessageDelegate = 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 : FIRMessagingDelegate { 
    // Receive data message on iOS 10 devices. 
    func applicationReceivedRemoteMessage(_ remoteMessage: FIRMessagingRemoteMessage) { 
     print("%@", remoteMessage.appData) 
    } 
} 
+1

所以,我必須使用Xcode的8?當我在Xcode 7.3.1中導入UserNotifications時,出現錯誤:沒有這樣的模塊'UserNotifications'... –

+0

是的,這段代碼是用於Xcode8的 – Alexey

+3

請問您是否更新objective-c? – Nik

4

您需要將以下導入添加到您的文件:

import Firebase 
import FirebaseMessaging 
import UserNotifications 

您還需要一個名爲tokenRefreshNotification類似於下面的方法:

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

不要忘記

進口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 (e.g. 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

+0

你好。你能解釋爲什麼需要在appdidenterbackground時斷開Firebase的連接?那我可以在後臺應用程序中收到消息嗎? – kemdo

0

適用於swift 2.0 添加FIRMessagingDelegate協議

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

雨燕2.3的版本阿列克謝的回答將是:

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) 
    } 
} 
相關問題