很多朋友都反馈,发现了iOS9升级到iOS10推送功能不正常的问题,所以这篇文章总结了一下要点,亲们可以根据以下步骤,逐步排查问题,也可以逐步实现iOS10的推送功能。下面来一起看看吧。
1、在项目 target 中,打开Capabilitie —> Push Notifications,并会自动在项目中生成 .entitlement 文件。(很多同学升级后,获取不到 deviceToken,大概率是由于没开这个选项)
Capabilitie —> Push Notifications
自动生成 .entitlement
2、确保添加了 UserNotifications.framework,并 import到 AppDelegate,记得实现 UNUserNotificationCenterDelegate 。
#import <UserNotifications/UserNotifications.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate,UNUserNotificationCenterDelegate>
@end
3、在 didFinishLaunchingWithOptions 方法中,首先实现 UNUserNotificationCenter delegate,并使用 UIUserNotificationSettings 请求权限。
//注意,关于 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);
}
注意:需要根据系统版本号来判断是否使用新的 UserNotifications.framework,因此,不要着急删除 iOS 10 以前的代码。
总结
以上就是关于iOS10添加推送功能时的注意点和问题总结,希望这篇文章对大家开发iOS推送功能能有所帮助,如果有疑问大家可以留言交流。
本文标题为:iOS10实现推送功能时的注意点和问题总结
- 详解flutter engine 那些没被释放的东西 2022-12-04
- Android MaterialButton使用实例详解(告别shape、selector) 2023-06-16
- Android实现监听音量的变化 2023-03-30
- iOS 对当前webView进行截屏的方法 2023-03-01
- SurfaceView播放视频发送弹幕并实现滚动歌词 2023-01-02
- 作为iOS开发,这道面试题你能答出来,说明你基础很OK! 2023-09-14
- Android实现轮询的三种方式 2023-02-17
- 最好用的ios数据恢复软件:PhoneRescue for Mac 2023-09-14
- Flutter实现底部和顶部导航栏 2022-08-31
- Android studio实现动态背景页面 2023-05-23
