美文网首页
程序中控制是否接收推送通知

程序中控制是否接收推送通知

作者: JunkieBa | 来源:发表于2016-10-12 15:54 被阅读0次

    程序中控制是否接收推送通知

    标签:IOS推送通知取消接收推送APNSiphone

    如果只需要一个开关,可以控制通知中心的开关

    关用[[UIApplicationsharedApplication]unregisterForRemoteNotifications];

    开用[[UIApplicationsharedApplication]registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound|UIRemoteNotificationTypeAlert)];

    苹果没有相关的接口

    这个功能一般是配合服务端来实现的

    服务端保存用户的设置比如是否接受push,以及push接受时段,当客户端的设置更新时,把改动提交给服务器端。

    服务器端需要发新的推送时检查下用户的设置,用户是不是接受推送,以及现在是不是用户接受推送的时间段来决定是不是要给这个用户推消息

    APNS是iOS的消息推送机制,网上有很好的资料,请查看最后的参考列表,这里强调一些关键步骤。

    一、生成证书。

    与苹果的APNS Server交互涉及到iOS设备和提供Push服务的Provider,它们均需要证书进行验证。iOS设备与APNS的交互是通过底层进行的,应用程序本身并不需要引用什么证书,但是你的应用是否能够成功注册消息通知,会与你项目中的Bundle identifier有关,你指定的Bundle identifier所属的Provisioning Profile下的AppID必须开通了"EnableforApple Push Notification service",如下图:

    从图可见,分开了Development和Production,生成Provider证书时,如果是开发环境生成Development的证书,如果是生产环境生成Production的证书。

    Bundle identifier --> Provision Profile --> AppID --> EnableforApple Push Notification service --> Push SSL Certificate

    可见,虽然iOS应用并不需要加载下载下来的证书,但实际上它也是存在与其对应的证书的,只是通过Bundle identifier标识。

    注意,如果之前没有为对应的AppID开通APNS,此时就需要下载新的Provision

    Profile并重新安装,重装Profile的方法很简单,打开Organizer窗口,删除对应的Provision

    Profile文件,然后把新下载的文件拖拉到窗口中就OK了。

    如果要在应用程序中调试通过还需要做些配置,否则会出现如下错误:

    “no valid aps-environment entitlement foundforapplication”

    我们打开刚才下载下来的新Provision Profile文件,其中有一段配置:

    我们需要在iOS项目中添加Entitlements配置,步骤如下:

    1、开启并添加配置。

    2、也可以指定使用Entitlements.entitlements文件,通过以下方法添加文件:

    New Files --> Code Siging -->Entitlements --> Next...

    文件的内容和上述相同,在配置中选择此文件即可。

    3、订阅和退订。

    iOS设备对APNS进行订阅或退订消息通知的行为,可以在*AppDelegate.m中的application:didFinishLaunchingWithOptions:方法进行,也可以在别的*ViewController.m的某个触发事件中进行。

    订阅:

    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge|

    UIRemoteNotificationTypeSound|UIRemoteNotificationTypeAlert)];

    退订:

    [[UIApplication sharedApplication] unregisterForRemoteNotifications];

    如果想要避免每次启动都执行订阅和接收DeviceToken的操作,可以进行如下判断:

    if([[UIApplication sharedApplication] enabledRemoteNotificationTypes]

    {

    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge|

    UIRemoteNotificationTypeSound|UIRemoteNotificationTypeAlert)];

    }

    这样可能只会在第一次安装程序的时候才会订阅并接收到DeviceToken,如果后来调用了unregisterForRemoteNotifications,也不会再次执行了。

    4、Delegate方法。

    当调用registerForRemoteNotifications方法,或者应用程序接收到来源自于APNS的通知时都会触发相关的

    delegate方法,因为这些delegate方法定义在UIApplicationDelegate中,所以实现需要做

    在*AppDelegate.m类中。例如:

    注意:通过 application:didRegisterForRemoteNotificationsWithDeviceToken:获取

    到DeviceToken后需要推送给提供Push服务的Provider,Provider保存好此应用的uuid对应的token,以便在发送消息的

    时候使用。

    如何生成.NET所需证书的可以根据这里的步骤来做。

    二、Provider开发。

    可以通过apns-sharp这个开源库进行开发,另外还有很多相关的文章详细解说其开发原理,例如这几篇文章:

    Local and Push Notification Programming Guide: About LocalNotificationsand PushNotifications

    Local and Push Notification Programming Guide: Provider Communication with Apple Push Notification Service

    Programming Apple Push Notification Services

    Apple Push Notification Service

    另外,还有PushMeBaby供XCode下开发测试的Provider

    相关文章

      网友评论

          本文标题:程序中控制是否接收推送通知

          本文链接:https://www.haomeiwen.com/subject/dayiyttx.html