我一直在寻找答案,但我似乎找不到答案。我想要做的是让我的应用程序在后台运行时发送本地通知,当用户打开通知时,它将把他们带到一个网站。我已全部设置好,但它一直打开应用程序而不是访问网站。
我的问题是,这甚至可以做到吗?如果是的话,你能看看我下面的代码哪里出了问题吗?感谢您的帮助。
代码:
-(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];
}
关于ios - 使用本地通知 ios 打开 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20276829/