Skip to content

Commit

Permalink
iOS-only: Adds support for the idsAvailable and postNotification meth…
Browse files Browse the repository at this point in the history
…ods. Also includes a small fixes for getTags error logic
  • Loading branch information
Carlos Henrique Zinato committed Jan 26, 2017
1 parent 6f04ed8 commit ca1e4d9
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 27 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,23 @@ Before setting up the Titanium SDK, you must generate the appropriate credential
Ti.API.info(e.results);
});
```
1. IdsAvailable (iOS-only for now):

```js
onesignal.idsAvailable(function(e) {
//pushToken will be nil if the user did not accept push notifications
alert(e);
});
```
1. postNotification (iOS-only for now):

```js
//You can use idsAvailable for retrieving a playerId
onesignal.postNotification({
message:'Titanium test message',
playerIds:["00000000-0000-0000-0000-000000000000"]
});
```
1. Set log level (iOS-only for now):

```js
Expand Down
101 changes: 82 additions & 19 deletions example/app.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,92 @@
// open a single window
var win = Ti.UI.createWindow({
backgroundColor: 'white'
backgroundColor:'white'
});

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

var scroll = Ti.UI.createScrollView({
layout: 'vertical',
top: 50
});
win.add(scroll);

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);
title:'Add a tag',
width:Ti.UI.FILL,
height:40,
color:'black',
opacity:0.8,
backgroundColor:'transparent',
top:10
});

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

var getTags = Ti.UI.createButton({
title:'Get all tags',
width:Ti.UI.FILL,
height:40,
color:'black',
opacity:0.8,
backgroundColor:'transparent',
top:10
});

getTags.addEventListener('click',function(e){
onesignal.getTags(function(e) {
if (!e.success) {
alert("Error: " + e.error);
return
}
alert(e.results);
setTimeout(function () {
var tags = JSON.parse(e.results);
alert(tags.length);
}, 2500);
});
});
scroll.add(getTags);

var getIds = Ti.UI.createButton({
title:'Get player ID',
width:Ti.UI.FILL,
height:40,
color:'black',
opacity:0.8,
backgroundColor:'transparent',
top:10
});

getIds.addEventListener('click',function(e){
onesignal.idsAvailable(function(e) {
//pushToken will be nil if the user did not accept push notifications
alert(e);
});
});
scroll.add(getIds);

var postNotificationButton = Ti.UI.createButton({
title:'Send a notification',
width:Ti.UI.FILL,
height:40,
color:'black',
opacity:0.8,
backgroundColor:'transparent',
top:10
});

postNotificationButton.addEventListener('click',function(e){
onesignal.postNotification({
message:'Titanium test message',
playerIds:["00000000-0000-0000-0000-000000000000"]
});
});
scroll.add(postNotificationButton);


onesignal.addEventListener("notificationOpened", function(evt) {
Expand Down
2 changes: 2 additions & 0 deletions ios/Classes/ComWilliamrijksenOnesignalModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,7 @@ typedef void(^TagsResultHandler)(NSDictionary*, NSError*);
- (void)deleteTag:(id)args;
- (void)getTags:(id)value;
- (void)setLogLevel:(id)args;
- (void)idsAvailable:(id)args;
- (void)postNotification:(id)arguments;

@end
44 changes: 39 additions & 5 deletions ios/Classes/ComWilliamrijksenOnesignalModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,12 @@ - (void)getTags:(id)args
@"success": NUMBOOL(error == nil),
}];

if (error == nil) {
propertiesDict[@"error"] = [error localizedDescription];
propertiesDict[@"code"] = NUMINTEGER([error code]);
} else {
// Are all keys and values Kroll-save? If not, we need a validation utility
if (error == nil) {
// Are all keys and values Kroll-save? If not, we need a validation utility
propertiesDict[@"results"] = results ?: @[];
} else {
propertiesDict[@"error"] = [error localizedDescription];
propertiesDict[@"code"] = NUMINTEGER([error code]);
}

NSArray *invocationArray = [[NSArray alloc] initWithObjects:&propertiesDict count:1];
Expand All @@ -158,6 +158,40 @@ - (void)getTags:(id)args
}];
}

- (void)idsAvailable:(id)args
{
id value = args;
ENSURE_UI_THREAD(idsAvailable, value);
ENSURE_SINGLE_ARG(value, KrollCallback);

[OneSignal IdsAvailable:^(NSString* userId, NSString* pushToken) {
NSMutableDictionary *idsDict = [NSMutableDictionary dictionaryWithDictionary:@{
@"userId" : userId ?: @[],
@"pushToken" :pushToken ?: @[]
}];
NSArray *invocationArray = [[NSArray alloc] initWithObjects:&idsDict count:1];
[value call:invocationArray thisObject:self];
[invocationArray release];
}];
}

- (void)postNotification:(id)arguments
{
id args = arguments;
ENSURE_UI_THREAD_1_ARG(args);
ENSURE_SINGLE_ARG(args, NSDictionary);

NSString *message = [TiUtils stringValue:[args objectForKey:@"message"]];
NSArray *playerIds = [args valueForKey:@"playerIds"];

if(([message length] != 0) && ([playerIds count] != 0)){
[OneSignal postNotification:@{
@"contents" : @{@"en": message},
@"include_player_ids": playerIds
}];
}
}

- (void)setLogLevel:(id)arguments
{
id args = arguments;
Expand Down
2 changes: 1 addition & 1 deletion ios/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.3.0
version: 1.3.1
apiversion: 2
architectures: armv7 arm64 i386 x86_64
description: com.williamrijksen.onesignal
Expand Down
4 changes: 2 additions & 2 deletions ios/module.xcconfig
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
//
//
// IMPORTANT NOTE: always use $(inherited) in your overrides
//
FRAMEWORK_SEARCH_PATHS=$(SRCROOT)/../../modules/iphone/com.williamrijksen.onesignal/1.3.0/platform "~/Library/Application\ Support/Titanium/modules/iphone/com.williamrijksen.onesignal/1.3.0/platform"
//
FRAMEWORK_SEARCH_PATHS=$(SRCROOT)/../../modules/iphone/com.williamrijksen.onesignal/1.3.1/platform "~/Library/Application\ Support/Titanium/modules/iphone/com.williamrijksen.onesignal/1.3.1/platform"
OTHER_LDFLAGS=$(inherited) -framework OneSignal
LD_RUNPATH_SEARCH_PATHS= $(inherited) "@executable_path/Frameworks" $(FRAMEWORK_SEARCH_PATHS)

0 comments on commit ca1e4d9

Please sign in to comment.