源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

iOS10实现推送功能时的注意点和问题总结

  • 时间:2020-09-19 15:24 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:iOS10实现推送功能时的注意点和问题总结
1、在项目 target 中,打开[code]Capabilitie —> Push Notifications[/code],并会自动在项目中生成 .entitlement 文件。(很多同学升级后,获取不到 deviceToken,大概率是由于没开这个选项) [img]http://files.jb51.net/file_images/article/201609/2016925114506563.png?2016825114518[/img] Capabilitie —> Push Notifications [img]http://files.jb51.net/file_images/article/201609/2016925114556479.png?201682511463[/img] 自动生成 .entitlement 2、确保添加了[code] UserNotifications.framework[/code],并 [code]import[/code]到 [code]AppDelegate[/code],记得实现 [code]UNUserNotificationCenterDelegate [/code]。
#import <UserNotifications/UserNotifications.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate,UNUserNotificationCenterDelegate>
@end
3、在 [code]didFinishLaunchingWithOptions [/code]方法中,首先实现 [code]UNUserNotificationCenter delegate[/code],并使用 [code]UIUserNotificationSettings [/code]请求权限。
//注意,关于 iOS10 系统版本的判断,可以用下面这个宏来判断。不能再用截取字符的方法。
#define SYSTEM_VERSION_GRATERTHAN_OR_EQUALTO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{

if(SYSTEM_VERSION_GRATERTHAN_OR_EQUALTO(@"10.0")){
 UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
 center.delegate = self;
 [center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error){
  if( !error ){
  [[UIApplication sharedApplication] registerForRemoteNotifications];
  }
 }]; 
}

return YES;
}
4、最后实现以下两个回调。
//====================For iOS 10====================

-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{
NSLog(@"Userinfo %@",notification.request.content.userInfo);

//功能:可设置是否在应用内弹出通知
completionHandler(UNNotificationPresentationOptionAlert);
}

//点击推送消息后回调
-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler{
NSLog(@"Userinfo %@",response.notification.request.content.userInfo);
}
[b]注意:[/b]需要根据系统版本号来判断是否使用新的 [code]UserNotifications.framework[/code],因此,不要着急删除 iOS 10 以前的代码。 [b]总结[/b] 以上就是关于iOS10添加推送功能时的注意点和问题总结,希望这篇文章对大家开发iOS推送功能能有所帮助,如果有疑问大家可以留言交流。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部