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.xml<module platform="android">ti.cloudpush</module> // 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.

原文: 这里

更多相关文章

  1. Android(安卓)Studio 生成UML类图
  2. 原文:Android(安卓)Theme XML
  3. multipart data using Android(安卓)Volley
  4. android edittext 去边框
  5. Android(安卓)复习笔记之图解Activity的4种加载模式
  6. Android中Style和Theme的使用
  7. -Android各版本系统源代码下载
  8. 最好的Android(安卓)apps,Android(安卓)apps推荐下载
  9. Android(安卓)Fresco图片处理库用法API英文原文文档2-2(Facebook

随机推荐

  1. Android 添加快捷方式
  2. Android适用于IM通知音频的Vibrator
  3. Android(安卓)studio 点击按钮跳转到新的
  4. android 与其他程序交互(一)
  5. android 对软键盘的显示和消失监听
  6. 安装android studio 不能启动
  7. android图片上传springMvc
  8. TextInputLayout,让输入框更有灵性
  9. android kill package
  10. Android 常用的SDCARD和内存操作