APN推送通知踩过的坑

1、对接收到的通知的处理

情况1、通过点击通知启动APP,处理接收到的通知

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    // 点击推送信息启动APP
    NSDictionary *notification = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];
    if (notification && 0 != notification.count)
    {
          // do something
         NSLog(@"notification = %@", notification);
    }
    
    return YES;
}

情况2、已经启动APP,重新激活APP,处理接收到的通知

情况3、已经启动APP,且已经激活APP,处理接收到的通知

// iOS8.0以下方法
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
        // apn 内容获取:
        // 取得 APNs 标准信息内容
        // do something
        NSLog(@"userInfo = %@", userInfo);
}

// iOS8.0及以上方法
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
        // do something
        NSLog(@"userInfo = %@", userInfo);
      
        if (application.applicationState == UIApplicationStateActive)
    {
        // 程序当前正处于前台
    }
    else if (application.applicationState == UIApplicationStateInactive)
    {
        // 程序处于后台进入前台
    }
}




你可能感兴趣的:(iOS,通知)