Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First steps into starting onesignal on DidFinishLaunching event #38

Merged
merged 16 commits into from
Aug 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
language: objective-c
osx_image: xcode8.2
osx_image: xcode8.3

env:
global:
Expand All @@ -18,7 +18,7 @@ before_install:
- brew install nvm
- nvm install 4
- npm config delete prefix
- nvm use --delete-prefix v4.4.7 4
- nvm use --delete-prefix v4.8.4 4
- brew cask uninstall oclint
- brew tap oclint/formulae
- brew install oclint
Expand All @@ -27,7 +27,7 @@ before_install:
install:
- cd $MODULE_ROOT
- curl -o install.sh https://rawcdn.githack.com/sgtcoolguy/ci/v8/travis/install.sh
- source install.sh -s "--branch 6_0_X"
- source install.sh -s "--branch 6_1_X"

before_script:
- curl -o script.sh https://rawcdn.githack.com/sgtcoolguy/ci/v8/travis/script.sh
Expand Down
23 changes: 17 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ Before setting up the Titanium SDK, you must generate the appropriate credential

```xml
<modules>
<module platform="iphone" version="1.6.0">com.williamrijksen.onesignal</module>
<module platform="android" version="1.6.0">com.williamrijksen.onesignal</module>
<module platform="iphone" version="1.7.0">com.williamrijksen.onesignal</module>
<module platform="android" version="1.7.0">com.williamrijksen.onesignal</module>
</modules>
```
1. Configure your app into the App Settings panel for the right Platform (Android and/or iOS).
Expand Down Expand Up @@ -46,9 +46,15 @@ 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. On iOS you'll need to request permission to use notifications:
```js
oneSignal.promptForPushNotificationsWithUserResponse(function(obj) {
alert(JSON.stringify(obj));
});
```
1. To add the possibility to target people for notifications, send a tag:

