Skip to content

Commit

Permalink
Add files
Browse files Browse the repository at this point in the history
  • Loading branch information
morinel committed Jun 12, 2015
1 parent b0b4ba1 commit 576ccad
Show file tree
Hide file tree
Showing 21 changed files with 861 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
@@ -0,0 +1,3 @@
build
dist
libs
15 changes: 15 additions & 0 deletions README.md
@@ -0,0 +1,15 @@
# Titanium Module for Google Cloud Messaging Push Notifications for Android #

A Titanium module for registering a device with GCM and handling push notifications sent to the device.

1. Installation
1. Sending Notifications (from a server)
1. Steps to build module for Titanium (Mac Users) (for people that who want to create their own gcm module)
1. Documents

## Installation

1. Install the module as usual in Titanium Studio by downloading the zip file from the dist directory
1. Refer to the example for possibilities
1. Send a server push notification with your preferred server-side technology to the registrationId returned while registering your device.
1. The callback you specified will then be called
6 changes: 6 additions & 0 deletions assets/README
@@ -0,0 +1,6 @@
Place your assets like PNG files in this directory and they will be packaged with your module.

If you create a file named com.activate.gcm.js in this directory, it will be
compiled and used as your module. This allows you to run pure Javascript
modules that are pre-compiled.

10 changes: 10 additions & 0 deletions build.xml
@@ -0,0 +1,10 @@
<project name="gcm" default="dist">
<description>
Ant build script for Titanium Android module gcm
</description>

<property name="ti.module.root" location="${basedir}"/>
<property file="build.properties" />

<import file="${titanium.platform}/../module/android/build.xml"/>
</project>
39 changes: 39 additions & 0 deletions documentation/index.md
@@ -0,0 +1,39 @@
# gcm Module

## Description

TODO: Enter your module description here

## Accessing the gcm Module

To access this module from JavaScript, you would do the following:

var gcm = require("com.activate.gcm");

The gcm variable is a reference to the Module object.

## Reference

TODO: If your module has an API, you should document
the reference here.

### ___PROJECTNAMEASIDENTIFIER__.function

TODO: This is an example of a module function.

### ___PROJECTNAMEASIDENTIFIER__.property

TODO: This is an example of a module property.

## Usage

TODO: Enter your usage example here

## Author

TODO: Enter your author name, email and other contact
details you want to share here.

## License

TODO: Enter your license/legal information here.
98 changes: 98 additions & 0 deletions example/PushNotification.js
@@ -0,0 +1,98 @@
/**
* @author Activate
*/


exports.pushNotification = function()
{
var hostURL = "http://YOUR_HOST_URL/register?";

var gcm = require('com.activate.gcm');
Ti.API.info('module gcm is => ' + gcm);

Ti.API.info('Registering...');

gcm.registerC2dm
(
{
success:function(e)
{
Ti.API.info('JS registration success event: ' + e.registrationId);
alert('Yeah JS registration success event: ' + e.registrationId);
alert('Sender ID:' + gcm.getSenderId());
alert('Registration ID:' + gcm.getRegistrationId());

var appName = Ti.App.name;
var appVersion = Ti.App.version;

var deviceUUID = Ti.Platform.macaddress; // Ti.Network.remoteDeviceUUID;
var deviceName = Ti.Platform.username;
var deviceModel = Ti.Platform.model;
var deviceSystemVersion = Ti.Platform.version;
var deviceToken = e.deviceToken;
var regId = e.registrationId;

//deviceUUID = deviceUUID.replace(/-/ig,'');
//deviceToken = deviceToken.replace(/<>/ig, '');


var host = hostURL;
var urlString = host;
/*urlString += "&appname=" + appName;
urlString += "&appversion=" + appVersion;
urlString += "&deviceuid=" + deviceUUID;
urlString += "&devicetoken=" + deviceToken;
urlString += "&devicename=" + deviceName;
urlString += "&devicemodel=" + deviceModel;
urlString += "&deviceversion=" + deviceSystemVersion;*/
urlString += "regId=" + regId;

var loader = Ti.Network.createHTTPClient();
loader.setTimeout(60000);

loader.onload = function(evt)
{
alert(evt);
}

loader.open('GET', urlString, false);
loader.send();

alert(urlString);
},
error:function(e)
{
Ti.API.error("Error during registration : " + e.error);
alert("Error during registration : " + e.error);

var message;
if(e.error == "ACCOUNT_MISSING")
{
message = "No Google account found; you will need to add on in order to activate notifications";
}

Titanium.UI.createAlertDialog
(
{
title:'Push Notification Setup',
message:message,
buttonNames:['OK']
}
).show();
},
callback:function(e) // called when a push notification is received
{
Ti.API.info('JS message event: ' + JSON.stringify(e.data));
alert('JS message event: ' + JSON.stringify(e.data));

//same as e.data
//var data = Ti.App.Properties.getString("com.activate.gcm.last_data","");
//data = JSON.parse(data);
//Ti.App.Properties.removeProperty("com.activate.gcm.last_data");
//Ti.App.Properties.hasProperty("com.activate.gcm.last_data");
//Ti.Android.NotificationManager.cancelAll();

}
}
);
}
46 changes: 46 additions & 0 deletions example/app.js
@@ -0,0 +1,46 @@
var gcm = require("nl.vanvianen.android.gcm");

