iOS远程推送之(三):点击通知横幅启动应用

导读

iOS远程推送之(一):APNs原理和基本配置
iOS远程推送之(二):角标applicationIconNumber设置

点击横幅启动应用

我们应用的启动方式有很多种,主要的方式如下:

  • 一种是在HomeScreen上正常手动点击图标打开进入到应用中
  • 一种是通过其他应用调起该应用
  • 一种是通过点击通知横幅来启动应用

简说:

  • 第一种很正常的在didFinishLaunchingWithOptions里面处理就OKay的
  • 第二种是通过openURL的方式打开的,那么在对应的openURL方法来处理相关内容的
  • 第三种是通过判断didFinishLaunchingWithOptions中的参数launchOptions中的UIApplicationLaunchOptionsRemoteNotificationKey来处理相关的消息内容

PS: UIApplicationLaunchOptionsRemoteNotificationKey的说明如下:

The presence of this key indicates that a remote notification is available for the app to process. 
释义:这个键的存在表明,远程通知是可供应用的过程。
The value of this key is an NSDictionary containing the payload of the remote notification. 
释义:这个字典当中的这个键对应的值包含了推送消息负载(payload)
See the description of application:didReceiveRemoteNotification: for further information about handling remote notifications.
释义:看这个方法的描述能够进一步的了解处理盐城通知的相关信息

代码说明:

  • 第一种:didFinishLaunchingWithOptions
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.rootViewController = [[ViewController alloc] init];
    [self.window makeKeyAndVisible];
    
//code here...
    return YES;
}
  • 第二种openURL:
//这个是最新的iOS9之后的
-(BOOL) application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary *)options
{
    [[KODSDK defaultSDK]  kodDealWithApplication:app openURL:url sourceApplication:options[@"UIApplicationOpenURLOptionsSourceApplicationKey"] annotation:options[@"UIApplicationOpenURLOptionsOpenInPlaceKey"]];
    return YES;
}

//这个是iOS9之前的,9之后废弃
-(BOOL) application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    [[KODSDK defaultSDK]  kodDealWithApplication:application openURL:url
                                       sourceApplication:sourceApplication annotation:annotation];
    
    return YES;
}
  • 第三种:UIApplicationLaunchOptionsRemoteNotificationKey
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.rootViewController = [[ViewController alloc] init];
    [self.window makeKeyAndVisible];
    
    NSDictionary *userInfo = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];
    if (userInfo != nil) {
        //如果有值,说明是通过远程推送来启动的
        //[UIApplication sharedApplication].keyWindow是拿不到值的
    }
    return YES;
}

PS:以上中keyWindow是拿不到值的,所以这里的话我一般的做法是在根控制器创建完成之后发送一个通知出来,然后Appdelegate中接收通知,然后实现跳转对应的控制器或者界面

扩展:launch_options_keys

UIApplicationLaunchOptionsURLKey

UIApplicationLaunchOptionsSourceApplicationKey

UIApplicationLaunchOptionsRemoteNotificationKey

UIApplicationLaunchOptionsLocalNotificationKey

UIApplicationLaunchOptionsAnnotationKey

UIApplicationLaunchOptionsLocationKey

UIApplicationLaunchOptionsNewsstandDownloadsKey

UIApplicationLaunchOptionsBluetoothCentralsKey

UIApplicationLaunchOptionsBluetoothPeripheralsKey

UIApplicationLaunchOptionsShortcutItemKey

UIApplicationLaunchOptionsUserActivityDictionaryKey

UIApplicationLaunchOptionsUserActivityTypeKey

你可能感兴趣的:(iOS远程推送之(三):点击通知横幅启动应用)