zoukankan      html  css  js  c++  java
  • Apple Push Notification service

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

    一、生成证书。

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

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

    Bundle identifier --> Provision Profile --> AppID --> Enable for Apple Push Notification service --> Push SSL Certificate

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

    注意,如果之前没有为对应的AppID开通APNS,此时就需要下载新的Provision Profile并重新安装,重装Profile的方法很简单,打开Organizer窗口,删除对应的Provision Profile文件,然后把新下载的文件拖拉到窗口中就OK了。

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

    “no valid aps-environment entitlement found for application”

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

        <key>Entitlements</key>
    <dict>
    <key>application-identifier</key>
    <string>7C57XDWF8L.com.xxx.appname</string>
    <key>aps-environment</key>
    <string>development</string>
    <key>get-task-allow</key>
    <true/>
    <key>keychain-access-groups</key>
    <array>
    <string>7C57XDWF8L.*</string>
    </array>
    </dict>

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

    1、开启并添加配置。

    .xcodeproj --> Summary --> Entitlements节 --> 选中"Enable Entitlements"

    些时会自动生成一个${PRODUCT_NAME}.entitlements的文件,在文件中添加键值对:

    Key   Type      Value
    get-task-allow   Bollean      YES
    aps-environment   String   development   这行是必须的
    application-identifier   String   7C57XDWxxx.com.xxx.appname
    keychain-access-groups   Array

      Item0 String 7C57XDWxxx.*

    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类中。例如:

    //注册push服务
    - (void)application:(UIApplication *) application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *) deviceToken {
    NSString
    *token = [[deviceToken description] stringByTrimmingCharactersInSet:
        
    [NSCharacterSet characterSetWithCharactersInString:@"<>"]]; //去掉"<>"
        token = [[token description] stringByReplacingOccurrencesOfString:@" " withString:@""];//去掉中间空格
    NSLog(@"deviceToken: %@", token);
    }

    //注册push服务失败
    - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    NSLog(
    @"Error in registration for APNS. Error: %@", error);
    }

    //接收到push消息
    - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    NSLog(
    @"收到推送消息 : %@",[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]);
    UIAlertView
    * alert = [[UIAlertView alloc] initWithTitle:@"推送通知"
    message:[[userInfo objectForKey:
    @"aps"] objectForKey:@"alert"]
    delegate:self
    cancelButtonTitle:
    @"关闭"
    otherButtonTitles:
    @"更新状态",nil];
    [alert show];
    [alert release];
    }

    注意:通过 application:didRegisterForRemoteNotificationsWithDeviceToken:获取到DeviceToken后需要推送给提供Push服务的Provider,Provider保存好此应用的uuid对应的token,以便在发送消息的时候使用。

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

    二、Provider开发。

    可以通过apns-sharp这个开源库进行开发,另外还有很多相关的文章详细解说其开发原理,例如这几篇文章:
    Local and Push Notification Programming Guide: About Local Notifications and Push Notifications
    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




  • 相关阅读:
    docker volume
    Nginx 安装配置
    Shell test 命令,Shell 输入/输出重定向
    Shell 变量,Shell echo命令
    MongoDB数据库
    linux yum 命令
    Linux 磁盘管理,Linux vi/vim
    Python----Paramiko模块和堡垒机实战
    Linux 文件与目录管理,Linux系统用户组的管理
    Linux 忘记密码解决方法,Linux 远程登录
  • 原文地址:https://www.cnblogs.com/chenjunbiao/p/2119259.html
Copyright © 2011-2022 走看看