Firebase通知 - Firebase云消息传递

问题描述:

在我的应用程序中,我正在使用Firebase消息传递,并且正在测试以接收通知。 我使用Postman作为REST服务配置通知的身体像:Firebase通知 - Firebase云消息传递

{ 
"to": "/topics/test", 
"priority": "high", 
"notification": { 
    "title": "Test", 
    "body": "New", 
    "badge": "0" 
}, 
"data": { 
    "foo": "bar" 
} 
} 

证书就可以了。我不知道如何在数据包含开始编程一个视图控制器看着passed..For示例中的数据:

"data": { 
    "foo": "viewcontroller1" 
} 

我想,当用户点击该通知,即可开始ViewController1。

我只能在AppDelegate中打印数据?我如何使用传递的值?

这是我AppDelegate.swift:

import UIKit 
import Firebase 
import FirebaseMessaging 

@UIApplicationMain 
class AppDelegate: UIResponder, UIApplicationDelegate { 

var window: UIWindow? 

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) ->  Bool { 
    FIRApp.configure() 

    let notificationTypes : UIUserNotificationType = [UIUserNotificationType.Alert, UIUserNotificationType.Badge, UIUserNotificationType.Sound] 
    let notificationSettings = UIUserNotificationSettings(forTypes: notificationTypes, categories: nil) 
    application.registerForRemoteNotifications() 
     application.registerUserNotificationSettings(notificationSettings) 

    return true 
} 

// [START refresh_token] 
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() 
} 

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

//Receive and handle messages 
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) { 
    // Print message ID. 
    print("Value for foo -> \(userInfo["foo"])") 


    //start viewcontroller programmatically 



} 

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. 
} 

func applicationWillEnterForeground(application: UIApplication) { 
    // Called as part of the transition from the background to the inactive 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. 
} 

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


} 

可有人请给我解释一下好吗?

让手柄didReceiveRemoteNotification首先代码我们提取该视图控制器,我们应该提出:

let type = userInfo["foo"] as! String 

if type == "viewcontroller1" { 

// here we go to start the view controller 


} 

您将需要使用帮助方法来找到最顶部的视图控制器呈现在它的上面。

func getTopViewController()->UIViewController{ 

    if var topController = UIApplication.sharedApplication().keyWindow?.rootViewController { 
     while let presentedViewController = topController.presentedViewController { 
      topController = presentedViewController 
     } 
     return topController 
     // topController should now be your topmost view controller 
    } 
    return UIViewController() 
} 

要启动ViewController,您应该为Storyboard中的标识符创建一个标识符。可以说,它也被称为:viewcontroller1则:

let storyboard = UIStoryboard(name: "Main", bundle: nil) 
let vc = storyboard.instantiateViewControllerWithIdentifier("viewcontroller1") as! viewcontroller1 
self.getTopViewController().presentViewController(vc, animated: true, completion: nil) 

注:当接收你需要检查,如果应用程序在后台,或者是应用程序或者是应用程序之外的通知。对于每个人对于如何以及何时需要展示或呈现视图控制器都有不同的处理。

+0

谢谢你@Aaoli,我需要把所有这些代码放在我的AppDelegate中? – Jigen

+0

@Jigen为获得最佳实践,您需要获取用户信息并创建另一个类来管理通知,因此,从应用程序委托您需要创建发布通知,并且通知处理程序类将以观察者的身份收听它,然后你可以在那里处理它,我会在你的工作时间后为你提供样品来处理它! – AaoIi

+0

@Aaoli谢谢你,这个例子会非常有帮助,但在我的应用程序中,我需要填充一个tableview,这样我就可以开发一个方法来在viewcontroller加载时填充它(在viewDidLoad中) – Jigen