Created
August 12, 2014 08:35
-
-
Save Amimul100/98dd3f25b0c5809f7875 to your computer and use it in GitHub Desktop.
ACS push notification for Android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Hello, Here is the Code sample and the steps to successfully configure and send an ACS push notification for Android. I have tested this and it's working. | |
| TESTING ENVIRONMENT | |
| CLI version 3.3.0, | |
| Titanium SDK version 3.3.0.GA | |
| Android Device Nexus -4 -4.4.2 | |
| API -19 | |
| STEP TO TEST | |
| To create a push notification enabled application: | |
| 1. At first you need to create a cloud and cloudpush enable application. | |
| * Create a new Classic Project With Cloud_- enabled. | |
| * Open your project's tiapp.xml file. | |
| * In the Modules section, click the Add button (green plus sign). | |
| * Select ti.cloudpush and click OK. | |
| 2. Creating a Google API project. | |
| * Go to this link (http://developer.android.com/google/gcm/gs.html#create-proj) Follow the instructions. | |
| * Get the (Project Number generated for your application The "sender ID") and the API Key. | |
| 3.To configure your app for push notifications in the web console (Community developers). | |
| * Open My Apps (https://cloud.appcelerator.com/apps) | |
| * Find your application in the list of apps and click Manage ACS. | |
| * Click Settings option. | |
| * In the Android Push Configuration section, enter your Google API key in the Google Cloud Messaging (GCM) API Keyfield and your GCM sender ID (Project Number) in the Google Cloud Messaging (GCM) Sender ID field. | |
| * Click Save Android Push Configuration Changes. | |
| 4. Create user and run the app in Android Device. | |
| * In the web console (In your app) create a user that you will use for login. | |
| * In the studio, Copy the code provided below "app.js" to your project "app.js" file | |
| * Copy the code provided below "tiapp.xml" to your project "tiapp.xml". | |
| * Provide the login details of the user you created, in your code. | |
| * Run the project in an android device. | |
| * Send a push notification through web console to test in your device. | |
| {code:app.js} | |
| Titanium.UI.setBackgroundColor('#000'); | |
| var win = Ti.UI.createWindow({ | |
| backgroundColor : '#ccc', | |
| title : 'Android Cloud Push Notification' | |
| }); | |
| var CloudPush = require('ti.cloudpush'); | |
| CloudPush.debug = true; | |
| CloudPush.enabled = true; | |
| CloudPush.showTrayNotificationsWhenFocused = true; | |
| CloudPush.focusAppOnPush = false; | |
| var deviceToken; | |
| var Cloud = require('ti.cloud'); | |
| Cloud.debug = true; | |
| var submit = Ti.UI.createButton({ | |
| title : 'Register For Push Notification', | |
| color : '#000', | |
| height : 53, | |
| width : 200, | |
| top : 100, | |
| }); | |
| win.add(submit); | |
| submit.addEventListener('click', function(e) { | |
| CloudPush.retrieveDeviceToken({ | |
| success : function deviceTokenSuccess(e) { | |
| alert('Device Token: ' + e.deviceToken); | |
| deviceToken = e.deviceToken; | |
| loginDefault(); | |
| }, | |
| error : function deviceTokenError(e) { | |
| alert('Failed to register for push! ' + e.error); | |
| } | |
| }); | |
| }); | |
| function loginDefault(e) { | |
| // At first you need to create an user from the application dashboard | |
| // Then login that email and password | |
| Cloud.Users.login({ | |
| login : 'Email', | |
| password : 'pass' | |
| }, function(e) { | |
| if (e.success) { | |
| alert("login success"); | |
| defaultSubscribe(); | |
| } else { | |
| alert('Error: ' + ((e.error && e.message) || JSON.stringify(e))); | |
| } | |
| }); | |
| } | |
| function defaultSubscribe() { | |
| Cloud.PushNotifications.subscribe({ | |
| channel : 'alert', | |
| 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))); | |
| } | |
| }); | |
| } | |
| CloudPush.addEventListener('callback', function(evt) { | |
| //alert(evt); | |
| //alert(evt.payload); | |
| }); | |
| CloudPush.addEventListener('trayClickLaunchedApp', function(evt) { | |
| Ti.API.info('Tray Click Launched App (app was not running)'); | |
| //alert('Tray Click Launched App (app was not running'); | |
| }); | |
| CloudPush.addEventListener('trayClickFocusedApp', function(evt) { | |
| Ti.API.info('Tray Click Focused App (app was already running)'); | |
| //alert('Tray Click Focused App (app was already running)'); | |
| }); | |
| win.open(); | |
| {code} | |
| {code:tiapp.xml} | |
| <property name="acs-push-type-development" type="string">gcm</property> | |
| <property name="acs-push-type-production" type="string">gcm</property> | |
| <property name="acs-push-type" type="string">gcm</property> | |
| {code} | |
| Thanks. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment