How to use the same foreground background notification in APNS

I'm developing APNS. When I submit APNS, please provide the url and move the url. APNS succeeded, but when the app was launched it could not receive a foreground notification. However, in the background, it works. when it is in the foreground it just goes to url without notice. Could you help me..?

    - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    UIApplicationState state = [UIApplication sharedApplication].applicationState;
    BOOL state_active = (state == UIApplicationStateActive);
    dic_apns = [userInfo objectForKey:@"aps"];

    // alert export
    NSString * msg = [dic_apns objectForKey:@"alert"];
    NSString * eventcode = [userInfo objectForKey:@"eventcode"];
    [[NSUserDefaults standardUserDefaults] setValue :msg forKey:@"push_msg"];
    [[NSUserDefaults standardUserDefaults] setValue :eventcode forKey:@"eventcode"];
    [[NSUserDefaults standardUserDefaults] synchronize];

    NSLog(@"APNS : msg=%@ | eventcode=%@", msg , eventcode);
    [self goto_link];
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];

}
-(void) goto_link{

    NSString * eventcode = [[NSUserDefaults standardUserDefaults] valueForKey:@"eventcode"];
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@%@", _MAIN_URL, _PUSH_PARAM, eventcode]];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    ViewController* main = (ViewController *)  self.window.rootViewController;

    if (!main.webview_sin )
    {
        NSLog(@"main.webView  is nil!!!");
    }

    [main.webview_sin loadRequest:request];
}

      

+3


source to share


1 answer


when it's in the foreground it just navigates to the url without notice

This is expected behavior. No notifications are displayed if your app is running.

Instead, you can use UIAlertController

to show a message to the user



-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo 
{
    if (application.applicationState == UIApplicationStateActive)        
    {               
        UIAlertController *alertController = [UIAlertController
                          alertControllerWithTitle:@"Notification"
                          message:/* Get the message from APS */
                          preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancelAction = [UIAlertAction 
        actionWithTitle:@"Dismiss"
                  style:UIAlertActionStyleCancel
                handler:^(UIAlertAction *action)
                {
                    //Do Nothing
                }];
        [alertController addAction:cancelAction];
        [/*pick an appropriate view controller */ presentViewController:alertController animated:YES completion:nil];
    }    
}

      

Some code was adapted from here

0


source







All Articles