/* If the app is started or resumed act on pending data saved when the notification was received */
var lastData = gcm.getLastData();
if (lastData) {
Ti.API.info("Last notification received " + JSON.stringify(lastData));
gcm.clearLastData();
}

gcm.registerPush({
/* The Sender ID from Google Developers Console, see https://console.developers.google.com/project/XXXXXXXX/apiui/credential */
/* It's the same as your project id */
senderId: 'XXXXXXXX',
notificationSettings: {
sound: 'mysound.mp3', /* Place in platform/android/res/raw/mysound.mp3 */
smallIcon: 'notification_icon.png', /* Place in platform/android/res/drawable/notification_icon.png */
largeIcon: 'appicon.png', /* Same */
vibrate: true
},
success: function (event) {
Ti.API.info("Push registration success: " + JSON.stringify(event));
/* Add code to send event.registrationId to your server */
},
error: function (event) {
Ti.API.info("Push registration error = " + JSON.stringify(event));
alert(event.error);
},
callback: function (event) {
Ti.API.info("Push callback = " + JSON.stringify(event));
/* Called when a notification is received and the app is in the foreground */

var dialog = Ti.UI.createAlertDialog({
title: 'Push received',
message: JSON.stringify(event.data)
buttonNames: ['View'],
cancel: 1
});
dialog.addEventListener("click", function(event) {
dialog.hide();
if (event.index == 0) {
/* Do stuff to view the notification */
}
});
dialog.show();
}
});
52 changes: 52 additions & 0 deletions example/tiapp.xml
@@ -0,0 +1,52 @@
<?xml version="1.0" encoding="UTF-8"?>
<ti:app xmlns:ti="http://ti.appcelerator.org">
<deployment-targets>
<target device="mobileweb">false</target>
<target device="iphone">false</target>
<target device="ipad">false</target>
<target device="android">true</target>
<target device="blackberry">false</target>
</deployment-targets>
<sdk-version>2.1.2.GA</sdk-version>
<id>com.activate.mohh</id>
<name>MOHH</name>
<version>1.0.1</version>
<publisher>activate</publisher>
<url>http://www.activateplay.com</url>
<description>not specified</description>
<copyright>2012 by activate</copyright>
<icon>appicon.png</icon>
<persistent-wifi>false</persistent-wifi>
<prerendered-icon>false</prerendered-icon>
<statusbar-style>default</statusbar-style>
<statusbar-hidden>false</statusbar-hidden>
<fullscreen>false</fullscreen>
<navbar-hidden>false</navbar-hidden>
<analytics>true</analytics>
<guid>65b97566-a444-4a96-87ff-ebcc007586e7</guid>
<property name="ti.ui.defaultunit">system</property>
<iphone>
<orientations device="iphone">
<orientation>Ti.UI.PORTRAIT</orientation>
</orientations>
<orientations device="ipad">
<orientation>Ti.UI.PORTRAIT</orientation>
<orientation>Ti.UI.UPSIDE_PORTRAIT</orientation>
<orientation>Ti.UI.LANDSCAPE_LEFT</orientation>
<orientation>Ti.UI.LANDSCAPE_RIGHT</orientation>
</orientations>
</iphone>
<android xmlns:android="http://schemas.android.com/apk/res/android">
<tool-api-level>8</tool-api-level>
<manifest android:versionCode="1" android:versionName="1.0" package="com.activate.mohh">
<supports-screens android:anyDensity="true"/>
<uses-sdk android:minSdkVersion="8"/>
</manifest>
<modules>
<module platform="android" version="0.3">com.activate.gcm</module>
</modules>
</android>
<property name="com.activate.gcm.sender_id" type="string">00000000</property> <!-- put your sender_id here -->
<property name="com.activate.gcm.icon" type="int">2130837504</property> <!-- get this from R.java, but converted to decimal, 0x7f020000 give you 2130837504 (type "0x7f020000 to decimal" in google) -->
<property name="com.activate.gcm.component" type="string">gcm.gcm/gcm.gcm.GcmActivity</property> <!-- [package name]/[package name].[activity name] -->
</ti:app>
1 change: 1 addition & 0 deletions hooks/README
@@ -0,0 +1 @@
These files are not yet supported as of 1.4.0 but will be in a near future release.
35 changes: 35 additions & 0 deletions hooks/add.py
@@ -0,0 +1,35 @@
#!/usr/bin/env python
#
# This is the module project add hook that will be
# called when your module is added to a project
#
import os, sys

