2017-06-10 40 views
0

我收到的通知是什么。 发送到Android会罚款,发送到iOS是失败带有Firebase通知的iOS,全部不能正常工作

我从火力地堡控制台发送

我没有收到前台或后台通知

这里是AppDelegate.swift

import UIKit 
import Firebase 
import FirebaseMessaging 
import UserNotifications 

@UIApplicationMain 
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate { 

    var window: UIWindow? 


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

     if #available(iOS 10.0, *) { 
      UIView.appearance().semanticContentAttribute = .forceLeftToRight 
     } 

     FirebaseApp.configure() 

     // [START set_messaging_delegate] 
     Messaging.messaging().delegate = self as? MessagingDelegate 
     // [END set_messaging_delegate] 

     //create the notificationCenter 
     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 }) 

      // For iOS 10 data message (sent via FCM) 
      //FIRMessaging.messaging().remoteMessageDelegate = self 

     } else { 
      let settings: UIUserNotificationSettings = 
       UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil) 
      application.registerUserNotificationSettings(settings) 
     } 

     application.registerForRemoteNotifications() 

     return true 
    } 

    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { 
     var token = "" 
     for i in 0..<deviceToken.count { 
      token = token + String(format: "%02.2hhx", arguments: [deviceToken[i]]) 
     } 
     print("Registration succeeded! Token: ", token) 

     let topicName = "/topics/ChaclateOnMobile" 
     Messaging.messaging().subscribe(toTopic: topicName) 
    } 

    func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) { 
     print("Registration failed! \(error)") 
    } 

    // Firebase notification received 
    @available(iOS 10.0, *) 
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (_ options: UNNotificationPresentationOptions) -> Void) { 

     // custom code to handle push while app is in the foreground 
     print("Handle push from foreground\(notification.request.content.userInfo)") 

     let dict = notification.request.content.userInfo["aps"] as! NSDictionary 
     let d : [String : Any] = dict["alert"] as! [String : Any] 
     let body : String = d["body"] as! String 
     let title : String = d["title"] as! String 
     print("Title:\(title) + body:\(body)") 
     self.showAlertAppDelegate(title: title,message:body,buttonTitle:"ok",window:self.window!) 

    } 

    @available(iOS 10.0, *) 
    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> Void) { 
     // if you set a member variable in didReceiveRemoteNotification, you will know if this is from closed or background 
     print("Handle push from background or closed\(response.notification.request.content.userInfo)") 
    } 

    func showAlertAppDelegate(title: String,message : String,buttonTitle: String,window: UIWindow){ 
     let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert) 
     alert.addAction(UIAlertAction(title: buttonTitle, style: UIAlertActionStyle.default, handler: nil)) 
     window.rootViewController?.present(alert, animated: false, completion: nil) 
    } 
    // Firebase ended here 

}

+0

检查:

在代码中添加这didFinishLaunchingWithOptions。 – PGDev

+0

1.已经从Xcode功能启用了**推送通知**? 2.您是否启用了**背景模式下的远程通知**(也适用于Xcode功能)?有关您需要执行的操作的完整列表,请参阅[此处](https://stackoverflow.com/questions/42275060/what-is-difference-between-remote-notification-and-silent-notification-in-ios/42302369 #42302369) – Honey

回答

1
  1. 检查,如果你是使用正确的token发送通知。
  2. 你有没有registered你的应用程序可以收到Push Notifications
  3. 检查一下您是否在AppDelegate.

当应用程序是not running实施适当的方法,该通知在收到didFinishLaunchingWithOptions。如果您使用的是正确的令牌发送通知

if let notification = launchOptions?[.remoteNotification] as? [String: AnyObject] 
{ 
    //Your code 
} 

当你的应用是在background/foreground,在收到通知后在didReceiveRemoteNotification

func application(
    _ application: UIApplication, 
    didReceiveRemoteNotification userInfo: [AnyHashable : Any], 
    fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) 
{ 
    //Your code 
}