I'd like to set alerts for specific items and have a UILocalNotification
open a specific item from my table view controller and display its details. In other words, when a notification appears I'd like to be able to tap on "Show item" and instead of showing the list of all items I'd like to see the details of just that specific item.
For this to work I would need to store information on the specific item (title, index, etc.) How can I do this? Store the title of my item in UILocalNotification.userInfo
?
To add information
Objective c
localNotification.userInfo =@{@"title":title,@"index":index};
Swift
var userInfo = [String:String]()
userInfo["title"] = title
userInfo["index"] = index
notification.userInfo = userInfo
To get information when notification arrive
Objective C
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
NSLog(@“title = %@”,notification.userInfo[@"title"]);
NSLog(@“index = %@”,notification.userInfo[@"index"]);
}
Swift
func application(application: UIApplication!, didReceiveLocalNotification notification: UILocalNotification!) {
println(notification.userInfo?["title"])
println(notification.userInfo?["index"])
}
********EDIT********
Passing the "title" and "index" from the notification's userInfo to the Table View Controller
Objective C
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"BookNotification" object:notification.userInfo]
// or update table view data source
}
Swift
func application(application: UIApplication!, didReceiveLocalNotification notification: UILocalNotification!) {
NSNotificationCenter.defaultCenter().postNotificationName("BookNotification", object:notification.userInfo)
// or update table view data source
}
In Table View Controller
add observer in when notification arrive get "title" and "index" then update Table View Controller
datasource
and reload tableview
Inside Table View Controller
Objective 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
}
Swift
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
}