打开本地通知的url
我一直在寻找一段时间试图find答案,但我似乎无法find一个。 我试图做的是让我的应用程序发送本地通知,当应用程序在后台运行,当用户打开通知,将他们带到一个网站。 我已经设置好了,但它不断地打开应用程序,而不是去网站。
我的问题是,这甚至有可能吗? 如果可以的话,请看下面我的代码出错的地方吗? 感谢您的帮助。
码:
– (void)applicationDidEnterBackground:(UIApplication *)application {//使用此方法释放共享资源,保存用户数据,使计时器无效并存储足够的应用程序状态信息,以便将应用程序恢复到当前状态,以防稍后终止。 //如果您的应用程序支持后台执行,则调用此方法而不是applicationWillTerminate:当用户退出时。
NSDate *date = [NSDate date]; NSDate *futureDate = [date dateByAddingTimeInterval:3]; notifyAlarm.fireDate = futureDate; notifyAlarm.timeZone = [NSTimeZone defaultTimeZone]; notifyAlarm.repeatInterval = 0; notifyAlarm.alertBody = @"Visit Our Website for more info"; [app scheduleLocalNotification:notifyAlarm]; if ( [notifyAlarm.alertBody isEqualToString:@"Visit Our Website for more info"] ) { [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://www.bluemoonstudios.com.au"]]; }
在您的业务逻辑
-(void)scheduleNotificationForDate:(NSDate *)fireDate{ UILocalNotification *notification = [[UILocalNotification alloc] init]; notification.fireDate = fireDate; notification.alertAction = @"View"; notification.alertBody = @"New Message Received"; notification.userInfo = @{@"SiteURLKey": @"http://www.google.com"}; [[UIApplication sharedApplication] scheduleLocalNotification:notification]; }
在你的AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey]; if(notification != nil){ NSDictionary *userInfo = notification.userInfo; NSURL *siteURL = [NSURL URLWithString:[userInfo objectForKey:@"SiteURLKey"]]; [[UIApplication sharedApplication] openURL:siteURL]; } } -(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{ NSDictionary *userInfo = notification.userInfo; NSURL *siteURL = [NSURL URLWithString:[userInfo objectForKey:@"SiteURLKey"]]; [[UIApplication sharedApplication] openURL:siteURL]; }