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.
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"]];
}
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
- (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];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With