Skip to content

Commit

Permalink
Merge pull request #161 from OneSignal/outcome_events_release
Browse files Browse the repository at this point in the history
Outcome events release
  • Loading branch information
jkasten2 authored Oct 25, 2019
2 parents 2f6e704 + e7a4301 commit c8f7ecc
Show file tree
Hide file tree
Showing 24 changed files with 451 additions and 39 deletions.
12 changes: 6 additions & 6 deletions android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
group 'com.onesignal.flutter'
version '2.1.0'
version '2.2.0'

buildscript {
repositories {
Expand All @@ -8,7 +8,7 @@ buildscript {
}

dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
classpath 'com.android.tools.build:gradle:3.5.0'
}
}

Expand All @@ -34,16 +34,16 @@ android {
}

dependencies {
compile('com.onesignal:OneSignal:3.11.4')
api 'com.onesignal:OneSignal:3.12.2'
}

// Adds required manifestPlaceholders keys to allow mainifest merge gradle step to complete
// The OneSignal app id should be set in your JS code.
// Google project number / FCM Sender ID will be pulled in from the OneSignal dashbaord
class DefaultManifestPlaceHolders {
static final MANIFEST_PLACEHOLDERS_DEFAULTS = [
onesignal_app_id: '',
onesignal_google_project_number: 'REMOTE'
onesignal_app_id: '${onesignal_app_id}',
onesignal_google_project_number: '${onesignal_google_project_number}'
]

static void addManifestToAppProject(Project proj) {
Expand All @@ -62,7 +62,7 @@ class DefaultManifestPlaceHolders {
}

rootProject.childProjects.each { projName, proj ->
if (projName != 'app')
if (projName != 'app' && projName != 'onesignal_flutter')
return

if (proj.hasProperty('android')) {
Expand Down
4 changes: 2 additions & 2 deletions android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Mon Jul 16 17:30:59 PDT 2018
#Thu Oct 17 14:56:12 PDT 2019
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package com.onesignal.flutter;

import com.onesignal.OneSignal;
import com.onesignal.OutcomeEvent;

import java.util.HashMap;
import java.util.concurrent.atomic.AtomicBoolean;

import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugin.common.PluginRegistry.Registrar;

class OSFlutterOutcomeEventsHandler extends FlutterRegistrarResponder implements OneSignal.OutcomeCallback {
private Result result;

// the outcome events callbacks can in some instances be called more than once
// ie. cached vs. server response.
// this property guarantees the callback will never be called more than once.
private AtomicBoolean replySubmitted = new AtomicBoolean(false);

OSFlutterOutcomeEventsHandler(PluginRegistry.Registrar flutterRegistrar, MethodChannel channel, Result result) {
this.flutterRegistrar = flutterRegistrar;
this.channel = channel;
this.result = result;
}

@Override
public void onSuccess(OutcomeEvent outcomeEvent) {
if (this.replySubmitted.getAndSet(true))
return;

if (outcomeEvent == null)
replySuccess(result, new HashMap<>());
else
replySuccess(result, OneSignalSerializer.convertOutcomeEventToMap(outcomeEvent));
}

}

public class OneSignalOutcomeEventsController extends FlutterRegistrarResponder implements MethodCallHandler {
private MethodChannel channel;
private Registrar registrar;

static void registerWith(Registrar registrar) {
OneSignalOutcomeEventsController controller = new OneSignalOutcomeEventsController();
controller.registrar = registrar;
controller.channel = new MethodChannel(registrar.messenger(), "OneSignal#outcomes");
controller.channel.setMethodCallHandler(controller);
controller.flutterRegistrar = registrar;
}

@Override
public void onMethodCall(MethodCall call, Result result) {
if (call.method.contentEquals("OneSignal#sendOutcome"))
this.sendOutcome(call, result);
else if (call.method.contentEquals("OneSignal#sendUniqueOutcome"))
this.sendUniqueOutcome(call, result);
else if (call.method.contentEquals("OneSignal#sendOutcomeWithValue"))
this.sendOutcomeWithValue(call, result);
else
replyNotImplemented(result);
}

private void sendOutcome(MethodCall call, Result result) {
String name = (String) call.arguments;

if (name == null || name.isEmpty()) {
replyError(result, "OneSignal", "sendOutcome() name must not be null or empty", null);
return;
}

OneSignal.sendOutcome(name, new OSFlutterOutcomeEventsHandler(registrar, channel, result));
}

private void sendUniqueOutcome(MethodCall call, Result result) {
String name = (String) call.arguments;

if (name == null || name.isEmpty()) {
replyError(result, "OneSignal", "sendUniqueOutcome() name must not be null or empty", null);
return;
}

OneSignal.sendUniqueOutcome(name, new OSFlutterOutcomeEventsHandler(registrar, channel, result));
}

private void sendOutcomeWithValue(MethodCall call, Result result) {
String name = call.argument("outcome_name");
Double value = call.argument("outcome_value");

if (name == null || name.isEmpty()) {
replyError(result, "OneSignal", "sendOutcomeWithValue() name must not be null or empty", null);
return;
}

if (value == null) {
replyError(result, "OneSignal", "sendOutcomeWithValue() value must not be null", null);
return;
}

OneSignal.sendOutcomeWithValue(name, value.floatValue(), new OSFlutterOutcomeEventsHandler(registrar, channel, result));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public boolean onViewDestroy(FlutterNativeView flutterNativeView) {

OneSignalTagsController.registerWith(registrar);
OneSignalInAppMessagingController.registerWith(registrar);
OneSignalOutcomeEventsController.registerWith(registrar);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,23 @@ static HashMap<String, Object> convertInAppMessageClickedActionToMap(OSInAppMess
return hash;
}

static HashMap<String, Object> convertOutcomeEventToMap(OutcomeEvent outcomeEvent) {
HashMap<String, Object> hash = new HashMap<>();

hash.put("session", outcomeEvent.getSession().toString());

if (outcomeEvent.getNotificationIds() == null)
hash.put("notification_ids", new JSONArray().toString());
else
hash.put("notification_ids", outcomeEvent.getNotificationIds().toString());

hash.put("id", outcomeEvent.getName());
hash.put("timestamp", outcomeEvent.getTimestamp());
hash.put("weight", String.valueOf(outcomeEvent.getWeight()));

return hash;
}

private static HashMap<String, Object> convertAndroidBackgroundImageLayoutToMap(BackgroundImageLayout layout) {
HashMap<String, Object> hash = new HashMap<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ static void registerWith(Registrar registrar) {
controller.registrar = registrar;
controller.channel = new MethodChannel(registrar.messenger(), "OneSignal#tags");
controller.channel.setMethodCallHandler(controller);
controller.flutterRegistrar = registrar;
}

@Override
Expand Down
8 changes: 4 additions & 4 deletions example/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ apply plugin: 'com.android.application'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
compileSdkVersion 27
compileSdkVersion 28

lintOptions {
disable 'InvalidPackage'
Expand All @@ -39,7 +39,7 @@ android {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "com.onesignal.onesignalexample"
minSdkVersion 16
targetSdkVersion 27
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
Expand All @@ -60,6 +60,6 @@ flutter {

dependencies {
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
2 changes: 1 addition & 1 deletion example/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
}

dependencies {
classpath 'com.android.tools.build:gradle:3.1.3'
classpath 'com.android.tools.build:gradle:3.5.0'
}
}

Expand Down
6 changes: 6 additions & 0 deletions example/android/gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
org.gradle.jvmargs=-Xmx1536M

android.enableR8=true
android.enableD8=true

android.useAndroidX=true
android.enableJetifier=true
4 changes: 2 additions & 2 deletions example/android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Fri Jun 23 08:50:38 CEST 2017
#Thu Oct 17 15:00:44 PDT 2019
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip
34 changes: 34 additions & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ class _MyAppState extends State<MyApp> {

// Some examples of how to use In App Messaging public methods with OneSignal SDK
oneSignalInAppMessagingTriggerExamples();

// Some examples of how to use Outcome Events public methods with OneSignal SDK
oneSignalOutcomeEventsExamples();
}

void _handleGetTags() {
Expand Down Expand Up @@ -265,6 +268,37 @@ class _MyAppState extends State<MyApp> {
OneSignal.shared.pauseInAppMessages(false);
}

oneSignalOutcomeEventsExamples() async {
// Await example for sending outcomes
outcomeAwaitExample();

// Send a normal outcome and get a reply with the name of the outcome
OneSignal.shared.sendOutcome("normal_1");
OneSignal.shared.sendOutcome("normal_2").then((outcomeEvent) {
var json = outcomeEvent.jsonRepresentation();
print("Successfully sent outcome event: $json");
});

// Send a unique outcome and get a reply with the name of the outcome
OneSignal.shared.sendUniqueOutcome("unique_1");
OneSignal.shared.sendUniqueOutcome("unique_2").then((outcomeEvent) {
var json = outcomeEvent.jsonRepresentation();
print("Successfully sent unique outcome event: $json");
});

// Send an outcome with a value and get a reply with the name of the outcome
OneSignal.shared.sendOutcomeWithValue("value_1", 3.2);
OneSignal.shared.sendOutcomeWithValue("value_2", 3.9).then((outcomeEvent) {
var json = outcomeEvent.jsonRepresentation();
print("Successfully sent outcome event with value: $json");
});
}

Future<void> outcomeAwaitExample() async {
var outcomeEvent = await OneSignal.shared.sendOutcome("await_normal_1");
print(outcomeEvent.jsonRepresentation());
}

@override
Widget build(BuildContext context) {
return new MaterialApp(
Expand Down
4 changes: 2 additions & 2 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ dependencies:
dev_dependencies:
flutter_test:
sdk: flutter
onesignal_flutter: ^2.0.1

onesignal_flutter: ^2.2.0

# For information on the generic Dart part of this file, see the
# following page: https://www.dartlang.org/tools/pub/pubspec
Expand Down
5 changes: 5 additions & 0 deletions ios/Classes/OneSignalCategories.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@
- (NSDictionary *)toJson;
@end

// TODO: Will reference the OSOutcomeEvent in OneSignal.h
//@interface OSOutcomeEvent (Flutter)
//- (NSDictionary *)toJson;
//@end

@interface NSError (Flutter)
- (FlutterError *)flutterError;
@end
Expand Down
15 changes: 15 additions & 0 deletions ios/Classes/OneSignalCategories.m
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,21 @@ - (NSDictionary *)toJson {
}
@end

// TODO: Will reference the OSOutcomeEvent in OneSignal.h
//@implementation OSOutcomeEvent (Flutter)
//- (NSDictionary *)toJson {
// NSMutableDictionary *json = [NSMutableDictionary new];
//
// json[@"session"] = self.session;
// json[@"notification_ids"] = self.notificationIds;
// json[@"name"] = self.name;
// json[@"timestamp"] = self.timestamp;
// json[@"weight"] = self.weight;
//
// return json;
//}
//@end

@implementation NSError (Flutter)
- (FlutterError *)flutterError {
return [FlutterError errorWithCode:[NSString stringWithFormat:@"%i", (int)self.code] message:self.localizedDescription details:nil];
Expand Down
9 changes: 4 additions & 5 deletions ios/Classes/OneSignalInAppMessagesController.m
Original file line number Diff line number Diff line change
Expand Up @@ -61,26 +61,25 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result
- (void)addTriggers:(FlutterMethodCall *)call withResult:(FlutterResult)result {
NSDictionary *triggers = call.arguments;
[OneSignal addTriggers:triggers];
result(@[]);
result(nil);
}

- (void)removeTriggerForKey:(FlutterMethodCall *)call withResult:(FlutterResult)result {
NSString *key = call.arguments;
[OneSignal removeTriggerForKey:key];
result(@[]);

result(nil);
}

- (void)removeTriggersForKeys:(FlutterMethodCall *)call withResult:(FlutterResult)result {
NSArray *keys = call.arguments;
[OneSignal removeTriggersForKeys:keys];
result(@[]);
result(nil);
}

- (void)pauseInAppMessages:(FlutterMethodCall *)call withResult:(FlutterResult)result {
BOOL pause = [call.arguments boolValue];
[OneSignal pauseInAppMessages:pause];
result(@[]);
result(nil);
}

@end
Loading

0 comments on commit c8f7ecc

Please sign in to comment.