无法接收推送通知swift5 ios13

问题描述 投票:0回答:1

我是推送通知的新手。我尝试通过iOS13将一些通知从Firebase推送到iPhone,经过几次尝试后,我收到了通知。

[一个小时后,我尝试从Firebase发送另一个通知,但我的iPhone没有显示任何内容。为什么会这样呢?我什么都没改变。

import UIKit
import CoreData
import Firebase
import UserNotifications

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, MessagingDelegate {

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

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

    FirebaseApp.configure()

    let navController = UINavigationController(rootViewController: MainVC())

    window = UIWindow()
    window?.rootViewController = navController

    attemptingToRegisterForNotifications(application: application)

    return true
}

func attemptingToRegisterForNotifications(application: UIApplication) {
    UNUserNotificationCenter.current().delegate = self

    let options: UNAuthorizationOptions = [.alert, .badge, .sound]
    UNUserNotificationCenter.current().requestAuthorization(options: options) { (authorized, error) in
        if authorized {
            print("Authorized")
        }
    }
    application.registerForRemoteNotifications()
    Messaging.messaging().delegate = self

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

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    print("Registered for remote notifications with token:", deviceToken)
    Messaging.messaging().apnsToken = deviceToken
}

func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
    print("Firebase registration token: \(fcmToken)")

    let dataDict:[String: String] = ["token": fcmToken]
    NotificationCenter.default.post(name: Notification.Name("FCMToken"), object: nil, userInfo: dataDict)
}

func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
    print("Remote Message ", remoteMessage.appData)
}

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    let userInfo = notification.request.content.userInfo

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

    print(userInfo)
    completionHandler(.alert)
}

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

  print(userInfo)
  completionHandler()
}

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
  if let messageID = userInfo[gcmMessageIDKey] {
    print("Message ID: \(messageID)")
  }

  print(userInfo)
}

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
                 fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
  if let messageID = userInfo[gcmMessageIDKey] {
    print("Message ID: \(messageID)")
  }

  print(userInfo)
  completionHandler(UIBackgroundFetchResult.newData)
}
ios swift push-notification ios13
1个回答
0
投票

尝试重新安装该应用程序,然后尝试将您的应用程序放置在foregroundbackground中。您是否正在使用Firebase Web界面发送通知?

© www.soinside.com 2019 - 2024. All rights reserved.