【问题标题】:open url with local notification ios使用本地通知 ios 打开 url
【发布时间】:2013-12-15 02:57:16
【问题描述】:

我一直在四处寻找答案,但似乎找不到答案。我想要做的是让我的应用程序在后台运行时发送本地通知,当用户打开通知时,它会将他们带到网站。我已经全部设置好了,但它一直打开应用程序而不是访问网站。

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

代码:

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

【问题讨论】:

    标签: ios iphone xcode ipad


    【解决方案1】:

    在您的业务逻辑中

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

    【讨论】:

    • 感谢您的回复,我如何使用上面已经显示的代码来实现这一点?还是这与那是分开的?
    • 再次感谢您,但您所说的“在您的业务逻辑中”是什么意思?对不起,但我还是新手。感谢您的理解和帮助
    • 将该函数粘贴到您的 ViewController 中并从您定义的任何 IBAction 中调用它。例如 [self scheduleNotificationForDate:[NSDate date]];
    • 好的,我明白了,谢谢。但现在它只有在我按下视图控制器中的按钮时才会进入网站。我想要实现的是当应用程序关闭并且您收到通知时,当您滑动通知时我希望它直接进入网站而不打开应用程序。这有意义吗?
    • 这在官方 iOS SDK 中是不可能的。滑动/点击通知横幅会以任何方式打开应用程序。尝试探索应用程序状态检查,即 Active / InActive 以在 didReceiveLocalNotification 方法中使用它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-28
    • 1970-01-01
    • 2020-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多