gpt4 book ai didi

通过 Firebase 的 iOS 推送通知不起作用

转载 作者:行者123 更新时间:2023-11-30 13:05:45 24 4
gpt4 key购买 nike

您好,我正在制作一个 iPhone 应用,通过 Google 的 Firebase 发送推送通知。我使用 Swift 和 Xcode 进行编程。当我打开应用程序时,系统会要求我允许推送通知,但是当我从 Firebase 控制台发送通知时,我没有收到任何通知。我想知道你是否可以帮助我。我正在使用 Ad Hoc 导出将 .isa 传输到我 friend 的 iPhone 并以这种方式进行测试。我完全按照 Firebase 教程进行操作 - 添加 .plist、cocoa pod,并在项目设置中上传了证书。我有一个付费的苹果开发者帐户。

import UIKit
import CoreData
import Firebase
import FirebaseMessaging


@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?


func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
FIRApp.configure()

let notificationTypes: UIUserNotificationType = [UIUserNotificationType.Alert,UIUserNotificationType.Badge,UIUserNotificationType.Sound]

let notificationSettings = UIUserNotificationSettings(forTypes:notificationTypes, categories:nil)

application.registerForRemoteNotifications()
application.registerUserNotificationSettings(notificationSettings)
return true
}


func applicationWillResignActive(application: UIApplication) {
}

func applicationDidEnterBackground(application: UIApplication) {
}

func applicationWillEnterForeground(application: UIApplication) {
}

func applicationDidBecomeActive(application: UIApplication) {
}

func applicationWillTerminate(application: UIApplication) {
}

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject],
fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
print("MessageID : \(userInfo["gcm_message_id"]!)")
print("%@", userInfo)

}
}

最佳答案

您还需要做一些事情。当您 registerForRemoteNotifications 时,您会收到一个 APNS token ,您需要将其提供给 Firebase。这是在应用程序 didRegisterForRemoteNotificationsWithDeviceToken 中完成的。

import UIKit
import CoreData
import Firebase
import FirebaseMessaging

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

override init() {

super.init()

FIRApp.configure()
}

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

application.registerForRemoteNotifications()
application.registerUserNotificationSettings(
UIUserNotificationSettings(
forTypes: [.Alert, .Badge, .Sound],
categories: nil
)
)

NSNotificationCenter.defaultCenter().addObserver(self,
selector: #selector(tokenRefreshNotification),
name: kFIRInstanceIDTokenRefreshNotification,
object: nil
)

return true
}

func applicationDidEnterBackground(application: UIApplication) {

FIRMessaging.messaging().disconnect()
}

func applicationDidBecomeActive(application: UIApplication) {

FIRMessaging.messaging().connectWithCompletion { (error) in

switch error {
case .Some:
print("Unable to connect with FCM. \(error)")
case .None:
print("Connected to FCM.")
}
}
}

func applicationWillResignActive(application: UIApplication) {}

func applicationWillEnterForeground(application: UIApplication) {}

func applicationWillTerminate(application: UIApplication) {}

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {

FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: .Sandbox)
}

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject],
fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {

print("MessageID : \(userInfo["gcm.message_id"]!)")
print("%@", userInfo)
}

func tokenRefreshNotification(notification: NSNotification) {

if let refreshedToken = FIRInstanceID.instanceID().token() {
print("Instance ID token: \(refreshedToken)")
}

applicationDidBecomeActive(UIApplication.sharedApplication())
}
}

关于通过 Firebase 的 iOS 推送通知不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39397373/

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