UILocalNotification打开特定项

时间:2016-05-09 12:56:23

标签: ios swift uilocalnotification

我想为特定项目设置提醒,并让UILocalNotification从我的表格视图控制器中打开一个特定项目并显示其详细信息。换句话说,当出现通知时,我希望能够点击"显示项目"而不是显示所有项目的列表,我希望看到该特定项目的详细信息。

为此我需要存储有关特定项目的信息(标题,索引等)我该怎么做?将我的项目标题存储在UILocalNotification.userInfo

2 个答案:

答案 0 :(得分:4)

添加信息

目标c

localNotification.userInfo =@{@"title":title,@"index":index};

夫特

var userInfo = [String:String]()
userInfo["title"] = title
userInfo["index"] = index
notification.userInfo = userInfo

在通知到达时获取信息

目标C

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
    NSLog(@“title = %@”,notification.userInfo[@"title"]);
    NSLog(@“index = %@”,notification.userInfo[@"index"]);
}

夫特

func application(application: UIApplication!, didReceiveLocalNotification notification: UILocalNotification!) {
    println(notification.userInfo?["title"])
    println(notification.userInfo?["index"])
}

******** ******** EDIT

将通知的userInfo中的“标题”和“索引”传递给Table View Controller

目标C

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"BookNotification" object:notification.userInfo]
    // or update table view data source
}

斯威夫特

func application(application: UIApplication!, didReceiveLocalNotification notification: UILocalNotification!) {
    NSNotificationCenter.defaultCenter().postNotificationName("BookNotification", object:notification.userInfo)
    // or update table view data source
}

Table View Controller中添加观察者,当通知到达时获取“标题”和“索引”,然后更新Table View Controller datasource并重新加载tableview

内部Table View Controller 目标C

[[NSNotificationCenter defaultCenter] addObserver:self
    selector:@selector(receiveBookNotification:) 
    name:@"BookNotification"
    object:nil];


- (void) receiveTestNotification:(NSNotification *) notification
{ 
    NSString *title = notification.userInfo[@"title"];
    NSString *title = notification.userInfo[@"index"];
    // Update data source and reload
}

夫特

NSNotificationCenter.defaultCenter().addObserver(self, selector: "receiveBookNotification:", name:"BookNotification", object: nil)


func receiveTestNotification(notification: NSNotification) {
    let title = notification.userInfo?["title"]
    let index = notification.userInfo?["index"]
    // Update data source and reload
}

答案 1 :(得分:2)

是的,你可以这样做。本地通知的userInfo属性用于将您需要的任何信息传递给通知处理程序。

如果您希望向用户显示按钮,则需要向本地通知添加操作,并且您还必须处理在应用处于前台时通知触发的情况没有通知显示给用户。