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 6 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
4 changes: 2 additions & 2 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.6.1">com.williamrijksen.onesignal</module>
<module platform="android" version="1.6.1">com.williamrijksen.onesignal</module>
</modules>
```
1. Configure your app into the App Settings panel for the right Platform (Android and/or iOS).
Expand Down
2 changes: 1 addition & 1 deletion android/manifest
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# this is your module manifest and used by Titanium
# during compilation, packaging, distribution, etc.
#
version: 1.6.0
version: 1.6.1
apiversion: 3
architectures: armeabi-v7a x86
description: com.williamrijksen.onesignal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,48 +23,42 @@ public class ComWilliamrijksenOnesignalModule extends KrollModule
{
private static final String LCAT = "ComWilliamrijksenOnesignalModule";
private static final boolean DBG = TiConfig.LOGD;
private boolean oneSignalInitDone;
private static final String ModuleName = "ComWilliamrijksenOnesignal";

public ComWilliamrijksenOnesignalModule()
{
super();
initOneSignal(TiApplication.getInstance().getCurrentActivity());
super(ModuleName);
}

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

oneSignalInitDone = true;
private KrollFunction getTagsCallback = null;
private KrollFunction idsAvailableCallback = null;

@Kroll.onAppCreate
public static void onAppCreate(TiApplication app)
{
Log.d(LCAT, "inside onAppCreate");
OneSignal
.startInit(activity)
.startInit(TiApplication.getInstance())
.setNotificationReceivedHandler(new NotificationReceivedHandler())
.setNotificationOpenedHandler(new NotificationOpenedHandler())
.inFocusDisplaying(OneSignal.OSInFocusDisplayOption.None)
.init();
}
//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");
private static ComWilliamrijksenOnesignalModule getModule() {
TiApplication appContext = TiApplication.getInstance();
ComWilliamrijksenOnesignalModule onesignalModule = (ComWilliamrijksenOnesignalModule)appContext.getModuleByName(ModuleName);

if (onesignalModule == null) {
Log.w(LCAT,"OneSignal module not currently loaded");
}
return onesignalModule;
}

@Override
public void onResume(Activity activity)
{
super.onResume(activity);
Log.d(LCAT, "Trying to initialize OneSignal if necessary");
initOneSignal(activity);
}

@Kroll.method
Expand Down Expand Up @@ -133,48 +127,52 @@ 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"));
}

if (payload.has("additionlData")) {
String additional = payload.getJSONObject("additionalData").toString();
kd.put("additionalData", additional);
ComWilliamrijksenOnesignalModule onesignalModule = getModule();
if (onesignalModule != null) {
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"));
}

if (payload.has("additionlData")) {
String additional = payload.getJSONObject("additionalData").toString();
kd.put("additionalData", additional);
}
}
onesignalModule.fireEvent("notificationOpened", kd);
}
catch (Throwable t) {
Log.d(LCAT, "Notification result could not be converted to JSON");
}
fireEvent("notificationOpened", kd);
}
catch (Throwable t) {
Log.d(LCAT, "Notification result could not be converted to JSON");
}
}
}

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){
ComWilliamrijksenOnesignalModule onesignalModule = getModule();
if (onesignalModule != null && additionalData != null){
String payload = additionalData.toString();
HashMap<String, Object> kd = new HashMap<String, Object>();
kd.put("additionalData", payload);
fireEvent("notificationReceived", kd);
}else{
onesignalModule.fireEvent("notificationReceived", kd);
} else {
Log.d(LCAT, "No additionalData on notification payload =/");
}
}
Expand Down
5 changes: 4 additions & 1 deletion ios/Classes/ComWilliamrijksenOnesignalModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@

#import "TiModule.h"
#import <OneSignal/OneSignal.h>
#import "OneSignalManager.h"
#import "OneSignalDelegate.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
109 changes: 43 additions & 66 deletions ios/Classes/ComWilliamrijksenOnesignalModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,102 +9,79 @@
#import "TiBase.h"
#import "TiHost.h"
#import "TiUtils.h"
#import "TiApp.h"

@implementation ComWilliamrijksenOnesignalModule

NSString * const TiNotificationReceived = @"notificationReceived";
NSString * const TiNotificationOpened = @"notificationOpened";

static OneSignalManager* _oneSignalManager = nil;

#pragma mark Internal

// this is generated for your module, please do not change it
-(id)moduleGUID
- (id)moduleGUID
{
return @"67065763-fd5e-4069-a877-6c7fd328f877";
}

// this is generated for your module, please do not change it
-(NSString*)moduleId
- (NSString*)moduleId
{
return @"com.williamrijksen.onesignal";
}

#pragma mark Lifecycle

- (void) receivedHandler:(OSNotification *)notification {
OSNotificationPayload* payload = notification.payload;

NSString* title = @"";
NSString* body = @"";
NSDictionary* additionalData = [[NSDictionary alloc] init];

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

if (payload.body) {
body = [payload.body copy];
}

if (payload.additionalData) {
additionalData = payload.additionalData;
}

NSDictionary* notificationData = @{
@"title": title,
@"body": body,
@"additionalData": additionalData
};
[self fireEvent:@"notificationReceived" withObject:notificationData];
};
- (void)startup
{
[super startup];

- (void) actionHandler:(OSNotificationOpenedResult *)result {
OSNotificationPayload* payload = result.notification.payload;

NSString* title = @"";
NSString* body = @"";
NSDictionary* additionalData = [[NSDictionary alloc] init];
[_oneSignalManager setDelegate:self];
NSLog(@"[INFO] started %@", self);
}

if(payload.title) {
title = payload.title;
+(void)onAppCreate:(NSNotification *)notification
{
if (!_oneSignalManager) {
_oneSignalManager = [[OneSignalManager alloc] initWithNSNotification:notification];
}
}

if (payload.body) {
body = [payload.body copy];
}
+ (void)load
{
NSLog(@"[DEBUG] com.williamrijksen.onesignal load");
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onAppCreate:)
name:@"UIApplicationDidFinishLaunchingNotification" object:nil];
}

if (payload.additionalData) {
additionalData = payload.additionalData;
}

NSDictionary* notificationData = @{
@"title": title,
@"body": body,
@"additionalData": additionalData};
[self fireEvent:@"notificationOpened" withObject:notificationData];
-(void)notificationOpened:(NSDictionary*)info
{
if ([self _hasListeners:TiNotificationOpened]) {
NSLog(@"[DEBUG] com.williamrijksen.onesignal FIRE TiNotificationOpened: %@", info);
[self fireEvent:TiNotificationOpened withObject:info];
}
}

- (void)startup
-(void)notificationReceived:(NSNotification*)info
{
[super startup];
[[TiApp app] setRemoteNotificationDelegate:self];

NSString *OneSignalAppID = [[TiApp tiAppProperties] objectForKey:@"OneSignal_AppID"];
[OneSignal initWithLaunchOptions:[[TiApp app] launchOptions]
appId:OneSignalAppID
handleNotificationReceived:^(OSNotification *notification) {
[self receivedHandler:notification];
}
handleNotificationAction:^(OSNotificationOpenedResult *result) {
[self actionHandler:result];
}
settings:@{
kOSSettingsKeyInFocusDisplayOption: @(OSNotificationDisplayTypeNone),
kOSSettingsKeyAutoPrompt: @YES}
];
//TODO these settings should be configurable from the Titanium App on module initialization
if ([self _hasListeners:TiNotificationReceived]) {
NSLog(@"[DEBUG] com.williamrijksen.onesignal FIRE TiNotificationReceived: %@", info);
[self fireEvent:TiNotificationReceived withObject:info];
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make it ARC ;-)


#pragma mark Public API's

- (void)promptForPushNotificationsWithUserResponse:(id)args
{
ENSURE_UI_THREAD_1_ARG(args);
[OneSignal promptForPushNotificationsWithUserResponse:^(BOOL accepted) {
NSLog(@"[DEBUG] com.williamrijksen.onesignal User accepted notifications: %d", accepted);
}];
}

- (void)sendTag:(id)arguments
{
id args = arguments;
Expand Down
16 changes: 16 additions & 0 deletions ios/Classes/OneSignalDelegate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// OneSignalDelegate.h
// com.williamrijksen.onesignal
//
// Created by William Rijksen on 01-08-17.
//
//

#import <Foundation/Foundation.h>

@protocol OneSignalDelegate <UIApplicationDelegate>

-(void)notificationReceived:(NSDictionary*)info;
-(void)notificationOpened:(NSDictionary*)info;

@end
18 changes: 18 additions & 0 deletions ios/Classes/OneSignalManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// OneSignalManager.h
// com.williamrijksen.onesignal
//
// Created by William Rijksen on 01-08-17.
//
//

#import <Foundation/Foundation.h>
#import "OneSignalDelegate.h"

@interface OneSignalManager : NSObject {}

@property(assign, nonatomic) id<OneSignalDelegate> delegate;

- (OneSignalManager*)initWithNSNotification:(NSNotification*)notification;

@end
Loading