1

我正在開發一個具有部署目標iOS 7.1的應用程序。我正在使用Firebase進行推送通知。推送通知在iOS 9設備上正常工作。但不適用於iOS 10設備。如何通過FCM(Firebase)或本機在iOS 10中發送推送通知?

當我搜索時,我發現這個筆記here

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 
} 

這注:

重要提示:爲運行iOS的設備10及以上的,必須將委託對象分配給UNUserNotificationCenter對象,以接收顯示通知。

是否需要通過此方法發送推送通知(這是一個新類UNUserNotificationCenter)?是否可以通過舊的推送通知註冊方法?

請讓我知道。因此,我需要將我的項目更新到Swift版本2.3或3.0,這需要時間。

+1

是的,它需要時間,你需要更新https://firebase.google.com/docs/cloud-messaging列出的上述方法/ IOS /客戶端。這是由於在iOS 10中引入UserNotifications。 –

+0

@iOSCuriosity感謝您的回覆。 –

+0

@iOSCuriosity我有同樣的問題。我的應用已準備好發佈appstore。現在我沒有足夠的時間將我的應用程序更改爲swift 3.有沒有解決方案可以在不更新xcode和swift的情況下在ios10設備中接收推送通知? –

回答

0

Firebase文檔中的示例已過時。下面是最近的Xcode 8和斯威夫特3代碼:

import Firebase 
import FirebaseMessaging 
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) 
    } 
}