用本地通知ios打开网址 [英] open url with local notification ios

查看:102
本文介绍了用本地通知ios打开网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

代码:

- (void)applicationDidEnterBackground:(UIApplication *)application
{
//使用此方法释放共享资源,保存用户数据,使计时器无效,并存储足够的应用程序状态信息,以便将应用程序恢复到当前状态,以防以后终止。
//如果您的应用程序支持后台执行,则调用此方法而不是applicationWillTerminate:当用户退出时。

-(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.

    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中的

in your 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打开网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