当前位置:首页 > 通信资讯 > 正文

ios推送设置(ios10通知)

1、在项目 target 中,打开capabilitie —> push notifications,并会自动在项目中生成 .entitlement 文件。(很多同学升级后,获取不到 devicetoken,大概率是由于没开这个选项)

ios推送设置(ios10通知)
capabilitie —> push notifications

ios推送设置(ios10通知)
自动生成 .entitlement

2、确保添加了 usernotifications.framework,并 importappdelegate,记得实现 unusernotificationcenterdelegate

?
1 2 3 4 #import <usernotifications/usernotifications.h> @interface appdelegate : uiresponder <uiapplicationdelegate,unusernotificationcenterdelegate> @end

3、在 didfinishlaunchingwithoptions 方法中,首先实现 unusernotificationcenter delegate,并使用 uiusernotificationsettings 请求权限。

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 //注意,关于 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、最后实现以下两个回调。

?
1 2 3 4 5 6 7 8 9 10 11 12 13 //====================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推送功能能有所帮助,如果有疑问大家可以留言交流。

原文链接:http://www.jianshu.com/p/e5a45a7e80a7

如果您对该产品感兴趣,请填写办理(客服微信:xiaoxiongyidong)

为您推荐:

发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。