从Swift 4服务器发送通知时,Firebase通知无法正常工作?

时间:2018-09-26 15:44:15

标签: ios firebase push-notification apple-push-notifications swift4

我正在快速为我的项目创建一个推送通知。Firebase控制台上的推送消息显示在我的iPhone上,但是从服务器端推送的消息未传递到我的iPhone。我需要在登录时将fcm_token发布到服务器。我的代码是:

AppDelegate

import UIKit
import CoreData
import CoreLocation
import FBSDKCoreKit
import FBSDKLoginKit
import UserNotifications
import Firebase
import FirebaseInstanceID
import FirebaseMessaging

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

  var window: UIWindow?
  var locationManager: CLLocationManager!
  var notificationCenter: UNUserNotificationCenter!

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

    self.locationManager = CLLocationManager()
    self.locationManager!.delegate = self

    // get the singleton object
    self.notificationCenter = UNUserNotificationCenter.current()

    // register as it's delegate
    notificationCenter.delegate = self

    // define what do you need permission to use
    let options: UNAuthorizationOptions = [.alert, .sound]

    // request permission
    notificationCenter.requestAuthorization(options: options) { (granted, error) in
      if !granted {
        print("Permission not granted")
      }
    }

    if launchOptions?[UIApplicationLaunchOptionsKey.location] != nil {
      print("I woke up thanks to geofencing")
    }

    // Messaging.messaging().delegate = self

    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
      Messaging.messaging().delegate = self
    } else {
      let settings: UIUserNotificationSettings =
        UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
      application.registerUserNotificationSettings(settings)
    }
    application.registerForRemoteNotifications()
    FirebaseApp.configure()
    return true
  }

  func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {

    InstanceID.instanceID().instanceID { (result, error) in
      if let error = error {
        print("Error fetching remote instange ID: \(error)")
      } else if let result = result {
        print("Remote instance ID token: \(result.token)")

        UserDefaults.standard.set(result.token, forKey: "FCM_Token")
        UserDefaults.standard.synchronize()
      }
    }
    }

我已经将p12证书上传到了Firebase控制台。后端开发人员是否需要在服务器上安装pem证书,因为推送通知的消息是通过服务器发送的?

0 个答案:

没有答案