阅读更多 What is Push Notification?
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. read more

Push Notification in Titanium Using Cloud Service
We can achieve Push Notification in Titanium using Appcelerator Cloud
  • Service in 5 steps.
  • Cloudpush Module Implementation
  • Retrieve Device Token
  • Cloud User Login
  • Subscribe  a Channel
  • Push Configuration

1)Cloudpush Module Implementation
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.xmlti.cloudpush // add this line in app.jsvar CloudPush = require('ti.cloudpush'); 



2)Retrieve Device Token
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);     }});


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



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)));     }});


4)Subscribe a Channel
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)));    }});


5)Android Push Configuration
It is final step. In Appcelerator Cloud Console go to
My Apps -> Manage ACS -> DEVELOPMENT -> Settings/Android Push Configuration 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



Cool.., You have completed Android Push Notification setup( Here is the whole sample code). This time for testing, run the application in android device and click the button " Android Cloud Push Notification". You will get 3 alerts continuously.

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




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)

How to customize ACS Push notification?
Here you can use use your custom sounds and icons.

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

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)

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

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




Here you can download the complete working project from my Github Appcelerator Titanium Android Push Notification.

原文: 这里
  • 大小: 41 KB
  • 大小: 16 KB
  • 大小: 9.4 KB
  • 大小: 27.3 KB
  • 大小: 42.2 KB
  • 大小: 57.5 KB
  • 查看图片附件

更多相关文章

  1. android图片压缩并转为base64字符串
  2. LowMemoryKiller in Android
  3. First project in android
  4. 【转】Android(安卓)SDK版本与API Level对照表
  5. android 双击图片放大缩小
  6. Android上运行QT项目Necessitas的基本原理
  7. Android从相机或相册获取图片裁剪
  8. Android(安卓)drawableleft如何设置图片大小
  9. ImageView设置图片大小

随机推荐

  1. Android标题栏TitleBar全攻略
  2. execlp启动android进程命令窗口通过adb s
  3. Android EditView属性详细介绍
  4. Android 面试题
  5. Ubuntu上开发Android
  6. Android - 开发者应该深入学习的10个开源
  7. Android之Android(安卓)N多窗口
  8. Android之——多线程断点续传下载示例
  9. Android基础_页面布局_TableLayout(表格
  10. Android之AsyncTask异步任务详解总结