IOS 消息推送原理及实现总结

来源:互联网 发布:php面向对象程序设计 编辑:程序博客网 时间:2024/05/25 12:21
IOS消息推送原理及实现总结中讲述了消息推送的原理及实现总结,但并未讲到Provider服务端及Client App客户端的实现,但我在这里只是简单讲述Provider服务端(JAVA实现)步骤如下:

一、 制作Provider服务端所需要的*.p12文件:

IOS消息推送原理及实现总结的图2-8中所展示的密钥、证书,我们并没有使用到它,那么为什么要展示出来呢,猜猜是为什么?为制作*.p12文件?哈哈,您猜对了,下面我们就利用它来制作*.p12文件,步骤:([MacBookà应用程序à实用工具à钥匙串访问[钥匙串:登录,种类:证书]à选择刚刚生成的证书(Apple Development IOS Services:*******)à菜单à文件à导出项目à[存储为:任意文件名(如:iPush),文件格式:默认(个人信息交换(.p12)]à输入密码以进行导出[密码:任意,验证:与密码同一]à输入MACBook登录密码à允许à最终生成。关键步骤如图1-1

1-1

二、 编写Client App客户端的关键代码,如下:

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

{

   self.window = [[[UIWindowalloc]initWithFrame:[[UIScreenmainScreen]bounds]]autorelease];

   self.window.backgroundColor = [UIColorwhiteColor];

    [self.windowmakeKeyAndVisible];

   //消息推送支持的类型

   UIRemoteNotificationType types =

    (UIRemoteNotificationTypeBadge

     |UIRemoteNotificationTypeSound

     |UIRemoteNotificationTypeAlert);

   //注册消息推送

    [[UIApplicationsharedApplication]registerForRemoteNotificationTypes:types];

   returnYES;

}

//获取DeviceToken成功

- (void)application:(UIApplication *)application

didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken

{

   NSLog(@"DeviceToken: {%@}",deviceToken);

   //这里进行的操作,是将Device Token发送到服务端

}

//注册消息推送失败

- (void)application:(UIApplication *)application

didFailToRegisterForRemoteNotificationsWithError:(NSError *)error

{

   NSLog(@"Register Remote Notifications error:{%@}",[errorlocalizedDescription]);

}

//处理收到的消息推送

- (void)application:(UIApplication *)application

didReceiveRemoteNotification:(NSDictionary *)userInfo

{

   NSLog(@"Receive remote notification : %@",userInfo);

   UIAlertView *alert =

    [[UIAlertViewalloc]initWithTitle:@"温馨提示"

                              message:@"推送成功!"

                             delegate:nil

                    cancelButtonTitle:@"确定"

                    otherButtonTitles:nil];

    [alertshow];

    [alertrelease];

}

三、编写Provider服务端关键代码,如下:

import javapns.back.PushNotificationManager;

import javapns.back.SSLConnectionHelper;

import javapns.data.Device;

import javapns.data.PayLoad;

publicclass MainSend

{

   publicstaticvoid main(String[] args)throws Exception

    {

       try

        {

           //从客户端获取的deviceToken

            String deviceToken ="3a20764942e9cb4c4f6249274f12891946bed26131b686b8aa95322faff0ad46";

            System.out.println("Push Start deviceToken:" + deviceToken);

           //定义消息模式

            PayLoad payLoad =new PayLoad();

            payLoad.addAlert("消息推送测试!");

            payLoad.addBadge(4);

            payLoad.addSound("default");

           //注册deviceToken

            PushNotificationManager pushManager = PushNotificationManager.getInstance();

            pushManager.addDevice("iPhone", deviceToken);

           //连接APNS

            String host ="gateway.sandbox.push.apple.com";

           int port = 2195;

            String path ="/Users/iMilo/Work.localized/iShop/project/service/iPush/";

            String certificatePath = (path +"src/ipush/iPush.p12");

           //certificatePath步骤一中生成的*.p12文件位置

            String certificatePassword ="Love24mm";

            pushManager.initializeConnection(host, port, certificatePath, certificatePassword, SSLConnectionHelper.KEYSTORE_TYPE_PKCS12);

           //发送推送

            Device client = pushManager.getDevice("iPhone");

            pushManager.sendNotification(client, payLoad);

           //停止连接APNS

            pushManager.stopConnection();

           //删除deviceToken

            pushManager.removeDevice("iPhone");

            System.out.println("Push End");

        }

       catch (Exception ex)

        {

            ex.printStackTrace();

        }

    }

}

注意:如果Provider服务端为Objective-C实现的话,就不需要*.p12证书。下面给出网上的相应demo地址:

Provider服务端(JAVA实现):apns_iphone.zip

Provider服务端(Objective-C实现):PushMeBabySource.zip

 

原创粉丝点击