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();
}
- iOS推送 (百度推送)
- iOS推送 (百度推送)
- iOS- 推送极光推送
- iOS推送
- IOS推送
- ios推送。。。
- IOS推送
- ios 推送
- IOS 推送
- iOS推送
- IOS推送
- IOS推送
- IOS推送
- IOS推送
- iOS 推送
- ios推送
- iOS 推送
- IOS推送
- TP5模型修改器和读取器
- PymongoDB
- 关于解决jsp页面无法正常解析EL表达式的问题
- 每天几道面(笔)试题
- SpringMVC+JWT+Swagger UI+RestFul
- iOS 推送
- CheckedTextView 使用
- 安装mysql5.7.17-二进制包形式
- eclipse UML插件
- 我眼中的光明·第八周·豫卦上六·三
- HDU 6098 Inversion(RMQ)
- 异步加载
- windows服务启动一个当前用户的进程
- 深入浅出es6之箭头函数