Skip to content

Commit

Permalink
Merge pull request #3 from chmiiller/master
Browse files Browse the repository at this point in the history
Adds support for additionalData using…
  • Loading branch information
williamrijksen authored Dec 2, 2016
2 parents eb69c41 + fa28984 commit 3b2e3ca
Show file tree
Hide file tree
Showing 11 changed files with 265 additions and 89 deletions.
42 changes: 41 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Before setting up the Titanium SDK, you must generate the appropriate credential
1. Register device for Push Notifications

```js
// This registers your device automatically into OneSignal
// This registers your device automatically into OneSignal
var onesignal = require('com.williamrijksen.onesignal');
```
1. To add the possibility to target people for notifications, send a tag:
Expand Down Expand Up @@ -75,4 +75,44 @@ Before setting up the Titanium SDK, you must generate the appropriate credential
visualLevel: onesignal.LOG_LEVEL_NONE
});
```
1. Receive notifications callback: (does not work on iOS when the app is closed (swiped away). But works fine when the app is running on background)
Opened:

```js
onesignal.addEventListener('notificationOpened', function (evt) {
alert(evt);
if (evt) {
var title = '';
var content = '';
var data = {};
if (evt.title) {
title = evt.title;
}
if (evt.body) {
content = evt.body;
}
if (evt.additionalData) {
if (Ti.Platform.osname === 'android') {
// Android receives it as a JSON string
data = JSON.parse(evt.additionalData);
} else {
data = evt.additionalData;
}
}
}
alert("Notification opened! title: " + title + ', content: ' + content + ', data: ' + evt.additionalData);
});
```

1. Received:

```js
onesignal.addEventListener('notificationReceived', function(evt) {
console.log(' ***** Received! ' + JSON.stringify(evt));
});
```

Cheers!
Binary file not shown.
Binary file modified android/dist/com.williamrijksen.onesignal.jar
Binary file not shown.
92 changes: 57 additions & 35 deletions android/example/app.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,61 @@
// This is a test harness for your module
// You should do something interesting in this harness
// to test out the module and to provide instructions
// to users on how to use it by example.


// open a single window
var win = Ti.UI.createWindow({
backgroundColor:'white'
backgroundColor: 'white'
});

var onesignal = require('com.williamrijksen.onesignal');

var addTag = Ti.UI.createButton({
title: 'Add a tag',
width: Ti.UI.FILL,
height: Ti.UI.FILL,
color: 'black',
opacity: 0.8,
backgroundColor: 'transparent',
borderColor: '#4ee47f',
top: 60
});

addTag.addEventListener('click', function(e) {
onesignal.sendTag({
key: 'tag1',
value: true
});
alert('Tag added');
});
win.add(addTag);


onesignal.addEventListener("notificationOpened", function(evt) {
alert(evt);
if (evt) {
var title = '';
var content = '';
var data = {};

if (evt.title) {
title = evt.title;
}

if (evt.body) {
content = evt.body;
}

if (evt.additionalData) {
if (Ti.Platform.osname === 'android') {
//Android receives it as a JSON string
data = JSON.parse(evt.additionalData);
} else {
data = evt.additionalData;
}
}

alert("Notification opened! title: " + title + ', content: ' + content + ', data: ' + evt.additionalData);
}
});

onesignal.addEventListener("notificationReceived", function(evt) {
alert(' ***** Received! ' + JSON.stringify(evt));
});
var label = Ti.UI.createLabel();
win.add(label);
win.open();

// TODO: write your module tests here
var com_williamrijksen_onesignal = require('com.williamrijksen.onesignal');
Ti.API.info("module is => " + com_williamrijksen_onesignal);

label.text = com_williamrijksen_onesignal.example();

Ti.API.info("module exampleProp is => " + com_williamrijksen_onesignal.exampleProp);
com_williamrijksen_onesignal.exampleProp = "This is a test value";

if (Ti.Platform.name == "android") {
var proxy = com_williamrijksen_onesignal.createExample({
message: "Creating an example Proxy",
backgroundColor: "red",
width: 100,
height: 100,
top: 100,
left: 150
});

proxy.printMessage("Hello world!");
proxy.message = "Hi world!. It's me again.";
proxy.printMessage("Hello world!");
win.add(proxy);
}

win.open();
6 changes: 3 additions & 3 deletions android/manifest
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
# this is your module manifest and used by Titanium
# during compilation, packaging, distribution, etc.
#
version: 1.0.0
version: 1.1.0
apiversion: 2
architectures: armeabi armeabi-v7a x86
description: com.williamrijksen.onesignal
author: Your Name
author: William Rijksen
license: Specify your license
copyright: Copyright (c) 2016 by Your Company
copyright: Copyright (c) 2016 by William Rijksen

# these should not be edited
name: com.williamrijksen.onesignal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@

import android.content.Context;

import com.onesignal.OneSignal;
import com.onesignal.OSNotification;
import com.onesignal.OSNotificationAction;
import com.onesignal.OSNotificationOpenResult;

import java.util.HashMap;

import org.appcelerator.kroll.KrollModule;
import org.appcelerator.kroll.annotations.Kroll;

import org.appcelerator.titanium.TiApplication;
import org.appcelerator.kroll.common.Log;
import org.appcelerator.kroll.common.TiConfig;
import org.appcelerator.titanium.TiApplication;
import org.appcelerator.titanium.util.TiConvert;

import com.onesignal.OneSignal;
import org.json.JSONObject;

@Kroll.module(name="ComWilliamrijksenOnesignal", id="com.williamrijksen.onesignal")
public class ComWilliamrijksenOnesignalModule extends KrollModule
Expand All @@ -22,12 +25,16 @@ public class ComWilliamrijksenOnesignalModule extends KrollModule

public ComWilliamrijksenOnesignalModule()
{
super();
TiApplication appContext = TiApplication.getInstance();
OneSignal
.startInit(appContext)
.init();
super();
TiApplication appContext = TiApplication.getInstance();
OneSignal
.startInit(appContext)
.setNotificationReceivedHandler(new NotificationReceivedHandler())
.setNotificationOpenedHandler(new NotificationOpenedHandler())
.inFocusDisplaying(OneSignal.OSInFocusDisplayOption.None)
.init();
}
//TODO inFocusDisplaying should be configurable from Titanium App module initialization

@Kroll.onAppCreate
public static void onAppCreate(TiApplication app)
Expand All @@ -51,4 +58,44 @@ public void deleteTag(Object tag)
String key = TiConvert.toString(dict, "key");
OneSignal.deleteTag(key);
}

private class NotificationOpenedHandler implements OneSignal.NotificationOpenedHandler {
// This fires when a notification is opened by tapping on it.
@Override
public void notificationOpened(OSNotificationOpenResult result) {
String title = result.notification.payload.title;
String body = result.notification.payload.body;
JSONObject additionalData = result.notification.payload.additionalData;

HashMap<String, Object> kd = new HashMap<String, Object>();
if(title != null){
kd.put("title", title);
}

if(body != null){
kd.put("body", body);
}

if(additionalData != null){
String payload = additionalData.toString();
kd.put("additionalData", payload);
}
fireEvent("notificationOpened", kd);
}
}

private class NotificationReceivedHandler implements OneSignal.NotificationReceivedHandler {
@Override
public void notificationReceived(OSNotification notification) {
JSONObject additionalData = notification.payload.additionalData;
if(additionalData != null){
String payload = additionalData.toString();
HashMap<String, Object> kd = new HashMap<String, Object>();
kd.put("additionalData", payload);
fireEvent("notificationReceived", kd);
}else{
Log.d(LCAT, "No additionalData on notification payload =/");
}
}
}
}
86 changes: 54 additions & 32 deletions example/app.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,61 @@
// This is a test harness for your module
// You should do something interesting in this harness
// to test out the module and to provide instructions
// to users on how to use it by example.


// open a single window
var win = Ti.UI.createWindow({
backgroundColor:'white'
backgroundColor: 'white'
});
var label = Ti.UI.createLabel();
win.add(label);
win.open();

// TODO: write your module tests here
var com_williamrijksen_onesignal = require('com.williamrijksen.onesignal');
Ti.API.info("module is => " + com_williamrijksen_onesignal);

label.text = com_williamrijksen_onesignal.example();

Ti.API.info("module exampleProp is => " + com_williamrijksen_onesignal.exampleProp);
com_williamrijksen_onesignal.exampleProp = "This is a test value";
var onesignal = require('com.williamrijksen.onesignal');

var addTag = Ti.UI.createButton({
title: 'Add a tag',
width: Ti.UI.FILL,
height: Ti.UI.FILL,
color: 'black',
opacity: 0.8,
backgroundColor: 'transparent',
borderColor: '#4ee47f',
top: 60
});

if (Ti.Platform.name == "android") {
var proxy = com_williamrijksen_onesignal.createExample({
message: "Creating an example Proxy",
backgroundColor: "red",
width: 100,
height: 100,
top: 100,
left: 150
});
addTag.addEventListener('click', function(e) {
onesignal.sendTag({
key: 'tag1',
value: true
});
alert('Tag added');
});
win.add(addTag);


onesignal.addEventListener("notificationOpened", function(evt) {
alert(evt);
if (evt) {
var title = '';
var content = '';
var data = {};

if (evt.title) {
title = evt.title;
}

if (evt.body) {
content = evt.body;
}

if (evt.additionalData) {
if (Ti.Platform.osname === 'android') {
//Android receives it as a JSON string
data = JSON.parse(evt.additionalData);
} else {
data = evt.additionalData;
}
}

alert("Notification opened! title: " + title + ', content: ' + content + ', data: ' + evt.additionalData);
}
});

proxy.printMessage("Hello world!");
proxy.message = "Hi world!. It's me again.";
proxy.printMessage("Hello world!");
win.add(proxy);
}
onesignal.addEventListener("notificationReceived", function(evt) {
alert(' ***** Received! ' + JSON.stringify(evt));
});

win.open();
Loading

0 comments on commit 3b2e3ca

Please sign in to comment.