Question

in my iOS App i have an UIWebView so how i can open a specified URL in the UIWebView, with a push notification?

If somebody open the App with the notification, i want to show a specified Website in the UIWebView.

Can I bind the URL (in background) with the push notification?

Thank you.

Was it helpful?

Solution

According to Apple...

If the app is running and receives a remote notification, the app calls this method to process the notification. Your implementation of this method should use the notification to take an appropriate course of action. ... If the app is not running when a push notification arrives, the method launches the app and provides the appropriate information in the launch options dictionary. The app does not call this method to handle that push notification. Instead, your implementation of the application:willFinishLaunchingWithOptions: or application:didFinishLaunchingWithOptions: method needs to get the push notification payload data and respond appropriately.

So, there are three possible scenarios:

1) App is in foreground: you will have full control, just implement didReceiveNotification and do whatever you want.

2) App is running, but in background: the action won't be triggered until the user actually open your app using the received notification.

3) The app is not running: in this case you should implement didFinishLaunchingWithOptions in order to get the additional info and perform the task.

So the code should look like this for didFinishLaunchingWithOptions

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    NSDictionary *userInfo = [launchOptions valueForKey:@"UIApplicationLaunchOptionsRemoteNotificationKey"];
    NSDictionary *apsInfo = [userInfo objectForKey:@"aps"];

    if(apsInfo) {
        // Get the URL or any other data
    }
}

And this is an approximation for didReceiveNotification

/**
 * Remote Notification Received while application was open.
 */
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {

    #if !TARGET_IPHONE_SIMULATOR

    UIApplicationState state = [application applicationState];
    if (state == UIApplicationStateActive)
    {
        NSString *message = nil;
        id aps = [userInfo objectForKey:@"aps"];
        if ([aps isKindOfClass:[NSDictionary class]]) {
            message = [aps objectForKey:@"alert"];
        }

        if (message) {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Notificación"
                                                                message:message
                                                               delegate:self
                                                cancelButtonTitle:@"Aceptar"
                                                      otherButtonTitles:nil, nil];
            [alertView show];
        }
    }

    // Aditional data
    NSString *url = [userInfo objectForKey:@"url"];
    NSLog(@"Received Push URL: %@", url);

    if(url!=nil)
    {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
    }

    NSLog(@"remote notification: %@",[userInfo description]);
    NSDictionary *apsInfo = [userInfo objectForKey:@"aps"];

    NSString *alert = [apsInfo objectForKey:@"alert"];
    NSLog(@"Received Push Alert: %@", alert);

    NSString *sound = [apsInfo objectForKey:@"sound"];
    NSLog(@"Received Push Sound: %@", sound);
    AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);

    NSString *badge = [apsInfo objectForKey:@"badge"];
    NSLog(@"Received Push Badge: %@", badge);
    application.applicationIconBadgeNumber = [[apsInfo objectForKey:@"badge"] integerValue];

    #endif
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top