[英]Presenting a Custom UIViewcontroller when a local notification is clicked
我在applicationDidEnterBackground中創建本地通知 :
NSString *word;
word=[DictionaryHelper getWordOfTheDay];
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];
localNotification.alertBody =("%@",word);
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
它顯示本地通知並打開應用程序的rootviewcontroller(initial) 。
但是我想在單擊本地通知時打開特定的UIViewController ...
只需在applicationDidFinishLaunchingWithOptions
檢查字典userInfo即可。 如果它擁有鍵UIApplicationLaunchOptionsLocalNotificationKey
,則意味着該應用是通過點擊本地通知啟動的,您可以在userInfo字典中獲取對通知對象的引用。
更多信息,請參見 : UIApplicationDelegate協議參考
要呈現不同的UIViewController,只需為這種情況初始化或實例化viewController並將其設置為rootVC。
您還可以使用該方法來響應通知:
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
在您的didReceiveLocalNotification中:
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
NSLog(@"Local notification received: %@", notification.alertBody);
if ([notification.userInfo[@"notificationName"] isEqualToString:@"myLocalNotification"]) {
[self.window.rootViewController performSegueWithIdentifier:@"MainToNotification" sender:nil];
}
}
我的自定義搜索:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.