iOS Push Notification Redirect to View when App becomes Active

點選推播通知,開啟 app 並跳到特定的 view。
首先參考這篇,起碼要先接得到通知訊息。

第一種狀況,當 app 沒開啟也沒在背景執行,接收到推播的時候會觸發didFinishLaunchingWithOptions
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  // register for remote notification
  [application registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound];
  
  // app 如果在沒啟動的狀態下(前景/背景都無),點"推播通知"後,會將推播資料以 launchOptions 傳入。
  NSDictionary *remoteNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
  if (remoteNotification) {
    [self application:application didReceiveRemoteNotification:remoteNotification];
  }

  return YES;
}
第二種狀況,當 app 在背景執行時,接收到推播的時候會觸發didReceiveRemoteNotification
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
  // This function called when receive notification and app is in the foreground.
  for (id key in userInfo) {
    trace(@"Key=[%@], Value=[%@]", key, [userInfo objectForKey:key]);
  }
  
  NSString *openType = [userInfo objectForKey:@"open"];
  NSString *link = [userInfo objectForKey:@"link"];
  [[HVPApiManager sharedManager] saveNotificationWithOpenType:openType withLink:link];
  
  /* Output Badge number */
  trace(@"Badge: %@", [[userInfo objectForKey:@"aps"] objectForKey:@"badge"]);
}