Skip to content

Commit be6676e

Browse files
authored
Merge pull request #1524 from OneSignal/user_model_beta2/rename_events
[User Model] Rename Events & API Changes
2 parents 7dd6b19 + e20d6db commit be6676e

20 files changed

+692
-596
lines changed

MIGRATION_GUIDE.md

Lines changed: 78 additions & 69 deletions
Large diffs are not rendered by default.

android/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ dependencies {
3131

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

3636
testImplementation 'junit:junit:4.12'
3737
}

android/src/main/java/com/geektime/rnonesignalandroid/RNOneSignal.java

Lines changed: 160 additions & 147 deletions
Large diffs are not rendered by default.

android/src/main/java/com/geektime/rnonesignalandroid/RNUtils.java

Lines changed: 13 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,17 @@
1212
import com.facebook.react.bridge.WritableNativeArray;
1313
import com.facebook.react.bridge.WritableNativeMap;
1414

15-
// import com.onesignal.OSInAppMessage;
1615
import com.onesignal.inAppMessages.IInAppMessage;
1716
import com.onesignal.inAppMessages.IInAppMessageClickResult;
17+
import com.onesignal.inAppMessages.IInAppMessageWillDisplayEvent;
18+
import com.onesignal.inAppMessages.IInAppMessageDidDisplayEvent;
19+
import com.onesignal.inAppMessages.IInAppMessageWillDismissEvent;
20+
import com.onesignal.inAppMessages.IInAppMessageDidDismissEvent;
1821
import com.onesignal.notifications.INotification;
19-
import com.onesignal.notifications.INotificationAction;
2022
import com.onesignal.notifications.INotificationClickResult;
2123
import com.onesignal.notifications.INotificationReceivedEvent;
2224
import com.onesignal.user.subscriptions.IPushSubscription;
23-
import com.onesignal.user.subscriptions.ISubscription;
25+
import com.onesignal.user.subscriptions.PushSubscriptionState;
2426

2527
import org.json.JSONArray;
2628
import org.json.JSONException;
@@ -109,15 +111,6 @@ public static HashMap<String, Object> convertNotificationToMap(INotification not
109111
return hash;
110112
}
111113

