天天看點

iOS10下app運作中,但是處于背景,點選通知問題

/**
 *  當app完全離線狀态,點選通知欄的通知,會調用該方法此時launchOptions有值
 */
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    //APNS注冊通知
    if ([UIDevice currentDevice].systemVersion.doubleValue<) {
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeBadge)];
    }
    else {
        [[UIApplication sharedApplication] registerForRemoteNotifications];
        [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert categories:nil]];
    }
    NSLog(@"%@",launchOptions);
    //判斷是否由遠端消息通知觸發應用程式啟動
    if (launchOptions) {
        //擷取應用程式消息通知标記數(即小紅圈中的數字)
        NSInteger badge = [UIApplication sharedApplication].applicationIconBadgeNumber;
        self.window.rootViewController.view.backgroundColor = [UIColor redColor];
        if (badge>) {
            //如果應用程式消息通知标記數(即小紅圈中的數字)大于0,清除标記。
            badge--;
            //清除标記。清除小紅圈中數字,小紅圈中數字為0,小紅圈才會消除。
            [UIApplication sharedApplication].applicationIconBadgeNumber = badge;
            NSDictionary *pushInfo = [launchOptions objectForKey:@"UIApplicationLaunchOptionsRemoteNotificationKey"];



            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:[NSString stringWithFormat:@"%@,您可以在消息設定那裡進行檢視",[[pushInfo objectForKey:@"aps"] objectForKey:@"alert"]] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];

        }
    }

    [UIApplication sharedApplication].applicationIconBadgeNumber = ;
    return YES;
}

/**
 *  當app出于前台的時候,接收到消息,會調用下面這個方法.
 */

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{

    NSLog(@"%s", __func__);
    self.window.rootViewController.view.backgroundColor = [UIColor greenColor];

}
/**
 *  當app在運作中但是出于背景的時候,點選通知欄的通知,app從背景回到前台會觸發下面這個方法而不是上面這個
 *  雖說該方法提示已經過期,這是不是iOS10的一個bug呢?
 */

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    NSLog(@"%s", __func__);
    self.window.rootViewController.view.backgroundColor = [UIColor greenColor];

}
           
iOS10下app運作中,但是處于背景,點選通知問題
iOS