xcode 使用本地通知 ios 打开 url

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20276829/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 04:21:52  来源:igfitidea点击:

open url with local notification ios

iosiphonexcodeipad

提问by user2632573

I have been searching around for a while trying to find an answer but I cant seem to find one. What I am trying to do is have my app send a local notification when the app is running in the background and when the user opens the notification it will take them to a website. I have it all set up but it keeps opening the app instead of going to the website.

我已经搜索了一段时间试图找到答案,但我似乎无法找到。我想要做的是让我的应用程序在后台运行时发送本地通知,并且当用户打开通知时,它会将他们带到网站。我已经设置好了,但它一直打开应用程序而不是去网站。

My question is, is this even possible to do? And if so could you please see where i am going wrong with my code below? Thank you for your help.

我的问题是,这甚至可能做到吗?如果是的话,你能看看我下面的代码哪里出了问题吗?感谢您的帮助。

CODE:

代码:

-(void)applicationDidEnterBackground:(UIApplication *)application { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

-(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"]];
    }

回答by Muhammad Idris

In Your Business Logic

在您的业务逻辑中

-(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];

}

in your AppDelegate

在您的 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];
}