iOS 推送

来源:互联网 发布:mac文件排序 编辑:程序博客网 时间:2024/06/06 11:18

if ([[UIDevice currentDevice].systemVersion floatValue] >=10.0) {

    //iOS10

    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];

    //代理

    center.delegate = self;

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

        if (granted) {

            //允许

          

            [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings *_Nonnull settings) {

               

            }];

        } else {

            //不允许

        }

    }];

    //获得deviceToken

    [application registerForRemoteNotifications];

}else if ([[UIDevice currentDevice].systemVersion floatValue] >=8.0){

    //iOS8~iOS10

    [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge categories:nil]];

    //获得deviceToken

    [application registerForRemoteNotifications];

}




#pragma mark ---- 推送相关

//注册推送通知之后-->再次接收deviceToken

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

{    [self addDeviceToken:deviceToken];

 

}


//获取Device token失败后

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

    NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);

}


//添加DeviceToken

-(void)addDeviceToken:(NSData *)deviceToken{

    NSData *oldToken = [[NSUserDefaults standardUserDefaults]objectForKey:kDeviceToke];

    if (![oldToken isEqualToData:deviceToken]) {

        NSLog(@"deviceToken不一样");

        [[NSUserDefaults standardUserDefaults]setObject:deviceToken forKey:kDeviceToke];

    }

}



//接收到推送通知之后


//ios7~iOS10远程

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

{

    completionHandler(UIBackgroundFetchResultNoData);

}

//ios7~iOS10收到本地推送点击进来的方法

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification

{

    [application cancelLocalNotification:notification];

}


//// ios10 通知的点击事件

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

    

    UNNotificationRequest *request = response.notification.request; //收到推送的请求

    UNNotificationContent *content = request.content; // 收到推送的消息内容

    //NSString *body = content.body;    // 推送消息体

    

    if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {

       //收到远程通知

    }else {

        // 判断为本地通知

    }

    completionHandler();

}