```js
Expand Down Expand Up @@ -96,8 +102,10 @@ 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:
1. Opened listener:
The returned content is matching the available payload on OneSignal:
- [https://documentation.onesignal.com/docs/ios-native-sdk#section--osnotificationpayload-](iOS)
- [https://documentation.onesignal.com/docs/android-native-sdk#section--osnotificationpayload-](Android)

```js
onesignal.addEventListener('notificationOpened', function (evt) {
Expand Down Expand Up @@ -128,7 +136,10 @@ Before setting up the Titanium SDK, you must generate the appropriate credential
});
```

1. Received:
1. Received listener:
The returned content is matching the available payload on OneSignal:
- [https://documentation.onesignal.com/docs/ios-native-sdk#section--osnotificationpayload-](iOS)
- [https://documentation.onesignal.com/docs/android-native-sdk#section--osnotificationpayload-](Android)

```js
onesignal.addEventListener('notificationReceived', function(evt) {
Expand Down
9 changes: 9 additions & 0 deletions android/build.properties.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
titanium.sdk=<YOUR_HOMEPATH>/Library/Application\ Support/Titanium/
titanium.os=osx
titanium.version=6.1.2.GA
android.sdk=/usr/local/share/android-sdk
android.ndk=/usr/local/share/android-sdk/ndk-bundle

titanium.platform=${titanium.sdk}/mobilesdk/${titanium.os}/${titanium.version}/android
android.platform=${android.sdk}/platforms/android-23
google.apis=${android.sdk}/add-ons/addon-google_apis-google-23
Binary file modified android/lib/OneSignalSDK.jar
Binary file not shown.
4 changes: 2 additions & 2 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.6.0
version: 1.7.0
apiversion: 3
architectures: armeabi-v7a x86
description: com.williamrijksen.onesignal
author: William Rijksen
license: Specify your license
copyright: Copyright (c) 2016 by William Rijksen
copyright: Copyright (c) 2017 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 @@ -6,10 +6,12 @@
import com.onesignal.OSNotification;
import com.onesignal.OSNotificationAction;
import com.onesignal.OSNotificationOpenResult;
import com.onesignal.OSNotificationPayload;

import java.util.HashMap;

import org.appcelerator.kroll.KrollModule;
import org.appcelerator.kroll.KrollProxy;
import org.appcelerator.kroll.annotations.Kroll;
import org.appcelerator.kroll.KrollFunction;
import org.appcelerator.kroll.common.Log;
Expand All @@ -23,48 +25,49 @@ public class ComWilliamrijksenOnesignalModule extends KrollModule
{
private static final String LCAT = "ComWilliamrijksenOnesignalModule";
private static final boolean DBG = TiConfig.LOGD;
private boolean oneSignalInitDone;
private static ComWilliamrijksenOnesignalModule module;
private static OSNotificationOpenResult openNotification;

public ComWilliamrijksenOnesignalModule()
{
super();
initOneSignal(TiApplication.getInstance().getCurrentActivity());
module = this;
}

private void initOneSignal(Activity activity)
public static ComWilliamrijksenOnesignalModule getModuleInstance()
{
if (activity == null || oneSignalInitDone) {
return;
}

oneSignalInitDone = true;

OneSignal
.startInit(activity)
.setNotificationReceivedHandler(new NotificationReceivedHandler())
.setNotificationOpenedHandler(new NotificationOpenedHandler())
.inFocusDisplaying(OneSignal.OSInFocusDisplayOption.None)
.init();
return module;
}
//TODO inFocusDisplaying should be configurable from Titanium App module initialization

//variable to store the received call back function for the getTags method call
private KrollFunction getTagsCallback = null;

private KrollFunction idsAvailableCallback = null;

@Kroll.onAppCreate
public static void onAppCreate(TiApplication app)
{
Log.d(LCAT, "inside onAppCreate");
Log.d(LCAT, "com.williamrijksen.onesignal inside onAppCreate");

OneSignal
.startInit(TiApplication.getInstance())
.setNotificationReceivedHandler(new NotificationReceivedHandler())
.setNotificationOpenedHandler(new NotificationOpenedHandler())
.inFocusDisplaying(OneSignal.OSInFocusDisplayOption.None)
.init();
}

@Override
public void onResume(Activity activity)
public void listenerAdded(String type, int count, KrollProxy proxy)
{
super.onResume(activity);
Log.d(LCAT, "Trying to initialize OneSignal if necessary");
initOneSignal(activity);
Log.d(LCAT,"com.williamrijksen.onesignal added listener " + type);
if (type.equals("notificationOpened") && count == 1 && openNotification instanceof OSNotificationOpenResult) {
Log.d(LCAT,"com.williamrijksen.onesignal fire delayed event");
try {
OSNotificationPayload payload = openNotification.notification.payload;
proxy.fireEvent("notificationOpened", payload.toJSONObject());
} catch (Throwable t) {
Log.d(LCAT, "com.williamrijksen.onesignal OSNotificationOpenResult could not be converted to JSON");
}
openNotification = null;
}
}

@Kroll.method
Expand Down Expand Up @@ -98,9 +101,11 @@ public void idsAvailable(KrollFunction handler)
OneSignal.idsAvailable(new IdsAvailableHandler());
}

private class GetTagsHandler implements OneSignal.GetTagsHandler {
private class GetTagsHandler implements OneSignal.GetTagsHandler
{
@Override
public void tagsAvailable(JSONObject tags) {
public void tagsAvailable(JSONObject tags)
{
HashMap<String, Object> dict = new HashMap<String, Object>();
try {
dict.put("success", true);
Expand All @@ -117,7 +122,8 @@ public void tagsAvailable(JSONObject tags) {
}
}

private class IdsAvailableHandler implements OneSignal.IdsAvailableHandler {
private class IdsAvailableHandler implements OneSignal.IdsAvailableHandler
{
@Override
public void idsAvailable(String userId, String registrationId)
{
Expand All @@ -133,49 +139,46 @@ public void idsAvailable(String userId, String registrationId)
}
}

private class NotificationOpenedHandler implements OneSignal.NotificationOpenedHandler {
private static class NotificationOpenedHandler implements OneSignal.NotificationOpenedHandler
{
// This fires when a notification is opened by tapping on it.
@Override
public void notificationOpened(OSNotificationOpenResult result) {
try {
JSONObject json = result.toJSONObject();
HashMap<String, Object> kd = new HashMap<String, Object>();

if (json.has("notification") && json.getJSONObject("notification").has("payload")) {
JSONObject payload = json.getJSONObject("notification").getJSONObject("payload");

if (payload.has("title")) {
kd.put("title", payload.getString("title"));
}

if (payload.has("body")) {
kd.put("body", payload.getString("body"));
}
public void notificationOpened(OSNotificationOpenResult result)
{
Log.d(LCAT, "com.williamrijksen.onesignal Notification opened handler");
if (getModuleInstance() != null) {
try {
OSNotificationPayload payload = result.notification.payload;

if (payload.has("additionlData")) {
String additional = payload.getJSONObject("additionalData").toString();
kd.put("additionalData", additional);
if (getModuleInstance().hasListeners("notificationOpened") && payload != null) {
getModuleInstance().fireEvent("notificationOpened", payload.toJSONObject());
}
} catch (Throwable t) {
Log.d(LCAT, "com.williamrijksen.onesignal OSNotificationOpenResult could not be converted to JSON");
}
fireEvent("notificationOpened", kd);
}
catch (Throwable t) {
Log.d(LCAT, "Notification result could not be converted to JSON");
} else {
// save the notification for later processing
openNotification = result;
}
}
}

private class NotificationReceivedHandler implements OneSignal.NotificationReceivedHandler {
private static 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 =/");
public void notificationReceived(OSNotification notification)
{
Log.d(LCAT, "com.williamrijksen.onesignal Notification received handler");
if (getModuleInstance() != null) {
try {
OSNotificationPayload payload = notification.payload;

if (getModuleInstance().hasListeners("notificationReceived") && payload != null) {
getModuleInstance().fireEvent("notificationReceived", payload.toJSONObject());
}
} catch (Throwable t) {
Log.d(LCAT, "com.williamrijksen.onesignal OSNotification could not be converted to JSON");
}
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion ios/Classes/ComWilliamrijksenOnesignalModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@

#import "TiModule.h"
#import <OneSignal/OneSignal.h>
#import "OneSignalManager.h"

@interface ComWilliamrijksenOnesignalModule : TiModule {}
@interface ComWilliamrijksenOnesignalModule : TiModule<OneSignalDelegate> {}

typedef void(^TagsResultHandler)(NSDictionary*, NSError*);

- (void)promptForPushNotificationsWithUserResponse:(id)args;
- (void)sendTag:(id)args;
- (void)deleteTag:(id)args;
- (void)getTags:(id)value;
Expand Down
Loading