有没有办法打开推送通知中收到的 url 而无需在 ios 10 中打开应用程序?

Posted

技术标签:

【中文标题】有没有办法打开推送通知中收到的 url 而无需在 ios 10 中打开应用程序?【英文标题】:Is there a way to open urls received in a push notification without opening the app in ios 10? 【发布时间】:2017-01-27 05:50:52 【问题描述】:

我正在尝试打开一个在 ios 10 中通过推送通知传递的 url。 到目前为止,我还没有找到一种在不打开应用程序的情况下打开 url 的方法。 有没有办法在不打开应用程序的情况下打开推送通知中收到的网址?

我找到了打开 url 的变通方法(该变通方法适用于 ios

更新:

我注意到对于设备 (iOS 10.0) 调用以下方法 应用程序:didRegisterForRemoteNotificationsWithDeviceToken userNotificationCenter:willPresentNotification: userNotificationCenter:didReceiveNotificationResponse:

但是 -application:didReceiveRemoteNotification:fetchCompletionHandler: 和 -application:didReceiveRemoteNotification: 没有被调用。

我是 ios 新手,这就是我已经走了多远:

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

【问题讨论】:

我想您可以为此使用静默推送通知。当应用程序处于后台模式时,可以在不让用户知道的情况下接收静默推送通知。请参考***.com/a/36695473/1922188 我已配置后台模式接收通知。但我希望用户单击通知,然后被重定向到 url,但没有打开应用程序。 android可以,ios可以吗? 【参考方案1】:

希望您在启动应用程序之前无法控制推送行为。只有在应用启动后,您才能控制推送。

【讨论】:

以上是关于有没有办法打开推送通知中收到的 url 而无需在 ios 10 中打开应用程序?的主要内容,如果未能解决你的问题,请参考以下文章

收到推送通知时在后台执行代码

当推送通知到达(未收到)设备时调用委托

在Android中打开Activity而不点击通知

应用打开时显示本机推送通知?

尝试通过推送通知在 AppDelegate 中打开 SFSafariViewController

收到来自 Ionic 推送的通知时如何打开到特定状态?