点击通知栏上的通知不要自动打开链接

时间:2014-07-21 04:44:33

标签: ios push-notification apple-push-notifications

我的应用程序使用了APNS的通知。当服务器通过链接向客户端发送通知时。我点击通知栏上的通知,应用程序将在webview上打开通知链接。我的问题是,当应用程序运行活动或后台时,它运行正常并加载链接确定。但是,当应用程序不活动时,我点击通知,它将不会在通知中加载链接,它只会在NSUserDefaults中加载旧链接或链接" http://staging.nhomxe.vn"。这是我的代码:

APPDELEGATE.m

- (void)application:(UIApplication*)application 
didReceiveRemoteNotification:
(NSDictionary*)userInfo

{

NSLog(@"Received notification: %@", userInfo);
NSDictionary *data = [ userInfo objectForKey:@"aps"];
for(NSString *key in data) {
    NSString *info = [data objectForKey:key];
    NSLog(@"thong tin nhan dc: %@ : %@", key, info);
}

NSString *message = [userInfo valueForKey:@"link"] ;
//NSArray *info = [message componentsSeparatedByString:@"&@"];
//NSString *body = [info objectAtIndex:0];
//NSString *link = [info objectAtIndex:1];
NSLog(@"Thong tin Link: %@",message);
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setValue:message forKey:@"LINK"];

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Warning"
 message:message
 delegate:nil
 cancelButtonTitle:@"OK"
 otherButtonTitles:nil, nil];
 [alertView show];

ViewController *vc = (ViewController *)self.window.rootViewController;
NSURL *url = [NSURL URLWithString:message];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[vc.webView loadRequest:urlRequest];
[vc.webView3 loadRequest:urlRequest];

}

MYVIEWCONTROLLER.m

- (void)viewDidLoad

{   NSString *link = NULL;
NSUserDefaults *data = [NSUserDefaults standardUserDefaults];
link = [data objectForKey:@"LINK"];
NSString *connect = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://staging.nhomxe.vn"] encoding:NSUTF8StringEncoding error:nil];
if(connect == NULL)
{
    NSLog(@"Server hiện tại đang bảo trì. Ứng dụng sẽ đóng ngay bây giờ.!");
    UIAlertView * alert =[[UIAlertView alloc ] initWithTitle:@"Warning"
                                                     message:@"Server hiện tại đang bảo trì. Ứng dụng sẽ đóng ngay bây giờ."
                                                    delegate:self
                                           cancelButtonTitle:@"OK"
                                           otherButtonTitles: nil];
    alert.tag = 1;
    [alert show];

}else
{   if(link == NULL)
{

    NSString *linkWeb = @"http://staging.nhomxe.vn";
    NSURL *url = [NSURL URLWithString:linkWeb];
    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
    [self.webView loadRequest:urlRequest];
    [self.webView3 loadRequest:urlRequest];
}else{

    NSURL *url = [NSURL URLWithString:link];
    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
    [self.webView loadRequest:urlRequest];
    [self.webView3 loadRequest:urlRequest];
    //[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"LINK"];
    //[[NSUserDefaults standardUserDefaults] synchronize];
    //NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    //[defaults setValue:NULL forKey:@"LINK"];
}

}
// Schedule the runScheduledTask in 5 seconds
aTimer = [NSTimer scheduledTimerWithTimeInterval:30.0 target:self selector:@selector(runScheduledTask) userInfo:nil repeats:YES];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

2 个答案:

答案 0 :(得分:1)

我怀疑您没有代码从通知中启动应用程序。

看看- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions。如果应用从通知开始,您将拥有launchOptions

如果您有远程通知,则UIApplicationLaunchOptionsRemoteNotificationKey中会有launchOptions个密钥。

  

UIApplicationLaunchOptionsRemoteNotificationKey

     

此密钥的存在表示可以使用远程通知   应用程序来处理。这个键的值是   NSDictionary包含远程通知的有效负载。看到   应用程序的描述:didReceiveRemoteNotification:for   有关处理远程通知的更多信息。

答案 1 :(得分:1)

当App未在后台运行时

- (void)application:(UIApplication*)application 
didReceiveRemoteNotification:
(NSDictionary*)userInfo

不会被称为你应该像这样处理你的数据..

在你的AppDelegate.m中,在didFinishLaunchingWithOptions方法中执行类似这样的操作

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

  NSDictionary* userInfo = [launchOptions valueForKey:@"UIApplicationLaunchOptionsRemoteNotificationKey"];
    NSDictionary * data = [userInfo objectForKey:@"aps"];


for(NSString *key in data) {
    NSString *info = [data objectForKey:key];
    NSLog(@"thong tin nhan dc: %@ : %@", key, info);
}

//.…COntinue with your Execution So on…. You will get the data in the Data Dictionary which you are looking for 
}