2016-03-02 18 views
4

在按下本地通知上的按鈕時,我希望能夠根據通知中的某些信息打開特定的視圖控制器。如何才能做到這一點?如何從本地通知打開特定的視圖控制器?

在AppDelegate.swift

let notificationSettings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil) 
     UIApplication.sharedApplication().registerUserNotificationSettings(notificationSettings) 

     if let options = launchOptions { 
      if let notification = options[UIApplicationLaunchOptionsLocalNotificationKey] as? UILocalNotification { 
       if let userInfo = notification.userInfo { 
        let type = userInfo["TYPE"] as! String 
        // do something neat here 
        if (type == "SLEEP") { 

        } else if (type == "STRESS") { 

        } else { 

        } 
       } 
      } 
     } 

     return true 
    } 

在視圖控制器

let sleepInPrefs = prefs.valueForKey(key) as? NSDate 
if sleepInPrefs != nil { 
    print(sleepInPrefs) 
    print("Setting stress notif") 
    for notification in (UIApplication.sharedApplication().scheduledLocalNotifications)! { 
     if (notification.userInfo!["TYPE"]) != nil { 
      if (notification.userInfo!["TYPE"] as! String == key) { 
       UIApplication.sharedApplication().cancelLocalNotification(notification) 
       print("deleting notif") 
       break 
      } 
     } 
    } 
    let notification = UILocalNotification() 
    notification.fireDate = sleepInPrefs 
    notification.repeatInterval = NSCalendarUnit.Day 
    notification.timeZone = NSCalendar.currentCalendar().timeZone 
    notification.alertBody = "" 
    notification.hasAction = true 
    notification.alertAction = "open" 
    notification.soundName = UILocalNotificationDefaultSoundName 
    notification.userInfo = ["TYPE": key ] 
    notification.category = "PROMPT" 
    UIApplication.sharedApplication().scheduleLocalNotification(notification) 
} 
+0

你可以把你嘗試了一些代碼:[UIApplication的registerUserNotificationSettings] -

//在iOS系統8.0和更高版本,應用程序必須使用的用戶通知的註冊? –

+0

我更新了說明。謝謝。 – gohar94

+0

請原諒格式。我正在通過電話進行此操作。 – gohar94

回答

3

你可以通過重定向PARAMS和UILocalNotification的用戶信息附加信息的一個設置的通知。能夠安排之前和現在的UILocalNotifications

func registerForLocaleNotifications() 
{ 
    let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge , .Sound], categories: nil) 
    UIApplication.sharedApplication().registerUserNotificationSettings(settings) 
} 
func scheduleLocalNotification() 
{ 
    let notification = UILocalNotification() 
    notification.fireDate = NSDate(timeIntervalSinceNow: 20)//will be fired in 20 seconds 
    notification.timeZone = NSTimeZone.defaultTimeZone() 
    notification.soundName = UILocalNotificationDefaultSoundName 
    notification.alertBody = "Test UILocalNotification" 
    notification.userInfo = ["TYPE":"Page1"] 
    UIApplication.sharedApplication().scheduleLocalNotification(notification) 
} 

func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) 
{ 
    if (application.applicationState == UIApplicationState.Active) 
    { 
     print("Active") 
     // App is foreground and notification is recieved, 
     // Show a alert. 
    } 
    else if(application.applicationState == UIApplicationState.Background) 
    { 
     print("Background") 
     // App is in background and notification is received, 
     // You can fetch required data here don't do anything with UI. 
    } 
    else if(application.applicationState == UIApplicationState.Inactive) 
    { 
     print("Inactive") 
     // App came in foreground by used clicking on notification, 
     // Use userinfo for redirecting to specific view controller. 
     self.redirectToPage(notification.userInfo) 
    } 
} 

func redirectToPage(userInfo:[NSObject : AnyObject]!) 
{ 
    var viewControllerToBrRedirectedTo:UIViewController! 
    if userInfo != nil 
    { 
     if let pageType = userInfo["TYPE"] 
     { 
      if pageType as! String == "Page1" 
      { 
       viewControllerToBrRedirectedTo = UIViewController() // creater specific view controller 
      } 
     } 
    } 

    if viewControllerToBrRedirectedTo != nil 
    { 
     if self.window != nil && self.window?.rootViewController != nil 
     { 
      let rootVC = self.window?.rootViewController! 
      if rootVC is UINavigationController 
      { 
       (rootVC as! UINavigationController).pushViewController(viewControllerToBrRedirectedTo, animated: true) 
      } 
      else 
      { 
       rootVC?.presentViewController(viewControllerToBrRedirectedTo, animated: true, completion: {() -> Void in 

       }) 
      } 


     } 
    } 

} 
+0

謝謝。你能分享一些例子嗎? – gohar94

+0

你所做的事似乎很好。你是否面臨重定向到特定視圖控制器的問題? –

+0

是的,它打開了應用程序,但我沒有具體提到一個視圖控制器,因爲我不知道如何。 – gohar94

相關問題