Skip to content

Commit

Permalink
Merge pull request #1524 from OneSignal/user_model_beta2/rename_events
Browse files Browse the repository at this point in the history
[User Model] Rename Events & API Changes
  • Loading branch information
jennantilla authored Jul 24, 2023
2 parents 7dd6b19 + e20d6db commit be6676e
Show file tree
Hide file tree
Showing 20 changed files with 692 additions and 596 deletions.
147 changes: 78 additions & 69 deletions MIGRATION_GUIDE.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ dependencies {

// api is used instead of implementation so the parent :app project can access any of the OneSignal Java
// classes if needed. Such as com.onesignal.NotificationExtenderService
api 'com.onesignal:OneSignal:5.0.0-beta2'
api 'com.onesignal:OneSignal:5.0.0-beta4'

testImplementation 'junit:junit:4.12'
}
307 changes: 160 additions & 147 deletions android/src/main/java/com/geektime/rnonesignalandroid/RNOneSignal.java

Large diffs are not rendered by default.

49 changes: 13 additions & 36 deletions android/src/main/java/com/geektime/rnonesignalandroid/RNUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@
import com.facebook.react.bridge.WritableNativeArray;
import com.facebook.react.bridge.WritableNativeMap;

// import com.onesignal.OSInAppMessage;
import com.onesignal.inAppMessages.IInAppMessage;
import com.onesignal.inAppMessages.IInAppMessageClickResult;
import com.onesignal.inAppMessages.IInAppMessageWillDisplayEvent;
import com.onesignal.inAppMessages.IInAppMessageDidDisplayEvent;
import com.onesignal.inAppMessages.IInAppMessageWillDismissEvent;
import com.onesignal.inAppMessages.IInAppMessageDidDismissEvent;
import com.onesignal.notifications.INotification;
import com.onesignal.notifications.INotificationAction;
import com.onesignal.notifications.INotificationClickResult;
import com.onesignal.notifications.INotificationReceivedEvent;
import com.onesignal.user.subscriptions.IPushSubscription;
import com.onesignal.user.subscriptions.ISubscription;
import com.onesignal.user.subscriptions.PushSubscriptionState;

import org.json.JSONArray;
import org.json.JSONException;
Expand Down Expand Up @@ -109,15 +111,6 @@ public static HashMap<String, Object> convertNotificationToMap(INotification not
return hash;
}

public static HashMap<String, Object> convertNotificationClickResultToMap(INotificationClickResult openResult) throws JSONException {
HashMap<String, Object> hash = new HashMap<>();

hash.put("notification", convertNotificationToMap(openResult.getNotification()));
hash.put("action", convertNotificationActionToMap(openResult.getAction()));

return hash;
}

