3

Ich verwende Firebase mit meiner IOS-App und versuche, Push-Benachrichtigungen einzurichten.Wie bekomme ich FCM-Benachrichtigungen, während die App im Hintergrund läuft (ios)?

Momentan kann ich Push-Benachrichtigungen empfangen, während meine App im Vordergrund ist, aber wenn ich sie in den Hintergrund stelle, kann ich keine empfangen.

In meinen App-Funktionen habe ich Push-Benachrichtigungen sowie Remote-Benachrichtigungen im Abschnitt Hintergrundmodi aktiviert.

ich derzeit Postman bin mit der folgenden POST-Anfrage senden:

https://fcm.googleapis.com/fcm/send 

Content-Type:application/json 
Authorization:key=<my_server_key> 

{ "to" : "token_id", 
    "priority":"high", 
    "data":{ 
     "title":"mytitle", 
     "body":"mybody", 
     "url":"myurl" 
    }, 
    "notification":{ 
     "title":"mytitle", 
     "body":"mybody", 
     "content_available": true 
    } 
} 

Nachdem ich die Anfrage sende ich den Status 200 und folgende Antwort erhalten:

{ 
    "multicast_id": 7920502412622407470, 
    "success": 1, 
    "failure": 0, 
    "canonical_ids": 0, 
    "results": [ 
    { 
     "message_id": "0:1469561276461379%345a0fe5f9fd7ecd" 
    } 
    ] 
} 

ich diese beide gefolgt Anleitungen zum Einrichten des Sendens beim Empfangen von Nachrichten:

https://firebase.google.com/docs/cloud-messaging/ios/client#receive_messages_through_messaging

https://firebase.google.com/docs/cloud-messaging/ios/certs

Hier ist meine AppDelegate Datei:

AppDelegate.swift

@UIApplicationMain 
class AppDelegate: UIResponder, UIApplicationDelegate { 

    var window: UIWindow? 

    override init(){ 
     // Configure Firebase 
     FIRApp.configure() 
     // Mantain data offline (persists state) 
     FIRDatabase.database().persistenceEnabled = true 
    } 

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 
     // Register for remote notifications 
     if #available(iOS 8.0, *) { 
      // [START register_for_notifications] 
      let settings: UIUserNotificationSettings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil) 
      application.registerUserNotificationSettings(settings) 
      application.registerForRemoteNotifications() 
      // [END register_for_notifications] 
     } else { 
      // Fallback 
      let types: UIRemoteNotificationType = [.Alert, .Badge, .Sound] 
      application.registerForRemoteNotificationTypes(types) 
     } 

     // Add observer for InstanceID token refresh callback. 
     NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.tokenRefreshNotification), name: kFIRInstanceIDTokenRefreshNotification, object: nil) 


     // Override point for customization after application launch 
     return FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions) 
    } 

    func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) { 
     if notificationSettings.types != .None { 
      application.registerForRemoteNotifications() 
     } 
    } 

    func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) { 
     let refreshedToken = FIRInstanceID.instanceID().token()! 
     print("InstanceID token:", refreshedToken) 
    } 

    func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) { 
     print("Failed to register:", error) 
    } 

    func application(application: UIApplication, openURL url: NSURL, 
          sourceApplication: String?, 
          annotation: AnyObject) -> Bool { 

     return FBSDKApplicationDelegate.sharedInstance().application(
      application, 
      openURL: url, 
      sourceApplication: sourceApplication, 
      annotation: annotation) 
    } 

    func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], 
        fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) { 

     FIRMessaging.messaging().appDidReceiveMessage(userInfo) 
     // Print full message. 
     print("%@", userInfo) 
    } 

    func tokenRefreshNotification(notification: NSNotification) { 
     let refreshedToken = FIRInstanceID.instanceID().token()! 
     print("InstanceID token: \(refreshedToken)") 

     // Connect to FCM since connection may have failed when attempted before having a token. 
     connectToFcm() 
    } 

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

    func applicationWillResignActive(application: UIApplication) { 
     // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 
     // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. 
    } 

    func applicationDidEnterBackground(application: UIApplication) { 
     // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
     // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 
     FIRMessaging.messaging().disconnect() 
     print("Disconnected from FCM.") 
    } 

    func applicationWillEnterForeground(application: UIApplication) { 
     // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. 
    } 

    func applicationDidBecomeActive(application: UIApplication) { 
     // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 
     FBSDKAppEvents.activateApp() 
     connectToFcm() 
    } 

    func applicationWillTerminate(application: UIApplication) { 
     // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 
    } 

} 

Antwort

0

Sie müssen "Remote-Benachrichtigungen" in Ihrer Xcode-Projekt-Konfigurationsdatei zu überprüfen. Sehen Sie sich an this screeenshot

Obwohl die Nachricht nicht übermittelt wird, wenn Ihre App im Hintergrund ist. Z.B. Wenn Sie es töten oder noch nicht gestartet :(

Verwandte Themen