-1

我正在使用iOS Swift通知模塊。我沒有收到設備上的提醒/橫幅通知。在Swift中推送通知iOS

我可以在應用程序打開時得到通知,例如在前臺,但不能在應用程序處於後臺或終止狀態。

以下是我的代碼

import UIKit 

import UserNotifications 

import Firebase 

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

    var window: UIWindow? 

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

     FIRApp.configure() 

     askNotificationPermission(application) 

     NotificationCenter.default.removeObserver(self, name: NSNotification.Name.firInstanceIDTokenRefresh, object: nil) 
     NotificationCenter.default.addObserver(self, selector: #selector(self.getFirebaseInstaceID(_:)), name: NSNotification.Name.firInstanceIDTokenRefresh, object: nil) 

     connectToFcm() 

     return true 
    } 

    // MARK:- Push Notification Delegate Methods 
    @nonobjc func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { 
     FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenType.sandbox) 
    } 

    func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) { 
     print(error) 
    } 

    func applicationReceivedRemoteMessage(_ remoteMessage: FIRMessagingRemoteMessage) { 
     let appdata = remoteMessage.appData as NSDictionary as! [String: AnyObject] 
     print(appdata) 
    } 

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

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

    // Receive displayed notifications for iOS 10 devices. 
    @available(iOS 10.0, *) 
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { 

     let userInfo = notification.request.content.userInfo 
     print("Message ID: \(userInfo["gcm.message_id"]!)") 
     print("%@", userInfo) 
    } 

    // MARK: - Permissions 
    func askNotificationPermission(_ application: UIApplication) { 
     if #available(iOS 10.0, *) { 

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

      }) 

      UNUserNotificationCenter.current().delegate = self 

      FIRMessaging.messaging().remoteMessageDelegate = self 

     } else { 

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

     application.registerForRemoteNotifications() 
    } 

    // MARK: - Firebase Connection Methods 
    @objc func getFirebaseInstaceID(_ notification: Notification) { 

     if isNotNull(FIRInstanceID.instanceID().token() as AnyObject?) { 
      let strFirebaseInstanceID = FIRInstanceID.instanceID().token()! as String 
      if !strFirebaseInstanceID.isEmpty { 
       print("Firebase Instance ID - \(strFirebaseInstanceID)") 
       setString(strValue: strFirebaseInstanceID, forKey: Default.firebaseInstanceID) 

       NotificationCenter.default.post(name: Notification.Name(rawValue: Notifications.nSendFirebaseID), object: nil) 
      } else { 
       setString(strValue: "", forKey: Default.firebaseInstanceID) 
      } 
     } else { 
      setString(strValue: "", forKey: Default.firebaseInstanceID) 
     } 

     connectToFcm() 
    } 

    func connectToFcm() { 
     FIRMessaging.messaging().connect { (error) in 
      if (error != nil) { 
       print("Unable to connect with FCM. \(error)") 
      } else { 
       print("Connected to FCM.") 
      } 
     } 
    } 
} 
+0

能否請您分享您要發送有效載荷? –

+0

對於你面臨這個問題的操作系統? –

+0

@Karthick我正面臨iOS 10.1的這個問題 –

回答

0

我忘了補充這種方法

didReceiveRemoteNotification 
1

當您使用FCM發送推送,嘗試發送該有效載荷FCM推送。

{ 
    "to": "<REGISTRATION_TOKEN_HERE>", 
    "notification": { 
    "body": "Yipeee!!! I nailed it", 
    "sound" : "default" 
    }, 
    "data" : "" 
} 
+1

我正在使用Firebase控制檯發送通知 –

+0

我還沒有嘗試過使用控制檯發送推送,但是我使用googleapi的方式進行推送 使用'post request',url正在'https://gcm-http.googleapis.com/gcm/send '和頭文件'Authorization:key = yourKey Content-Type:application/json' –

+1

@TechGeek_iOS'https:// gcm-http.googleapis.com/gcm/send'是GCM端點。雖然兩者仍然兼容,但FCM儘可能使用'https:// fcm.googleapis.com/fcm/send'。 :) –

1

對於iOS 10. *,你需要給推送通知如下willPresent notification方法列表中的完成處理程序存在選項。像這樣嘗試可能會有所幫助。

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

     completionHandler([.alert, .badge, .sound]) 

} 

謝謝。

+0

嘗試過。沒有幫助 –