2017-01-27 10 views
0

Я пытаюсь открыть URL-адрес, который передается в push-уведомлении в ios 10. До сих пор я не нашел способ открыть URL-адрес, не открывая приложение. Есть ли способ открыть URL-адреса, полученные в push-уведомлении без открытия приложения?Есть ли способ открыть URL-адреса, полученные в push-уведомлении, не открывая приложение в ios 10?

Я нашел обход, чтобы открыть URL-адрес (обходные работы для ios < 10), но затем приложение открывается первым.

Update:

Я заметил, что для устройства (IOS 10.0) Следующие mentohds получают involked применение: didRegisterForRemoteNotificationsWithDeviceToken userNotificationCenter: willPresentNotification: userNotificationCenter: didReceiveNotificationResponse :

Но -приложение: didReceiveRemoteNotification: fetchCompletionHandler: and -application: didReceiveRemoteNotification: не.

Я новичок в ИОС, и это, как далеко я получил:

AppDelegate.h

#import <UIKit/UIKit.h> 
#import <UserNotifications/UserNotifications.h> 

@interface AppDelegate : UIResponder <UIApplicationDelegate,UNUserNotificationCenterDelegate> 

@property (strong, nonatomic) UIWindow *window; 


@end 

AppDelegate.m

#import "AppDelegate.h" 

@interface AppDelegate() 


@end 

@implementation AppDelegate 


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
// Override point for customization after application launch. 
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 10) 
{ 
    UNUserNotificationCenter * notificationCenter = [UNUserNotificationCenter currentNotificationCenter]; 
    notificationCenter.delegate = self; 
    [notificationCenter requestAuthorizationWithOptions:UNAuthorizationOptionAlert|UNAuthorizationOptionBadge|UNAuthorizationOptionSound completionHandler:^(BOOL granted, NSError * error) 
    { 
     [[UIApplication sharedApplication] registerForRemoteNotifications]; 
     if (error) 
     { 
      NSLog(@"Auth. error:%@",[error localizedDescription]); 
     } 
    }]; 
    [notificationCenter getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * settings) { 

    }]; 

} 
else 
{ 
    UIUserNotificationType type = UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound; 
    UIUserNotificationSettings * settings = [UIUserNotificationSettings settingsForTypes:type categories:nil]; 
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings]; 
    [[UIApplication sharedApplication] registerForRemoteNotifications]; 


} 

return YES; 
} 
... 
#pragma mark Push Notification methods 
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler 
{ 
NSLog(@"didReceiveRemoteNotification:"); 
NSString * message = [[userInfo valueForKey:@"aps"] valueForKey:@"alert"]; 
NSString * urlString = [[userInfo valueForKey:@"aps"] valueForKey:@"alert"]; 
NSLog(@"1 Received Push URL: %@", urlString); 

NSURL * url = [NSURL URLWithString:urlString]; 

if(url!=nil) 
{ 
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 10) { 
     // iOS 10 and above 
     [[UIApplication sharedApplication] openURL:url options:[NSDictionary dictionary] completionHandler:nil]; 
    } 
    else 
    { 
     [[UIApplication sharedApplication] openURL:url]; // iOS <10 
    } 
} 

if (application.applicationState == UIApplicationStateInactive) 
{ 
    NSLog(@"Application inactive"); 
    [[NSUserDefaults standardUserDefaults] setValue:message forKey:@"Push_Message"]; 
} 
else if (application.applicationState == UIApplicationStateBackground) 
{ 
    NSLog(@"Application in background"); 
} 
else 
{ 
    NSLog(@"Application active"); 
} 
} 
#pragma mark Notification Registration methods 
-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken 
{ 
NSString* token = [[[[deviceToken description] 
        stringByReplacingOccurrencesOfString: @"<" withString: @""] 
        stringByReplacingOccurrencesOfString: @">" withString: @""] 
        stringByReplacingOccurrencesOfString: @" " withString: @""]; 

NSLog(@"didRegisterForRemoteNotificationsWithDeviceToken:\n%@",token); 

} 
-(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error 
{ 
NSLog(@"Error:%@",[error localizedDescription]); 
NSLog(@"Suggest:%@",[error localizedRecoverySuggestion]); 
} 
#pragma mark App Push notification methods 

-(void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings 
{ 
NSLog(@"didRegisterUserNotificationSettings"); 
} 
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo 
{ 
NSLog(@"UserInfo: %@",userInfo); 
NSString * messageString = [[userInfo valueForKey:@"aps"] valueForKey:@"alert"]; 
NSLog(@"Message:%@",messageString); 
NSString * messageurl = [[userInfo valueForKey:@"aps"] valueForKey:@"alert"]; 
NSLog(@"2 Received Push URL: %@", messageurl); 

NSURL * url = [NSURL URLWithString:messageurl]; 

if(url!=nil) 
{ 
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 10) { 
     // iOS 10 and above 
     [[UIApplication sharedApplication] openURL:url options:[NSDictionary dictionary] completionHandler:nil]; 
    } 
    else 
    { 
     [[UIApplication sharedApplication] openURL:url]; // iOS <10 
    } 
} 

[[NSNotificationCenter defaultCenter] postNotificationName:@"PushMessage" object:self userInfo:@{@"alertString":messageString}]; 
} 
#pragma mark UNUserNotificationCenterDelegate methods 

-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler 
{ 
NSLog(@"didReceiveNotificationResponse"); 
//--URL click--// 

//Kindly suggest what can be done here? 
completionHandler(UNNotificationPresentationOptionAlert); 
} 
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler 
{ 
NSLog(@"willPresentNotification"); 
completionHandler(UNNotificationPresentationOptionAlert); 
} 


@end 
+1

Я думаю, вы можете использовать молчаливое оповещение для этой цели. Молчаливое push-уведомление может быть получено без уведомления пользователя, а также когда приложение находится в фоновом режиме. Пожалуйста, обратитесь к http://stackoverflow.com/a/36695473/1922188 –

+0

Я настроил фоновый режим для получения уведомления. Но я хочу, чтобы пользователь щелкнул уведомление, а затем перенаправлен на URL-адрес, но без открытия приложения. Это возможно в android, но возможно ли это в ios? – Prashant

ответ

0

Надеемся вы не можете контролировать поведение push до la дешифрование приложения. Вы получаете контроль над нажатием только после запуска приложения.