gpt4 book ai didi

Swift 5 iOS12 - 推送通知不起作用

转载 作者:行者123 更新时间:2023-11-30 10:39:13 25 4
gpt4 key购买 nike

我使用 Firebase 发送了通知,但通知未发送至手机。我认为代码有问题。你能检查一下问题出在哪里吗?我正在通过 Firebase 发送云消息,但它不起作用,没有收到任何通知。我的应用程序安装在苹果商店中。我正确加载了证书。

extension AppDelegate: UNUserNotificationCenterDelegate {

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

completionHandler()
}

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

}

func application(_ application: UIApplication,
didReceiveRemoteNotification notification: [AnyHashable : Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
if Auth.auth().canHandleNotification(notification) {
completionHandler(UIBackgroundFetchResult.noData)
return
}
// This notification is not auth related, developer should handle it.
}


func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
// Pass device token to auth
Auth.auth().setAPNSToken(deviceToken, type: AuthAPNSTokenType.sandbox)

// Further handling of the device token if needed by the app
// ...
}

func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {

}

/// Register for push notifications
func registerForPushNotification(){

if #available(iOS 10.0, *) {
let center = UNUserNotificationCenter.current()
center.delegate = self
center.requestAuthorization(options: [.sound, .alert, .badge]) { (granted, error) in
if error == nil{
DispatchQueue.main.async {
UIApplication.shared.registerForRemoteNotifications()
}
}
}
} else {

let settings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
UIApplication.shared.registerUserNotificationSettings(settings)
UIApplication.shared.registerForRemoteNotifications()
}
}
}


extension AppDelegate: MessagingDelegate {

func application(received remoteMessage: MessagingRemoteMessage) {


}

func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {

print(fcmToken)

}
}
class AppDelegate: UIResponder, UIApplicationDelegate, UITabBarControllerDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
//UIApplication.shared.isIdleTimerDisabled = true
UIApplication.shared.setMinimumBackgroundFetchInterval(1800)

Messaging.messaging().delegate = self
FirebaseApp.configure()
Fabric.sharedSDK().debug = true
Fabric.with([Crashlytics.self()])

self.registerForPushNotification()
}
func application(_ application : UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool
{
if FUIAuth.defaultAuthUI()?.handleOpen(url, sourceApplication: sourceApplication) ?? false {
return true
}
// other URL handling goes here.
return false

}

最佳答案

我会关注this tutorial用于通过 Firebase Cloud Messaging 发送和接收推送通知。设备可能需要服务器 key 和 token 来发送/接收本教程向您展示如何提供的通知。请确保不要将服务器 key 直接硬编码到您的代码中。您应该将其放入全局常量文件之类的文件中并从那里获取它。

关于Swift 5 iOS12 - 推送通知不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57233032/

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