有没有办法打开推送通知中收到的网址,而无需在ios 10中打开应用程序? [英] Is there a way to open urls received in a push notification without opening the app in ios 10?

查看:87
本文介绍了有没有办法打开推送通知中收到的网址,而无需在ios 10中打开应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to open a url that is passed in a push notification in ios 10. So far I haven't found a way to open a the url without opening the app. Is there a way to open urls received in a push notification without opening the app?

我找到了解决方法来打开网址(工作) -os适用于ios< 10),但之后应用程序首先打开。

I have found a work-around to open the url (the work-around works for ios <10), but then again the app opens up first.

更新:


我注意到对于设备(iOS 10.0)
以下的心理学家已经转移了
应用程序:didRegisterForRemoteNotificationsWithDeviceToken
userNotificationCenter:willPresentNotification:
userNotificationCenter:didReceiveNotificationResponse:

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

But -application:didReceiveRemoteNotification:fetchCompletionHandler: and -application:didReceiveRemoteNotification: aren't called.

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

I'm new to ios and this is how far I have gotten:

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


推荐答案

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

Hopefully you can't control the behaviour of the push before launching the app. You get control over the push only after the app launched.

这篇关于有没有办法打开推送通知中收到的网址,而无需在ios 10中打开应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