2016-07-26 169 views
1

在我的應用程序中,我正在使用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:. 
} 


} 

可有人請給我解釋一下好嗎?

回答

2

讓手柄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

相關問題