【问题标题】:How to use same notification on Background Foreground in APNS如何在 APNS 的背景前景上使用相同的通知
【发布时间】:2015-06-26 06:36:33
【问题描述】:

我正在开发 APNS。 当我发送 APNS 时,提供 url 并移动 url。 APNS 成功,但是当应用程序运行时,它无法在前台收到通知。 然而,在后台,它的工作。当它在前台时 它只是在没有通知的情况下移动到 url。 你能帮帮我吗?

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

【问题讨论】:

  • 根据发送本地通知或显示警报检查didRecieveRemoteNotification中的应用程序状态。

标签: ios apple-push-notifications


【解决方案1】:

当它在前台时,它只是在没有通知的情况下移动到 url

这是预期的行为。如果您的应用正在运行,则不会显示通知。

您可以改为使用 UIAlertController 向用户显示消息

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

部分代码改编自here

【讨论】:

  • 感谢您的帮助。但是我需要在顶部栏上通知不是警报或对话框..你能再帮我一次吗..?
  • 顶栏没有内置视图。您必须自己构建一个或使用第三方库。
猜你喜欢
  • 2020-05-28
  • 1970-01-01
  • 2014-08-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-08
  • 2020-06-20
  • 2015-02-05
相关资源
最近更新 更多