Я работаю над push-уведомлениями с помощью firebase.а моя функция didReceiveRemoteNotification не вызывается - PullRequest
0 голосов
/ 11 мая 2018

Я работаю над push-уведомлением с помощью firebase. и моя функция didReceiveRemoteNotification не вызывается. Я не знаю, почему не вызывается моя функция, а также данные полезной нагрузки не принимаются в консоли. Пожалуйста, устраните мою проблему. Заранее спасибо.

**Here is my code:**
    FirebaseApp.configure()
        Messaging.messaging().shouldEstablishDirectChannel = true

        // [START register_for_notifications]
    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()

    // [END register_for_notifications]
    // Add observer for InstanceID token refresh callback.
    NotificationCenter.default
        .addObserver(self, selector: 
#selector(AppDelegate.tokenRefreshNotification),
                     name: NSNotification.Name.InstanceIDTokenRefresh, 
object: nil)

 @objc func tokenRefreshNotification(_ notification: UIViewController)
    {
        Messaging.messaging().subscribe(toTopic: "Testing")
    }

func application(_ application: UIApplication, didReceiveRemoteNotification 
userInfo: [AnyHashable : Any])
{
    print("userInfo\(userInfo)")
}

Ответы [ 2 ]

0 голосов
/ 11 мая 2018

импорт пользовательских уведомлений

в didFinishLaunchingWithOptions

 if #available(iOS 10, *) {
        UNUserNotificationCenter.current().requestAuthorization(options:[.badge, .alert, .sound]){ (granted, error) in }
        application.registerForRemoteNotifications()
    } else {
        application.registerForRemoteNotifications(matching: [.badge, .sound, .alert])
    }
    let center = UNUserNotificationCenter.current()
    center.delegate = self
    center.requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in
        // Enable or disable features based on authorization.
    }
    application.registerForRemoteNotifications()
    registerForPushNotifications(application: application)

// MARK:  методы pushNotifications:

func registerForPushNotifications(application: UIApplication) {
    if #available(iOS 10.0, *){
        UNUserNotificationCenter.current().delegate = self
        UNUserNotificationCenter.current().requestAuthorization(options: [.badge, .sound, .alert], completionHandler: {(granted, error) in
            if (granted) {
                UIApplication.shared.registerForRemoteNotifications()
            } else{
                //Do stuff if unsuccessful...
            }
        })
    } else { //If user is not on iOS 10 use the old methods we've been using


    }
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    print(response.notification.request.content.userInfo)
    let dic = response.notification.request.content.userInfo as NSDictionary
    if let aps = dic["aps"] as? [String: Any] {
        print(aps)
    }
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    completionHandler([.alert, .badge, .sound])
    print(notification.request.content.userInfo)
    let dic = notification.request.content.userInfo as NSDictionary
    if let aps = dic["aps"] as? [String: Any] {
        print(aps)

    }
}
func application(_ application: UIApplication, didReceiveRemoteNotification data: [AnyHashable : Any]) {
    // Print notification payload data
    print("Push notification received: \(data)")
}
0 голосов
/ 11 мая 2018

Попробуйте использовать метод didreceiveremotenotification completionhandler.Не забудьте включить функцию push-уведомлений на вкладке «Возможности» в разделе настроек вашего проекта.

...