【问题标题】:willPresent and didReceive Notification delegate not get called when user in foreground and app is installed fresh当前台用户和应用程序全新安装时,不会调用 willPresent 和 didReceive 通知委托
【发布时间】:2019-10-15 19:04:09
【问题描述】:

我已经实现了FirebaseCloudMessaging,当应用程序位于Background 时得到notifications,但是当我安装新应用程序willPresentdidReceive 通知delegate 30 到35 分钟后不会被调用开始打电话。

只有当我通过删除旧的应用程序来安装应用程序时才会发生这种情况。

这是我的代码,你可以检查我做错的地方

import UIKit
import Firebase
import UserNotifications

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

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

        // Register for push notification
        self.registerForNotification()
        FirebaseApp.configure()
        Messaging.messaging().delegate = self
        return true
    }
}

extension AppDelegate: UNUserNotificationCenterDelegate {

    //MARK: - Register For RemoteNotification
    func registerForNotification() {
        UNUserNotificationCenter.current().delegate = self
        UNUserNotificationCenter.current().requestAuthorization(options: [.badge, .alert, .sound]) { granted, error in }
        UIApplication.shared.registerForRemoteNotifications()
    }

    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        #if DEVELOPMENT
            Messaging.messaging().setAPNSToken(deviceToken, type: .sandbox)
        #else
            Messaging.messaging().setAPNSToken(deviceToken, type: .prod)
        #endif
    }

    func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
        debugPrint("Unable to register for remote notifications: \(error.localizedDescription)")
    }

    //MARK: - UNUserNotificationCenterDelegate
    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
        debugPrint(userInfo)
    }

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

        let userInfo = notification.request.content.userInfo
        debugPrint(userInfo)
        completionHandler([.badge, .alert, .sound])
    }


    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        let userInfo = response.notification.request.content.userInfo
        print(userInfo)
        completionHandler()
    }
}

extension AppDelegate: MessagingDelegate {

    //MARK: - MessagingDelegate
    func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
        print(fcmToken)
    }

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

感谢您的帮助

【问题讨论】:

  • 安装应用程序后 30 分钟?还是启动应用程序后 30 分钟?我想您知道应用必须注册推送通知才能创建新令牌。
  • 安装应用程序后 30 分钟。当应用程序处于后台时,我会收到通知。注册过程正常。
  • 我的意思是安装应用程序是不够的。你必须启动它来调用 registerForNotification()。当权限请求警报出现时,用户必须接受。
  • 即使您从 Firebase 控制台发送手动通知也会发生这种情况吗?
  • @Gabriel,当应用程序处于后台通知时,收到意味着我调用了 registerForNotification() 函数并且还允许了权限。谢谢。

标签: ios swift push-notification firebase-cloud-messaging usernotifications


【解决方案1】:

用这个替换 registerForNotification 功能代码。也许会有所帮助

UNUserNotificationCenter.current().delegate = self
        let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
        UNUserNotificationCenter.current().requestAuthorization(options: authOptions) { (isAllow, error) in

            if isAllow {

                Messaging.messaging().delegate = self

            }

        }

        UIApplication.shared.registerForRemoteNotifications()

当用户允许通知时,将调用此委托方法

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

        print(fcmToken)



    }

点击通知时

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

    }

应用在前台时会调用该方法

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

        //this method is called when notification is about to appear in foreground

        completionHandler([.alert, .badge, .sound]) // Display notification as

    }

【讨论】:

  • @HarshadPipaliya .. 可能会起作用.. 在注册通知之前编写 FirebaseApp.configure() .. 还要检查您是否第二次运行您的应用程序?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-29
  • 1970-01-01
相关资源
最近更新 更多