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) 
    } 
}