简体   繁体   English

如何通过FCM(Firebase)或本机在iOS 10中发送推送通知?

[英]How to send push notification in iOS 10 via FCM(Firebase) or by native?

I am developing an app which has deployment target iOS 7.1. 我正在开发一个部署目标为iOS 7.1的应用程序。 I am using Firebase for the push notifications. 我正在使用Firebase进行推送通知。 Push notifications working fine in the iOS 9 devices. 推送通知在iOS 9设备中正常运行。 But not working in the iOS 10 devices. 但不适用于iOS 10设备。

When I searched, I found this note 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
}

And this note: 而且这个说明:

Important: for devices running iOS 10 and above, you must assign your delegate object to the UNUserNotificationCenter object to receive display notifications. 重要提示:对于运行iOS 10及更高版本的设备,必须将委托对象分配给UNUserNotificationCenter对象以接收显示通知。

Is it necessary to send push notification via this method (which is a new class UNUserNotificationCenter )? 是否有必要通过此方法发送推送通知(这是一个新类UNUserNotificationCenter )? is it be possible through the old push notification registration methods? 是否有可能通过旧的推送通知注册方法?

Please let me know. 请告诉我。 Because of this, I need to update my project to the Swift version 2.3 or 3.0, and it will take time. 因此,我需要将我的项目更新为Swift版本2.3或3.0,这需要时间。

The example in Firebase docs is outdated. Firebase文档中的示例已过时。 Here is the code for most recent Xcode 8 and Swift 3: 这是最新的Xcode 8和Swift 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)
    }
}

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

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