public static HashMap<String, Object> convertInAppMessageToMap(IInAppMessage message) {
HashMap<String, Object> hash = new HashMap<>();

Expand All @@ -126,18 +119,18 @@ public static HashMap<String, Object> convertInAppMessageToMap(IInAppMessage mes
return hash;
}

public static HashMap<String, Object> convertInAppMessageClickedActionToMap(IInAppMessageClickResult result) {
public static HashMap<String, Object> convertInAppMessageClickResultToMap(IInAppMessageClickResult result) {
HashMap<String, Object> hash = new HashMap<>();

hash.put("clickName", result.getAction().getClickName());
hash.put("clickUrl", result.getAction().getClickUrl());
hash.put("firstClick", result.getAction().isFirstClick());
hash.put("closesMessage", result.getAction().getClosesMessage());
hash.put("actionId", result.getActionId());
hash.put("urlTarget", result.getUrlTarget());
hash.put("url", result.getUrl());
hash.put("closingMessage", result.getClosingMessage());

return hash;
}

public static HashMap<String, Object> convertOnSubscriptionChangedToMap(IPushSubscription state) {
public static HashMap<String, Object> convertOnSubscriptionChangedToMap(PushSubscriptionState state) {
HashMap<String, Object> hash = new HashMap<>();

hash.put("token", state.getToken());
Expand Down Expand Up @@ -175,7 +168,7 @@ public static HashMap<String, Object> convertJSONObjectToHashMap(JSONObject obje
return hash;
}

public static Collection<String> convertReableArrayIntoStringCollection(ReadableArray readableArray) {
public static Collection<String> convertReadableArrayIntoStringCollection(ReadableArray readableArray) {
ArrayList<String> strings = new ArrayList<>();
for (Object object : readableArray.toArrayList()) {
if (object instanceof String)
Expand All @@ -184,7 +177,7 @@ public static Collection<String> convertReableArrayIntoStringCollection(Readable
return strings;
}

public static HashMap<String, String> convertReableMapIntoStringMap(ReadableMap readableMap) {
public static HashMap<String, String> convertReadableMapIntoStringMap(ReadableMap readableMap) {
HashMap<String, String> stringMap = new HashMap<>();
ReadableMapKeySetIterator iter = readableMap.keySetIterator();

Expand All @@ -206,22 +199,6 @@ public static HashMap<String, Object> convertPermissionToMap(boolean granted) {
return hash;
}

private static HashMap<String, Object> convertNotificationActionToMap(INotificationAction action) {
HashMap<String, Object> hash = new HashMap<>();

hash.put("id", action.getActionId());

switch (action.getType()) {
case Opened:
hash.put("type", 0);
break;
case ActionTaken:
hash.put("type", 1);
}

return hash;
}

private static List<Object> convertJSONArrayToList(JSONArray array) throws JSONException {
List<Object> list = new ArrayList<>();

Expand Down
42 changes: 12 additions & 30 deletions examples/RNOneSignalTS/src/OSButtons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -420,61 +420,43 @@ class OSButtons extends React.Component<Props> {
privacyConsentFields() {
const {loggingFunction} = this.props;

const getPrivacyConsentButton = renderButtonView(
'Get Privacy Consent',
async () => {
const granted = await OneSignal.getPrivacyConsent();
loggingFunction('Privacy consent granted: ', granted);
}
);

const setPrivacyConsentTrueButton = renderButtonView(
const setPrivacyConsentGivenTrueButton = renderButtonView(
'Set Privacy Consent to true',
async () => {
await OneSignal.setPrivacyConsent(true);
await OneSignal.setConsentGiven(true);
loggingFunction('Privacy Consent set to true');
}
);

const setPrivacyConsentFalseButton = renderButtonView(
const setPrivacyConsentGivenFalseButton = renderButtonView(
'Set Privacy Consent to false',
async () => {
await OneSignal.setPrivacyConsent(false);
await OneSignal.setConsentGiven(false);
loggingFunction('Privacy Consent set to false');
}
);

const getRequiresPrivacyConsentButton = renderButtonView(
'Get Requires Privacy Consent',
async () => {
const granted = await OneSignal.getRequiresPrivacyConsent();
loggingFunction('Requires Privacy Consent: ', granted);
}
);

const setRequiresPrivacyConsentTrueButton = renderButtonView(
const setPrivacyConsentRequiredTrueButton = renderButtonView(
'Set Requiers Privacy Consent to true',
async () => {
await OneSignal.setRequiresPrivacyConsent(true);
await OneSignal.setConsentRequired(true);
loggingFunction('Requires Privacy Consent set to true');
}
);

const setRequiresPrivacyConsentFalseButton = renderButtonView(
const setPrivacyConsentRequiredFalseButton = renderButtonView(
'Set Requiers Privacy Consent to false',
async () => {
await OneSignal.setRequiresPrivacyConsent(false);
await OneSignal.setConsentRequired(false);
loggingFunction('Requires Privacy Consent set to false');
}
);

return [
getPrivacyConsentButton,
setPrivacyConsentTrueButton,
setPrivacyConsentFalseButton,
getRequiresPrivacyConsentButton,
setRequiresPrivacyConsentTrueButton,
setRequiresPrivacyConsentFalseButton,
setPrivacyConsentGivenTrueButton,
setPrivacyConsentGivenFalseButton,
setPrivacyConsentRequiredTrueButton,
setPrivacyConsentRequiredFalseButton,
];
}

Expand Down
53 changes: 27 additions & 26 deletions examples/RNOneSignalTS/src/OSDemo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,60 +33,61 @@ class OSDemo extends React.Component<Props, State> {
OneSignal.initialize(APP_ID);
OneSignal.Debug.setLogLevel(6);

OneSignal.Notifications.setNotificationWillShowInForegroundHandler(
(notifReceivedEvent) => {
this.OSLog('OneSignal: notification will show in foreground:', notifReceivedEvent);
const notification = notifReceivedEvent.getNotification();
OneSignal.Notifications.addEventListener('foregroundWillDisplay',
(event) => {
this.OSLog('OneSignal: notification will show in foreground:', event);
let notif = event.getNotification();

const cancelButton = {
text: 'Cancel',
onPress: () => {
notifReceivedEvent.complete();
event.preventDefault();
},
style: 'cancel',
};

const completeButton = {
text: 'Complete',
text: 'Display',
onPress: () => {
notifReceivedEvent.complete(notification);
event.getNotification().display();
},
};

Alert.alert('Complete notification?', notification.title, [cancelButton, completeButton], {
Alert.alert('Display notification?', notif.title, [cancelButton, completeButton], {
cancelable: true,
});
},
);

OneSignal.Notifications.setNotificationClickHandler((notification) => {
this.OSLog('OneSignal: notification opened:', notification);
OneSignal.Notifications.addEventListener('click', (event) => {
this.OSLog('OneSignal: notification clicked:', event);
});

OneSignal.InAppMessages.setClickHandler((event) => {
OneSignal.InAppMessages.addEventListener('click', (event) =>{
this.OSLog('OneSignal IAM clicked:', event);
});

OneSignal.InAppMessages.setLifecycleHandler({
onWillDisplayInAppMessage: (message) => {
this.OSLog('OneSignal: will display IAM: ', message.messageId);
},
onDidDisplayInAppMessage: (message) => {
this.OSLog('OneSignal: did display IAM: ', message.messageId);
},
onWillDismissInAppMessage: (message) => {
this.OSLog('OneSignal: will dismiss IAM: ', message.messageId);
},
onDidDismissInAppMessage: (message) => {
this.OSLog('OneSignal: did dismiss IAM: ', message.messageId);
},
OneSignal.InAppMessages.addEventListener('willDisplay', (event) =>{
this.OSLog('OneSignal: will display IAM: ', event);
});

OneSignal.InAppMessages.addEventListener('didDisplay', (event) =>{
this.OSLog('OneSignal: did display IAM: ', event);
});

OneSignal.InAppMessages.addEventListener('willDismiss', (event) =>{
this.OSLog('OneSignal: will dismiss IAM: ', event);
});

OneSignal.InAppMessages.addEventListener('didDismiss', (event) =>{
this.OSLog('OneSignal: did dismiss IAM: ', event);
});

OneSignal.User.PushSubscription.addChangeHandler((subscription) => {
OneSignal.User.PushSubscription.addObserver((subscription) => {
this.OSLog('OneSignal: subscription changed:', subscription);
});

OneSignal.Notifications.addPermissionChangedHandler((granted) => {
OneSignal.Notifications.addPermissionObserver((granted) => {
this.OSLog('OneSignal: permission changed:', granted.permission);
});
}
Expand Down
2 changes: 1 addition & 1 deletion ios/RCTOneSignal/RCTOneSignal.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#import "../OneSignalFramework.h"
#endif

@interface RCTOneSignal : NSObject <OSPushSubscriptionObserver, OSPermissionObserver, OSInAppMessageLifecycleHandler>
@interface RCTOneSignal : NSObject <OSPushSubscriptionObserver, OSNotificationPermissionObserver, OSInAppMessageLifecycleListener, OSInAppMessageClickListener>

+ (RCTOneSignal *) sharedInstance;

Expand Down
26 changes: 13 additions & 13 deletions ios/RCTOneSignal/RCTOneSignal.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ @implementation RCTOneSignal {
BOOL didInitialize;
}

OSNotificationOpenedResult* coldStartOSNotificationOpenedResult;
OSNotificationClickResult* coldStartOSNotificationClickResult;

+ (RCTOneSignal *) sharedInstance {
static dispatch_once_t token = 0;
Expand Down Expand Up @@ -74,28 +74,28 @@ - (void)sendEvent:(NSString *)eventName withBody:(NSDictionary *)body {
[RCTOneSignalEventEmitter sendEventWithName:eventName withBody:body];
}

- (void)onOSPushSubscriptionChangedWithStateChanges:(OSPushSubscriptionStateChanges * _Nonnull)stateChanges {
[self sendEvent:OSEventString(SubscriptionChanged) withBody:[stateChanges.to jsonRepresentation]];
- (void)onPushSubscriptionDidChangeWithState:(OSPushSubscriptionChangedState * _Nonnull)state {
[self sendEvent:OSEventString(SubscriptionChanged) withBody:[state.current jsonRepresentation]];
}

- (void)onOSPermissionChanged:(OSPermissionState *)state {
[self sendEvent:OSEventString(PermissionChanged) withBody:[state jsonRepresentation]];
- (void)onNotificationPermissionDidChange:(BOOL)permission {
[self sendEvent:OSEventString(PermissionChanged) withBody:@{@"permission": @(permission)}];
}

- (void)onWillDisplayInAppMessage:(OSInAppMessage * _Nonnull)message {
[self sendEvent:OSEventString(InAppMessageWillDisplay) withBody:[message jsonRepresentation]];
- (void)onWillDisplayInAppMessage:(OSInAppMessageWillDisplayEvent * _Nonnull)event {
[self sendEvent:OSEventString(InAppMessageWillDisplay) withBody:[event.message jsonRepresentation]];
}

- (void)onDidDisplayInAppMessage:(OSInAppMessage * _Nonnull)message {
[self sendEvent:OSEventString(InAppMessageDidDisplay) withBody:[message jsonRepresentation]];
- (void)onDidDisplayInAppMessage:(OSInAppMessageDidDisplayEvent * _Nonnull)event {
[self sendEvent:OSEventString(InAppMessageDidDisplay) withBody:[event.message jsonRepresentation]];
}

- (void)onWillDismissInAppMessage:(OSInAppMessage * _Nonnull)message {
[self sendEvent:OSEventString(InAppMessageWillDismiss) withBody:[message jsonRepresentation]];
- (void)onWillDismissInAppMessage:(OSInAppMessageWillDismissEvent * _Nonnull)event {
[self sendEvent:OSEventString(InAppMessageWillDismiss) withBody:[event.message jsonRepresentation]];
}

- (void)onDidDismissInAppMessage:(OSInAppMessage * _Nonnull)message {
[self sendEvent:OSEventString(InAppMessageDidDismiss) withBody:[message jsonRepresentation]];
- (void)onDidDismissInAppMessage:(OSInAppMessageDidDismissEvent * _Nonnull)event {
[self sendEvent:OSEventString(InAppMessageDidDismiss) withBody:[event.message jsonRepresentation]];
}

- (void)dealloc {
Expand Down
4 changes: 2 additions & 2 deletions ios/RCTOneSignal/RCTOneSignalEventEmitter.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
typedef NS_ENUM(NSInteger, OSNotificationEventTypes) {
PermissionChanged,
SubscriptionChanged,
NotificationWillShowInForeground,
NotificationWillDisplayInForeground,
NotificationClicked,
InAppMessageClicked,
InAppMessageWillDisplay,
Expand All @@ -24,7 +24,7 @@ typedef NS_ENUM(NSInteger, OSNotificationEventTypes) {
InAppMessageDidDismiss,
};

#define OSNotificationEventTypesArray @[@"OneSignal-permissionChanged",@"OneSignal-subscriptionChanged",@"OneSignal-notificationWillShowInForeground",@"OneSignal-notificationClicked",@"OneSignal-inAppMessageClicked", @"OneSignal-inAppMessageWillDisplay", @"OneSignal-inAppMessageDidDisplay", @"OneSignal-inAppMessageWillDismiss", @"OneSignal-inAppMessageDidDismiss"]
#define OSNotificationEventTypesArray @[@"OneSignal-permissionChanged",@"OneSignal-subscriptionChanged",@"OneSignal-notificationWillDisplayInForeground",@"OneSignal-notificationClicked",@"OneSignal-inAppMessageClicked", @"OneSignal-inAppMessageWillDisplay", @"OneSignal-inAppMessageDidDisplay", @"OneSignal-inAppMessageWillDismiss", @"OneSignal-inAppMessageDidDismiss"]

#define OSEventString(enum) [OSNotificationEventTypesArray objectAtIndex:enum]

Expand Down
Loading

0 comments on commit be6676e

Please sign in to comment.