Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 我正在与firebase一起处理推送通知。而我的";DidReceivereMotenofication“;函数未被调用_Ios_Swift_Firebase_Push Notification_Firebase Cloud Messaging - Fatal编程技术网

Ios 我正在与firebase一起处理推送通知。而我的";DidReceivereMotenofication“;函数未被调用

Ios 我正在与firebase一起处理推送通知。而我的";DidReceivereMotenofication“;函数未被调用,ios,swift,firebase,push-notification,firebase-cloud-messaging,Ios,Swift,Firebase,Push Notification,Firebase Cloud Messaging,我正在与firebase一起处理推送通知。并且我的“DidReceiveMemoteNotification”函数没有调用。我不知道为什么我的函数没有调用,控制台中也没有收到有效负载数据。请解决我的问题。提前感谢 **Here is my code:** FirebaseApp.configure() Messaging.messaging().shouldEstablishDirectChannel = true // [START register_

我正在与firebase一起处理推送通知。并且我的“DidReceiveMemoteNotification”函数没有调用。我不知道为什么我的函数没有调用,控制台中也没有收到有效负载数据。请解决我的问题。提前感谢

**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)")
}

尝试使用
didReceiveMemoteNotificationCompletionHandler
方法。不要忘记在“项目设置”部分的“功能”选项卡中启用推送通知功能

导入用户通知

在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)
//标记: 推送通知方法:

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)")
}

你看到这个链接了吗?这是行不通的。请提供有关控制台中未接收有效负载数据的其他信息#曼尼什语Mahajan@SanpreetSingh你在模拟器上测试吗?没有在真实设备上测试#胡斯布