【转】Appcelerator Cloud Push Notification in Android

[color=orange][b]What is Push Notification?[/b][/color]
Push notification allows an app to notify you of new messages or events without the need to actually open the application, similar to how a text message will make a sound and pop up on your screen. This is a great way for apps to interact with us in the background, whether it be a game notifying us of some event occurring in our game world or simply the iPad's mail application beeping as a new message appears in our inbox. [url=http://ipad.about.com/od/iPad-Glossary/g/What-Is-Push-Notification.htm]read more[/url]

[color=orange][b]Push Notification in Titanium Using Cloud Service[/b][/color]
We can achieve Push Notification in Titanium using Appcelerator Cloud [list][*]Service in 5 steps.
[*]Cloudpush Module Implementation
[*]Retrieve Device Token
[*]Cloud User Login
[*]Subscribe a Channel
[*]Push Configuration[/list]

[color=orange]1)Cloudpush Module Implementation[/color]
Cloudpush provides methods for accessing Android push notifications from ACS using the MQTT protocol. require cloudpush in your app and add module in your tiapp.xml

// add this line in tiapp.xml
<module platform="android">ti.cloudpush</module>
// add this line in app.js
var CloudPush = require('ti.cloudpush');

[img]http://dl.iteye.com/upload/attachment/0070/6357/9d1eed23-f263-30f1-9c10-3e1b872da8f2.png[/img]

[color=orange]2)Retrieve Device Token[/color]
You can Retrieve Device Token using below code

CloudPush.retrieveDeviceToken({
success: function deviceTokenSuccess(e) {
alert('Device Token: ' + e.deviceToken);
deviceToken = e.deviceToken
},
error: function deviceTokenError(e) {
alert('Failed to register for push! ' + e.error);
}
});


[color=orange]3)Cloud User Login[/color]
Before subscribe for Push Notification, cloud user should logged in.
So create a test user in Appcelerator Cloud Console
[b]My Apps -> Manage ACS -> DEVELOPMENT -> Users[/b]
and login with credential. Use below code for cloud user login

[img]http://dl.iteye.com/upload/attachment/0070/6359/410b7dfd-2a1c-3d1d-bc87-c67e9076f2cc.png[/img]

Cloud.Users.login({
login: 'push123',
password: 'push123'
}, function (e) {
if (e.success) {
alert("login success");
} else {
alert('Error: ' +((e.error && e.message) || JSON.stringify(e)));
}
});


[color=orange]4)Subscribe a Channel[/color]
Add following code for channel subscribtion

Cloud.PushNotifications.subscribe({
channel: 'alert', // "alert" is channel name
device_token: deviceToken,
type: 'android'
}, function (e){
if (e.success) {
alert('Subscribed for Push Notification!');
}else{
alert('Error:' +((e.error && e.message) || JSON.stringify(e)));
}
});


[color=orange]5)Android Push Configuration[/color]
It is final step. In Appcelerator Cloud Console go to
[b]My Apps -> Manage ACS -> DEVELOPMENT -> Settings/Android Push Configuration[/b] and enter the Application Package in the text box. Your pakage name should be similar to com.xxx.androidpush Here xxx is company name and androidpush is Application name

[img]http://dl.iteye.com/upload/attachment/0070/6361/63a5e122-4f5a-3b9d-a18f-00302f70120a.png[/img]

Cool.., You have completed Android Push Notification setup([url=https://gist.github.com/2909981]Here is the whole sample code[/url]). This time for testing, run the application in android device and click the button "[b]Android Cloud Push Notification[/b]". You will get 3 alerts continuously.

Then go to [b]My Apps -> Manage ACS -> DEVELOPMENT -> Push Notifications[/b], here you can see "[b]1 Android clients subscribed to push notifications[/b]"

[img]http://dl.iteye.com/upload/attachment/0070/6363/ba83e092-e635-31c7-94ee-b9d2c5877edc.png[/img]


It is time to send push notification, enter the values and hit the button "Send Push Notification" instantly you will receive notification in your Android device(with default icon and sound)

[color=orange][b]How to customize ACS Push notification?[/b][/color]
Here you can use use your custom sounds and icons.

[b]Icon[/b]
custom icons must be placed in below directory
[i][b]App_root/platform/android/res/drawable/myicon.png [/b] [/i]

Here "myicon.png" is custom icon, specified in the push message without the filename extension. For example: in icon text box enter the file name without extension(myicon)

[b]Sound[/b]
Custom icons must be placed in below directory
[i][b]App_root/Resources/sound/beep.wav[/b][/i]

Here "beep.wav" is custom sound, specified in the push message with the filename extension. For example: in Sound/Custom/beep.wav enter custom sound filename in the text box(with extension). Here is the screens of custom notification

[img]http://dl.iteye.com/upload/attachment/0070/6365/d877c0c3-60f8-37e8-b552-01252d85a8c1.png[/img][img]http://dl.iteye.com/upload/attachment/0070/6367/819f3833-1e4b-3a9c-bc7d-5588b7933208.png[/img]


Here you can download the complete working project from my Github [url=https://github.com/railskarthi/acs_android_push]Appcelerator Titanium Android Push Notification[/url].

原文:[url=http://www.titaniumtutorial.com/2012/06/appcelerator-cloud-push-notification-in.html]这里[/url]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值