112-
public static HashMap<String, Object> convertNotificationClickResultToMap(INotificationClickResult openResult) throws JSONException {
113-
HashMap<String, Object> hash = new HashMap<>();
114-
115-
hash.put("notification", convertNotificationToMap(openResult.getNotification()));
116-
hash.put("action", convertNotificationActionToMap(openResult.getAction()));
117-
118-
return hash;
119-
}
120-
121114
public static HashMap<String, Object> convertInAppMessageToMap(IInAppMessage message) {
122115
HashMap<String, Object> hash = new HashMap<>();
123116

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

129-
public static HashMap<String, Object> convertInAppMessageClickedActionToMap(IInAppMessageClickResult result) {
122+
public static HashMap<String, Object> convertInAppMessageClickResultToMap(IInAppMessageClickResult result) {
130123
HashMap<String, Object> hash = new HashMap<>();
131124

132-
hash.put("clickName", result.getAction().getClickName());
133-
hash.put("clickUrl", result.getAction().getClickUrl());
134-
hash.put("firstClick", result.getAction().isFirstClick());
135-
hash.put("closesMessage", result.getAction().getClosesMessage());
125+
hash.put("actionId", result.getActionId());
126+
hash.put("urlTarget", result.getUrlTarget());
127+
hash.put("url", result.getUrl());
128+
hash.put("closingMessage", result.getClosingMessage());
136129

137130
return hash;
138131
}
139132

140-
public static HashMap<String, Object> convertOnSubscriptionChangedToMap(IPushSubscription state) {
133+
public static HashMap<String, Object> convertOnSubscriptionChangedToMap(PushSubscriptionState state) {
141134
HashMap<String, Object> hash = new HashMap<>();
142135

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

178-
public static Collection<String> convertReableArrayIntoStringCollection(ReadableArray readableArray) {
171+
public static Collection<String> convertReadableArrayIntoStringCollection(ReadableArray readableArray) {
179172
ArrayList<String> strings = new ArrayList<>();
180173
for (Object object : readableArray.toArrayList()) {
181174
if (object instanceof String)
@@ -184,7 +177,7 @@ public static Collection<String> convertReableArrayIntoStringCollection(Readable
184177
return strings;
185178
}
186179

187-
public static HashMap<String, String> convertReableMapIntoStringMap(ReadableMap readableMap) {
180+
public static HashMap<String, String> convertReadableMapIntoStringMap(ReadableMap readableMap) {
188181
HashMap<String, String> stringMap = new HashMap<>();
189182
ReadableMapKeySetIterator iter = readableMap.keySetIterator();
190183

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

209-
private static HashMap<String, Object> convertNotificationActionToMap(INotificationAction action) {
210-
HashMap<String, Object> hash = new HashMap<>();
211-
212-
hash.put("id", action.getActionId());
213-
214-
switch (action.getType()) {
215-
case Opened:
216-
hash.put("type", 0);
217-
break;
218-
case ActionTaken:
219-
hash.put("type", 1);
220-
}
221-
222-
return hash;
223-
}
224-
225202
private static List<Object> convertJSONArrayToList(JSONArray array) throws JSONException {
226203
List<Object> list = new ArrayList<>();
227204

examples/RNOneSignalTS/src/OSButtons.tsx

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -420,61 +420,43 @@ class OSButtons extends React.Component<Props> {
420420
privacyConsentFields() {
421421
const {loggingFunction} = this.props;
422422

423-
const getPrivacyConsentButton = renderButtonView(
424-
'Get Privacy Consent',
425-
async () => {
426-
const granted = await OneSignal.getPrivacyConsent();
427-
loggingFunction('Privacy consent granted: ', granted);
428-
}
429-
);
430-
431-
const setPrivacyConsentTrueButton = renderButtonView(
423+
const setPrivacyConsentGivenTrueButton = renderButtonView(
432424
'Set Privacy Consent to true',
433425
async () => {
434-
await OneSignal.setPrivacyConsent(true);
426+
await OneSignal.setConsentGiven(true);
435427
loggingFunction('Privacy Consent set to true');
436428
}
437429
);
438430

439-
const setPrivacyConsentFalseButton = renderButtonView(
431+
const setPrivacyConsentGivenFalseButton = renderButtonView(
440432
'Set Privacy Consent to false',
441433
async () => {
442-
await OneSignal.setPrivacyConsent(false);
434+
await OneSignal.setConsentGiven(false);
443435
loggingFunction('Privacy Consent set to false');
444436
}
445437
);
446438

447-
const getRequiresPrivacyConsentButton = renderButtonView(
448-
'Get Requires Privacy Consent',
449-
async () => {
450-
const granted = await OneSignal.getRequiresPrivacyConsent();
451-
loggingFunction('Requires Privacy Consent: ', granted);
452-
}
453-
);
454-
455-
const setRequiresPrivacyConsentTrueButton = renderButtonView(
439+
const setPrivacyConsentRequiredTrueButton = renderButtonView(
456440
'Set Requiers Privacy Consent to true',
457441
async () => {
458-
await OneSignal.setRequiresPrivacyConsent(true);
442+
await OneSignal.setConsentRequired(true);
459443
loggingFunction('Requires Privacy Consent set to true');
460444
}
461445
);
462446

463-
const setRequiresPrivacyConsentFalseButton = renderButtonView(
447+
const setPrivacyConsentRequiredFalseButton = renderButtonView(
464448
'Set Requiers Privacy Consent to false',
465449
async () => {
466-
await OneSignal.setRequiresPrivacyConsent(false);
450+
await OneSignal.setConsentRequired(false);
467451
loggingFunction('Requires Privacy Consent set to false');
468452
}
469453
);
470454

471455
return [
472-
getPrivacyConsentButton,
473-
setPrivacyConsentTrueButton,
474-
setPrivacyConsentFalseButton,
475-
getRequiresPrivacyConsentButton,
476-
setRequiresPrivacyConsentTrueButton,
477-
setRequiresPrivacyConsentFalseButton,
456+
setPrivacyConsentGivenTrueButton,
457+
setPrivacyConsentGivenFalseButton,
458+
setPrivacyConsentRequiredTrueButton,
459+
setPrivacyConsentRequiredFalseButton,
478460
];
479461
}
480462

examples/RNOneSignalTS/src/OSDemo.tsx

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -33,60 +33,61 @@ class OSDemo extends React.Component<Props, State> {
3333
OneSignal.initialize(APP_ID);
3434
OneSignal.Debug.setLogLevel(6);
3535

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

4141
const cancelButton = {
4242
text: 'Cancel',
4343
onPress: () => {
44-
notifReceivedEvent.complete();
44+
event.preventDefault();
4545
},
4646
style: 'cancel',
4747
};
4848

4949
const completeButton = {
50-
text: 'Complete',
50+
text: 'Display',
5151
onPress: () => {
52-
notifReceivedEvent.complete(notification);
52+
event.getNotification().display();
5353
},
5454
};
5555

56-
Alert.alert('Complete notification?', notification.title, [cancelButton, completeButton], {
56+
Alert.alert('Display notification?', notif.title, [cancelButton, completeButton], {
5757
cancelable: true,
5858
});
5959
},
6060
);
6161

62-
OneSignal.Notifications.setNotificationClickHandler((notification) => {
63-
this.OSLog('OneSignal: notification opened:', notification);
62+
OneSignal.Notifications.addEventListener('click', (event) => {
63+
this.OSLog('OneSignal: notification clicked:', event);
6464
});
6565

66-
OneSignal.InAppMessages.setClickHandler((event) => {
66+
OneSignal.InAppMessages.addEventListener('click', (event) =>{
6767
this.OSLog('OneSignal IAM clicked:', event);
6868
});
6969

70-
OneSignal.InAppMessages.setLifecycleHandler({
71-
onWillDisplayInAppMessage: (message) => {
72-
this.OSLog('OneSignal: will display IAM: ', message.messageId);
73-
},
74-
onDidDisplayInAppMessage: (message) => {
75-
this.OSLog('OneSignal: did display IAM: ', message.messageId);
76-
},
77-
onWillDismissInAppMessage: (message) => {
78-
this.OSLog('OneSignal: will dismiss IAM: ', message.messageId);
79-
},
80-
onDidDismissInAppMessage: (message) => {
81-
this.OSLog('OneSignal: did dismiss IAM: ', message.messageId);
82-
},
70+
OneSignal.InAppMessages.addEventListener('willDisplay', (event) =>{
71+
this.OSLog('OneSignal: will display IAM: ', event);
72+
});
73+
74+
OneSignal.InAppMessages.addEventListener('didDisplay', (event) =>{
75+
this.OSLog('OneSignal: did display IAM: ', event);
76+
});
77+
78+
OneSignal.InAppMessages.addEventListener('willDismiss', (event) =>{
79+
this.OSLog('OneSignal: will dismiss IAM: ', event);
80+
});
81+
82+
OneSignal.InAppMessages.addEventListener('didDismiss', (event) =>{
83+
this.OSLog('OneSignal: did dismiss IAM: ', event);
8384
});
8485

85-
OneSignal.User.PushSubscription.addChangeHandler((subscription) => {
86+
OneSignal.User.PushSubscription.addObserver((subscription) => {
8687
this.OSLog('OneSignal: subscription changed:', subscription);
8788
});
8889

89-
OneSignal.Notifications.addPermissionChangedHandler((granted) => {
90+
OneSignal.Notifications.addPermissionObserver((granted) => {
9091
this.OSLog('OneSignal: permission changed:', granted.permission);
9192
});
9293
}

ios/RCTOneSignal/RCTOneSignal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#import "../OneSignalFramework.h"
66
#endif
77

8-
@interface RCTOneSignal : NSObject <OSPushSubscriptionObserver, OSPermissionObserver, OSInAppMessageLifecycleHandler>
8+
@interface RCTOneSignal : NSObject <OSPushSubscriptionObserver, OSNotificationPermissionObserver, OSInAppMessageLifecycleListener, OSInAppMessageClickListener>
99

1010
+ (RCTOneSignal *) sharedInstance;
1111

ios/RCTOneSignal/RCTOneSignal.m

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ @implementation RCTOneSignal {
3030
BOOL didInitialize;
3131
}
3232

33-
OSNotificationOpenedResult* coldStartOSNotificationOpenedResult;
33+
OSNotificationClickResult* coldStartOSNotificationClickResult;
3434

3535
+ (RCTOneSignal *) sharedInstance {
3636
static dispatch_once_t token = 0;
@@ -74,28 +74,28 @@ - (void)sendEvent:(NSString *)eventName withBody:(NSDictionary *)body {
7474
[RCTOneSignalEventEmitter sendEventWithName:eventName withBody:body];
7575
}
7676

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

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

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

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

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

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

101101
- (void)dealloc {

ios/RCTOneSignal/RCTOneSignalEventEmitter.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
typedef NS_ENUM(NSInteger, OSNotificationEventTypes) {
1616
PermissionChanged,
1717
SubscriptionChanged,
18-
NotificationWillShowInForeground,
18+
NotificationWillDisplayInForeground,
1919
NotificationClicked,
2020
InAppMessageClicked,
2121
InAppMessageWillDisplay,
@@ -24,7 +24,7 @@ typedef NS_ENUM(NSInteger, OSNotificationEventTypes) {
2424
InAppMessageDidDismiss,
2525
};
2626

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

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

0 commit comments

Comments
 (0)