【问题标题】:Firebase push notification working for Android but for ios only working from Firebase consoleFirebase 推送通知适用于 Android,但仅适用于 Firebase 控制台
【发布时间】:2018-12-15 08:19:44
【问题描述】:

我已经完成了 Firebase 网站中有关 ios 和 Android 推送通知的步骤。对于 Android,一切正常,但对于 ios,我只能接收通过 Firebase 控制台发送的推送通知,而不是来自我的服务器端代码。知道可能出了什么问题吗?我研究了这个问题,发现了一个过时的 Firebase 配置解决方案,但我想知道是否有更新的解决方案。非常感谢!

这是我获得设置服务说明的链接。 https://firebase.google.com/docs/cloud-messaging/ios/client

这是我用于通知的 Swift 代码:

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?
let gcmMessageIDKey = "gcm.message_id"


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    FirebaseApp.configure()

    if #available(iOS 10.0, *) {
        // For iOS 10 display notification (sent via APNS)
        UNUserNotificationCenter.current().delegate = self

        let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
        UNUserNotificationCenter.current().requestAuthorization(
            options: authOptions,
            completionHandler: {_, _ in })
    } else {
        let settings: UIUserNotificationSettings =
            UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
        application.registerUserNotificationSettings(settings)
    }

    application.registerForRemoteNotifications()

    let token = Messaging.messaging().fcmToken
    print("***** MY FCM token: \(token ?? "")")

    return FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
}

func applicationWillResignActive(_ application: UIApplication) {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
    FBSDKAppEvents.activateApp()
}

func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
    return FBSDKApplicationDelegate.sharedInstance().application(application, open: url, sourceApplication: sourceApplication, annotation: annotation)
}


}


@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

    // With swizzling disabled you must let Messaging know about the message, for Analytics
    // Messaging.messaging().appDidReceiveMessage(userInfo)

    // Print message ID.
    if let messageID = userInfo[gcmMessageIDKey] {
        print("Message ID: \(messageID)")
    }

    // Print full message.
    print(userInfo)

    // Change this to your preferred presentation option
    completionHandler([.alert, .badge, .sound])
}

func userNotificationCenter(_ center: UNUserNotificationCenter,
                            didReceive response: UNNotificationResponse,
                            withCompletionHandler completionHandler: @escaping () -> Void) {
    let userInfo = response.notification.request.content.userInfo
    // Print message ID.
    if let messageID = userInfo[gcmMessageIDKey] {
        print("Message ID: \(messageID)")
    }

    // Print full message.
    print(userInfo)

    completionHandler()
}
}


extension AppDelegate : MessagingDelegate {
// [START refresh_token]
func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {
    print("Firebase registration token: \(fcmToken)")
}
// [END refresh_token]

// [START ios_10_data_message]
// Receive data messages on iOS 10+ directly from FCM (bypassing APNs) when the app is in the foreground.
// To enable direct data messages, you can set Messaging.messaging().shouldEstablishDirectChannel to true.
func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
    print("Received data message: \(remoteMessage.appData)")
}
// [END ios_10_data_message]
}

【问题讨论】:

  • 您必须显示链接并告诉我们您尝试了什么。我可能在考虑 9 个步骤,但您可能只考虑 4 个步骤 :)。另见steps to configure iOS app

标签: ios firebase push-notification


【解决方案1】:

我们可以看看您的通知结构是什么样的吗?我遇到了同样的问题,需要添加 priority: high 才能在 iOS 上工作。

【讨论】:

  • @AndraAvram 请检查这个作为正确答案。
【解决方案2】:

此解决方案有效。问题是后端代码。

【讨论】:

  • 您好,您能解释一下您遇到问题的地方吗?我也有同样的问题。我正在使用 Firebase 云功能和通知适用于 Android,但在 iOS 上它们不起作用。谢谢
  • 你是怎么修改你的后台的可以分享一下吗
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-23
  • 2020-05-22
  • 2018-05-25
  • 2016-11-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多