A Titanium module for registering a device with GCM and handling notifications sent to the device.
Android platform are using c2dm for push notification, but because c2dm have stopped accept new user, so we have decide to use the new service that google provided which is Google Cloud Messaging (GCM). Current Latest version is 0.5
Integrated vibration and sound property when sending message from server. Example php message:
$gcm = new GCM(); $registatoin_ids = array($regId); $message = array(title => 'title', message => $message, ticker => $message, vibrate => 1, sound => 'default'); $result = $gcm->send_notification($registatoin_ids, $message);
Add these to your tiapp.xml file inside the ti:app group.
<property name="com.activate.gcm.sender_id" type="string">?????</property>
<property name="com.activate.gcm.icon" type="int">?????</property>
<property name="com.activate.gcm.component" type="string">?????</property>
This is your sender ID from Google (not your server's API key).
This is the icon that will be shown in the Android notification centre.
public static final int appicon=
[number] in decimal
)This is a path to the activity that will be launched when the Android user taps to open a notification.
A Titanium App would normall launch the main activity.
The format is:
<package name>/<package name>.<activity name>
eg:
com.appcelerator.notificationsample/com.appcelerator.notificationsample.NotificationSampleActivity
(the main activity name always ends in "Activity")
For a similar case, see the Titanium documentation:
http://docs.appcelerator.com/platform/latest/#!/api/Titanium.Android.NotificationManager
"The Intent object associated with the notification determines what happens when the user clicks on the notification. In this case, we simply launch the application. The intent in this sample uses a className to identify the activity to launch."
"A Titanium application's main activity generally has the class name:"
<app_id>.<Appname>Activity
"For example, in this case the test application has the app ID com.appcelerator.notificationsample and app name NotificationSample, so the activity class name is:"
com.appcelerator.notificationsample.NotificationSampleActivity
"You can also find the activity's class name by looking in the build/android folder of your Titanium project."
For a notification to appear (in the notification center), include these properties of the the data
object of a standard GCM HTTP post:
To add optional alert sounds and vibrations use:
true
or a string with the wav file name (optional)true
(optional)For a silent notification containing data only, simply do not include the properties above. The data will still be received by the app.
JSON example:
{ "data": {
"title": "Title",
"message": "Message",
"sound": true,
"vibrate": "sound.wav",
},
"registration_ids": ["4", "8", "15", "16", "23", "42"]
}
For more info, see: http://developer.android.com/google/gcm/http.html
You have to get the android sdk manager, and download the google gcm from there.
Get the ANT 1.8 from >> http://ant.apache.org/bindownload.cgi, ANT 1.8 use to create module project and compile use for titanium use.
Using terminal to create module project for titanium by runing the command below.
/Users/Library/Application\ Support/Titanium/mobilesdk/osx/1.8.2/titanium.py create --platform=android --type=module --id=YOUR_PROJECT_ID --android=YOUR_ANDROID_SDK_PATH
For example: --id=com.activate.gcm, --android=/Applicatoins/android-sdk
Once the module project create success, copy the gcm/gcm-client/src folder from you have downloaded at Android SDK Manager into the module src folder.
Please make changes of the sdk path at the build.properties, such as titanium.platoform, android.platform, google.apis, and android.ndk
Now what you want to do is create the interface class between Java native code with titanium javascript using the Kroll Method. For more information please read here:
https://wiki.appcelerator.org/display/guides/Android+Module+Development+Guide
You also can take my source code as reference on how to create the interface. The source code is provided in this project which inside /src folder.
After complete setup all the class and interface, using ANT 1.8 to compile the module. To compile the module, we have to compile on build.xml. Now, navigate the ANT using terminal and type the command as follow.
/Users/Desktop/ANT/apache-ant-1.8.4/bin/ant -buildfile /Users/Desktop/gcm/build.xml
After all success compile, the terminal should prompt BUILD SUCCESSFUL, and navigate to folder /dist, inside have the zip file and jar file.
Finally, you have complete and success create the module, now look at "How to use this module at titanium."