国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

iOS10下的推送(簡單處理)

2019-11-08 00:24:35
字體:
來源:轉載
供稿:網友

iOS10下的推送增加了一些新的東西,廢話不多說直接上代碼

1.首先肯定是要注冊通知,由于各個系統的注冊通知方法有不同,所以注冊也是要多做些判斷,兼容更多系統

/** 注冊遠程通知 */

- (void)registerRemoteNotification {

    if ([[UIDevicecurrentDevice].systemVersionfloatValue] >= 10.0) {

#if __ipHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 // Xcode 8編譯會調用

        UNUserNotificationCenter *center = [UNUserNotificationCentercurrentNotificationCenter];

        center.delegate =self;

        [center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge |UNAuthorizationOptionSound |UNAuthorizationOptionAlert |UNAuthorizationOptionCarPlay)completionHandler:^(BOOL granted,NSError *_Nullable error) {

            if (!error) {

                NSLog(@"request authorization succeeded!");

            }

        }];

        

        [[UIapplicationsharedApplication] registerForRemoteNotifications];

#else // Xcode 7編譯會調用

        UIUserNotificationType types = (UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge);

        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:nil];

        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];

        [[UIApplication sharedApplication] registerForRemoteNotifications];

#endif

    } elseif ([[[UIDevicecurrentDevice] systemVersion]floatValue] >= 8.0) {

        UIUserNotificationType types = (UIUserNotificationTypeAlert |UIUserNotificationTypeSound |UIUserNotificationTypeBadge);

        UIUserNotificationSettings *settings = [UIUserNotificationSettingssettingsForTypes:typescategories:nil];

        [[UIApplicationsharedApplication] registerUserNotificationSettings:settings];

        [[UIApplicationsharedApplication] registerForRemoteNotifications];

    } else {

        UIRemoteNotificationType apn_type = (UIRemoteNotificationType)(UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeSound |UIRemoteNotificationTypeBadge);

        [[UIApplicationsharedApplication] registerForRemoteNotificationTypes:apn_type];

    }

}

注意!!!!!:xcode8需要打開通知開關

按照上圖所示

2.上一步是注冊通知,下面就是接受注冊成功的deviceToken了,這里的deviceToken是NSData,所以要拿到字符串要做一些處理

// 注冊成功的代理方法

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {

    NSString *token = [[deviceTokendescription] stringByTrimmingCharactersInSet:[NSCharacterSetcharacterSetWithCharactersInString:@"<>"]];

    token = [token stringByReplacingOccurrencesOfString:@" "withString:@""];

    NSLog(@"/nDeviceToken:%@/n/n", token);

}

// 注冊失敗的代理方法

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {

   NSLog(@"/n>>>[DeviceToken Error]:%@/n/n", error.description);

}

3.注冊是搞定了,下面就要處理通知了。在處理通知方面,iOS10又增加了新的方法,不過老方法還是會調用

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

    

    // [ GTSdk ]:將收到的APNs信息傳給個推統計

    [GeTuiSdkhandleRemoteNotification:userInfo];

    //應用的當前狀態判斷

    if(application.applicationState ==UIApplicationStateInactive){

        //當你的應用在后臺時,當你點擊通知欄消息時,會進入此條件

    }

    // 控制臺打印接收APNs信息

    HWLog(@"/n>>>[Receive RemoteNotification:%@/n/n", userInfo);

    

    completionHandler(UIBackgroundFetchResultNewData);

}

注意:這個方法里最好加上應用狀態的判斷,這個枚舉值有三個

typedefNS_ENUM(NSInteger, UIApplicationState) {

    UIApplicationStateActive, //應用激活狀態

    UIApplicationStateInactive,//應用待激活狀態或將要激活

    UIApplicationStateBackground//應用在后臺

} NS_ENUM_AVAILABLE_IOS(4_0);

下面是iOS10新增加的兩個方法(這兩個方法就夠用)

#PRagma mark - iOS 10中收到推送消息

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0

//  iOS 10: App在前臺獲取到通知

- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {

    HWLog(@"willPresentNotification:%@", notification.request.content.userInfo);

    // 根據APP需要,判斷是否要提示用戶Badge、Sound、Alert

    completionHandler(UNNotificationPresentationOptionBadge |UNNotificationPresentationOptionSound |UNNotificationPresentationOptionAlert);

}

//  iOS 10: 點擊通知進入App時觸發

- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {

    

    HWLog(@"didReceiveNotification:%@", response.notification.request.content.userInfo);

    

    completionHandler();

}

#endif

其中,在第二個方法里做事就可以了,這個方法可以拿到APNS的數據,只是自己做好了狀態判斷,這些方法都是需要點擊通知欄進入app才會調用
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宁陕县| 太仓市| 阜南县| 深州市| 池州市| 梁平县| 新和县| 老河口市| 瑞昌市| 龙南县| 东乌| 长岛县| 南和县| 大同县| 遵化市| 丹寨县| 赫章县| 邵武市| 尼勒克县| 岚皋县| 犍为县| 平湖市| 鄯善县| 甘泉县| 霍林郭勒市| 西盟| 巴里| 焦作市| 西林县| 蚌埠市| 上高县| 白玉县| 松原市| 舒城县| 万盛区| 阿拉善左旗| 高淳县| 专栏| 顺义区| 苗栗县| 定日县|