如何将数据从通知发送到应用程序

how to send data from notification to application

我将本教程用于我的应用程序中的通知 http://www.appcoda.com/local-notifications-ios8/

每个通知都有操作 - 编辑。我为它添加了 Observer,在 viewDidLoad() 方法中:

 NSNotificationCenter.defaultCenter().addObserver(self, selector: "handleModifyListNotification", name: "modifyListNotification", object: nil)

当在通知中按下编辑按钮时,应用调用此方法:

func handleModifyListNotification() {

    }

我使用的分配通知:

func scheduleLocalNotification() {
        var dateComp:NSDateComponents = NSDateComponents()
        dateComp.year = 2015;
        dateComp.month = 02;
        dateComp.day = 24;
        dateComp.hour = 14;
        dateComp.minute = 34;
        dateComp.timeZone = NSTimeZone.systemTimeZone()
        var calender:NSCalendar? = NSCalendar(calendarIdentifier: NSGregorianCalendar)
        var date:NSDate = calender!.dateFromComponents(dateComp)!
        var localNotification = UILocalNotification()
        localNotification.fireDate = fixNotificationDate(date)
        localNotification.alertBody = "Hey, you must go shopping, remember?"
        localNotification.alertAction = "View List"
        localNotification.category = "shoppingListReminderCategory"
      UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
    }

但是如何向这个方法发送数据才能知道是哪个通知调用了这个方法呢?

像下面这样声明你的notification

NSNotificationCenter.defaultCenter().addObserver(self, selector: "handleModifyListNotification:", name: "modifyListNotification", object: nil)

colonSelector

Post your notification喜欢,

NSNotificationCenter.defaultCenter().postNotificationName("modifyListNotification", object: "Your Object Value")

你的功能应该是,

func handleModifyListNotification(notification: NSNotification) {
    NSLog("Object is %@", notification.valueForKey("object") as String!)
}