在Titanium应用中导入Push通知(iOS篇)

[size=large][b]让你的Titanium应用也能实现Push的功能吧。[/b][/size]

[img]http://dl.iteye.com/upload/attachment/611973/8878f86d-5a62-3849-8bf2-cd55652114d2.jpg[/img]

[size=large][b]1)申请证书(.p12文件)[/b] [/size]http://developer.apple.com/devcenter/ios/index.action

Log into Apple's provisioning portal and create a new appid. Take note of your bundle identifier or appid, use the same one form the last step
[img]http://dl.iteye.com/upload/attachment/611994/9cc046ce-63a5-35c5-ae31-678eba3304f3.png[/img]

When you create your application, you need to configure it and enable push notifications. In your application list, click configure for the app you just created.

[img]http://dl.iteye.com/upload/attachment/611996/2a9e5b8c-9d4e-30a7-b4df-fbe55cae5dcb.png[/img]

Now check the box to enable the notification service and click the configure box next to "Development Push SSL Certificate". Note that usually you setup 2 different certificates - one for development and one for production. This maps directly to your development provisioning profile and distribution provisioning profile.

[img]http://dl.iteye.com/upload/attachment/611998/cfd61d57-5f03-32ca-bc3d-11463055a2ef.png[/img]

This will prompt us for a certificate

[img]http://dl.iteye.com/upload/attachment/612000/1348d260-36d4-300b-a728-a84aefda3e0b.png[/img]

On your machine, open up your keychain and then click "Request a Certificate from a Certificate Authority..."

[img]http://dl.iteye.com/upload/attachment/612002/c6032178-badd-3e8f-a21a-49663c01589b.png[/img]

Put in your email address and common name and make sure that you check "Saved to disk"

[img]http://dl.iteye.com/upload/attachment/612004/26e7dcf0-748f-3f44-a3d1-a6e5823f79c5.png[/img]

Now in your browser, select the certificate request we just generated and submit the request. Once its been uploaded, you will see a status of pending, just refresh until the download button appears.

[img]http://dl.iteye.com/upload/attachment/612006/928e534d-983c-364e-89b8-e98fe55e582c.png[/img]

Click the download button for the certificate and once downloaded to your local machine, open it. This will bring up your keychain app with the certificate listed.

[img]http://dl.iteye.com/upload/attachment/612008/8e59f7e5-9409-35ea-864e-4b732310aebe.png[/img]

Right click the certificate and click "Export Apple Development Push Services: xxxx"

[img]http://dl.iteye.com/upload/attachment/612018/97e0d6d3-8b6e-3da1-ba9e-2632e85e8eff.png[/img]

When you export, p12 format will work just fine (the default)

[img]http://dl.iteye.com/upload/attachment/612014/3f2a0bc3-a9a2-3a20-beee-8208ce34ef84.png[/img]

You will be prompted for a passphrase for the p12 file, you should definitely put something in here (urban airship requires this). Make sure that you take note of the passphrase as we'll need that later

[img]http://dl.iteye.com/upload/attachment/612016/ac15fefb-e2fe-3ea3-9f08-6fe51705127f.png[/img]

[size=large][b]2)将证书放在服务器端的任意文件夹下 [/b][/size]

[size=large][b]3)java版服务器为例,需要的Lib:[/b][/size]
apns-0.1.5-jar-with-dependencies.jar
slf4j-simple-1.6.1.jar

代码如下
==================================

【服务器端】


ApnsService service = APNS.newService()
.withCert(CER_PATH, CER_PASS)// CER_PATH是放证书的路劲 CER_PASS是证书的密码
.withSandboxDestination()// 调试模式
//.withProductionDestination() // 产品模式(调试和产品模式只能使用一种)
.build();

String payload = APNS
.newPayload()
.sound("default") // 收到Push信息的声音
.badge(1) // 桌面图标上表示的数字
.alertBody("A: This is a message") // 消息 最大 256bytes(包含自定义变量)
.customField("accountId", "1") // 自定义变量 (可以任意多个)
.build();

// device_token is 64 bytes hex letter.
// device_token indicates a unique user.
service.push(<<device_token>>, payload); // Push Notification。(没有返回值,只能扑捉NetworkIOException来判断异常)


[color=blue][b][size=large]PHP的话可以参考[url=http://code.google.com/p/apns-php/]apns-php[/url][/size][/b][/color]

【客户端】

Ti.Network.registerForPushNotifications({
types: [
Ti.Network.NOTIFICATION_TYPE_BADGE,
Ti.Network.NOTIFICATION_TYPE_ALERT,
Ti.Network.NOTIFICATION_TYPE_SOUND
],
success:function(e) {
Ti.API.debug("Push Notification success: " + JSON.stringify(e));
var deviceToken = e.deviceToken;
Ti.API.debug('successfully registered for apple device token with '+ deviceToken);
Ti.App.Properties.setString(KEY_DEVICE_TOKEN, deviceToken);
},
error:function(e) {
Ti.API.warn("push notifications disabled: "+ JSON.stringify(e));
},
callback:function(e) {
// 处理接收到的消息
processNotification(e);
}
});

这里有一篇文章是介绍iPhone Push的。[url=http://www.cocoachina.com/newbie/basic/2010/0401/900.html]iPhone的Push(推送通知)功能原理浅析[/url]

[color=red][size=xx-large][b]关于Android版的Push机能[/b][/size][/color]
=============================================

在Android 2.2版本FroYo之前, Google的Push机制直接就是利用XMPP协议的extension,也就是在<message>元素下加入自定义的子元素, 但自从FroYo, Android引入了一个新的框架C2DM(Cloud to Device Messaging), 而Google的Pushing 机制变成使用C2DM框架.
相关的介绍:[url=http://mysuperbaby.iteye.com/blog/902054]通过代码及流程图说明Google在Android上的Push机制的实现[/url]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值