2015-11-14 64 views

回答

19

要做到這一點,你需要爲你的應用程序可以與打開每個ViewController一個identifier,然後檢查​​在application:didFinishLaunchingWithOptions:launchOptions參數在AppDelegate下面是步驟來這樣做:

  1. 在你PFPush,使用setData一鍵添加到您的有效載荷與標識符:notification.setData(["alert":"your notification string", "identifier":"firstController"])

  2. 設置identifier上的各ViewController通過選擇它並改變下列值

Setting the Storyboard ID

  • 讓您的推送通知發送故事板ID在其​​與鍵identifier
    1. 檢查應用程序中的ID:didFinishLaunchingWithOptions:在該函數的末尾添加以下內容:
    if let payload = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? NSDictionary, identifier = payload["identifier"] as? String { 
        let storyboard = UIStoryboard(name: "Main", bundle: nil) 
        let vc = storyboard.instantiateViewControllerWithIdentifier(identifier) 
        window?.rootViewController = vc 
    } 
    
    +0

    我正在使用PFPush,此刻,我的推送通知只包含一串文字 – mechdon

    +0

    @mechdon在推送通知中使用'setData'方法發送數據以及使用字典'[「identifier」:「 firstController「]'或任何你的標識符應該是 – kabiroberai

    +0

    @mechdon你打算如何指定'ViewController'打開它? – kabiroberai

    2

    在AppDelegate中,你會得到一個委託回調「didFinishLoading」或「didReceivePushNotification」的方法(根據您的應用程序在背景或前景)。在該方法中,獲取最頂層的視圖控制器的實例,然後創建您想要顯示的特定視圖控制器,並從最頂層的視圖控制器呈現/推送。

    +3

    薩蒂揚,感謝您的回答,這也是正確的。但是我給了kabiroberai的答案,因爲他是我的問題最完整的解決方案。 – mechdon

    0
    UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]; 
        if (notification) 
        { 
         [self application:application didReceiveRemoteNotification:(NSDictionary*)notification]; 
        } 
    
    相關問題