gpt4 book ai didi

ios - 如何通过 FCM(Firebase) 或 native 在 iOS 10 中发送推送通知?

转载 作者:行者123 更新时间:2023-11-28 08:26:25 24 4
gpt4 key购买 nike

我正在开发一个部署目标为 iOS 7.1 的应用程序。我将 Firebase 用于推送通知。推送通知在 iOS 9 设备上运行良好。但不适用于 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
}

还有这个注释:

Important: for devices running iOS 10 and above, you must assign your delegate object to the UNUserNotificationCenter object to receive display notifications.

是否需要通过此方法(这是一个新类 UNUserNotificationCenter)发送推送通知?是否可以通过旧的推送通知注册方式?

请告诉我。因此,我需要将我的项目更新到 Swift 2.3 或 3.0 版本,这需要时间。

最佳答案

Firebase 文档中的示例已过时。这是最新的 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)
}
}

关于ios - 如何通过 FCM(Firebase) 或 native 在 iOS 10 中发送推送通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39834018/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com