def dequote(s):
if s[0:1] == '"':
return s[1:-1]
return s

def main(args,argc):
# You will get the following command line arguments
# in the following order:
#
# project_dir = the full path to the project root directory
# project_type = the type of project (desktop, mobile, ipad)
# project_name = the name of the project
#
project_dir = dequote(os.path.expanduser(args[1]))
project_type = dequote(args[2])
project_name = dequote(args[3])

# TODO: write your add hook here (optional)


# exit
sys.exit(0)



if __name__ == '__main__':
main(sys.argv,len(sys.argv))

19 changes: 19 additions & 0 deletions hooks/install.py
@@ -0,0 +1,19 @@
#!/usr/bin/env python
#
# This is the module install hook that will be
# called when your module is first installed
#
import os, sys

def main(args,argc):

# TODO: write your install hook here (optional)

# exit
sys.exit(0)



if __name__ == '__main__':
main(sys.argv,len(sys.argv))

34 changes: 34 additions & 0 deletions hooks/remove.py
@@ -0,0 +1,34 @@
#!/usr/bin/env python
#
# This is the module project remove hook that will be
# called when your module is remove from a project
#
import os, sys

def dequote(s):
if s[0:1] == '"':
return s[1:-1]
return s

def main(args,argc):
# You will get the following command line arguments
# in the following order:
#
# project_dir = the full path to the project root directory
# project_type = the type of project (desktop, mobile, ipad)
# project_name = the name of the project
#
project_dir = dequote(os.path.expanduser(args[1]))
project_type = dequote(args[2])
project_name = dequote(args[3])

# TODO: write your remove hook here (optional)

# exit
sys.exit(0)



if __name__ == '__main__':
main(sys.argv,len(sys.argv))

18 changes: 18 additions & 0 deletions hooks/uninstall.py
@@ -0,0 +1,18 @@
#!/usr/bin/env python
#
# This is the module uninstall hook that will be
# called when your module is uninstalled
#
import os, sys

def main(args,argc):

# TODO: write your uninstall hook here (optional)

# exit
sys.exit(0)


if __name__ == '__main__':
main(sys.argv,len(sys.argv))

Binary file added lib/gcm-server.jar
Binary file not shown.
Binary file added lib/gson-2.3.1.jar
Binary file not shown.

0 comments on commit 576ccad

Please sign in to comment.