Skip to content

Commit ee63a38

Browse files
committed
[User Model] Rename notification event listeners
* Make all notification listeners 0:* (some were 0:1) * Renamed all "handlers" to be either a "listener" or an "observer" depending on their purpose. All callbacks now have pattern "onXXXXXX" for each callback. * Use the terminology "click" instead of "open". * Add IEventNotifier.hasSubscribers to determine if there are subscribers. A few implementing classes had to add this. * Replaced `complete()` pattern with `preventDefault()` pattern in NSE notification received callback and foreground onWillDisplay callback. * Add IDisplayableNotification and IDisplayableMutableNotification which adds a `display()` function to an INotification. This allows the manual display of a notification, when `event.preventDefault()` is called. * Internally, rework NotificationGenerationProcessor to accommodate `preventDefault` pattern. The notification is now constructed when the notification job is constructed and is never copied/altered until the end of its lifecycle. * Update example application for new naming conventions and patterns. * Update for new naming conventions and patterns
1 parent 4172f8b commit ee63a38

File tree

53 files changed

+584
-762
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+584
-762
lines changed

Examples/OneSignalDemo/app/src/main/java/com/onesignal/sdktest/activity/MainActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ protected void onCreate(Bundle savedInstanceState) {
2121
setContentView(R.layout.main_activity_layout);
2222

2323
viewModel = new MainActivityViewModel();
24-
OneSignal.getNotifications().addPermissionChangedHandler(viewModel);
24+
OneSignal.getNotifications().addPermissionObserver(viewModel);
2525
// TODO("STILL SUPPORT?")
2626
// OneSignal.addSubscriptionObserver(viewModel);
2727
// OneSignal.addEmailSubscriptionObserver(viewModel);

Examples/OneSignalDemo/app/src/main/java/com/onesignal/sdktest/application/MainApplication.java

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
import com.onesignal.inAppMessages.IInAppMessageClickResult;
1414
import com.onesignal.inAppMessages.IInAppMessageLifecycleHandler;
1515
import com.onesignal.debug.LogLevel;
16-
import com.onesignal.notifications.INotification;
16+
import com.onesignal.notifications.IDisplayableNotification;
17+
import com.onesignal.notifications.INotificationLifecycleListener;
18+
import com.onesignal.notifications.INotificationWillDisplayEvent;
1719
import com.onesignal.sdktest.BuildConfig;
1820
import com.onesignal.sdktest.R;
1921
import com.onesignal.sdktest.constant.Tag;
@@ -76,23 +78,40 @@ public void inAppMessageClicked(@Nullable IInAppMessageClickResult result) {
7678
}
7779
});
7880

79-
OneSignal.getNotifications().setNotificationClickHandler(result ->
80-
{
81-
Log.v("MainApplication", "INotificationOpenedResult: " + result);
82-
});
83-
84-
OneSignal.getNotifications().setNotificationWillShowInForegroundHandler(notificationReceivedEvent ->
85-
{
86-
Log.v("MainApplication", "NotificationWillShowInForegroundHandler fired!" +
87-
" with notification event: " + notificationReceivedEvent.toString());
88-
89-
INotification notification = notificationReceivedEvent.getNotification();
90-
JSONObject data = notification.getAdditionalData();
81+
OneSignal.getNotifications().addClickListener(event ->
82+
{
83+
Log.v("MainApplication", "INotificationClickListener.onClick fired!" +
84+
" with event: " + event);
85+
});
9186

92-
notificationReceivedEvent.complete(notification);
93-
});
87+
OneSignal.getNotifications().addForegroundLifecycleListener(new INotificationLifecycleListener() {
88+
@Override
89+
public void onWillDisplay(@NonNull INotificationWillDisplayEvent event) {
90+
Log.v("MainApplication", "INotificationLifecycleListener.onWillDisplay fired!" +
91+
" with event: " + event);
92+
93+
IDisplayableNotification notification = event.getNotification();
94+
JSONObject data = notification.getAdditionalData();
95+
96+
//Prevent OneSignal from displaying the notification immediately on return. Spin
97+
//up a new thread to mimic some asynchronous behavior, when the async behavior (which
98+
//takes 2 seconds) completes, then the notification can be displayed.
99+
event.preventDefault();
100+
Runnable r = () -> {
101+
try {
102+
Thread.sleep(2000);
103+
} catch (InterruptedException e) {
104+
e.printStackTrace();
105+
}
106+
107+
notification.display();
108+
};
109+
110+
Thread t = new Thread(r);
111+
t.start();
112+
}
113+
});
94114

95-
// OneSignal.getNotifications().setUnsubscribeWhenNotificationsAreDisabled(true);
96115
OneSignal.getInAppMessages().setPaused(true);
97116
OneSignal.getLocation().setShared(false);
98117

Examples/OneSignalDemo/app/src/main/java/com/onesignal/sdktest/model/ActivityViewModel.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
import android.content.Context;
55
import androidx.appcompat.app.AppCompatActivity;
66

7-
import com.onesignal.notifications.IPermissionChangedHandler;
7+
import com.onesignal.notifications.IPermissionObserver;
88

99
/**
1010
* This is the interface created with a few generic methods for setting a ViewModel
1111
* as the responsible guardian of an Activity
1212
*/
13-
public interface ActivityViewModel extends IPermissionChangedHandler { // TODO() extends OSPermissionObserver, OSSubscriptionObserver, OSEmailSubscriptionObserver {
13+
public interface ActivityViewModel extends IPermissionObserver {
1414

1515
/**
1616
* Casts Context of the given Activity to an Activity object

Examples/OneSignalDemo/app/src/main/java/com/onesignal/sdktest/model/MainActivityViewModel.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ public void networkDisconnected() {
359359
}
360360

361361
@Override
362-
public void onPermissionChanged(@Nullable boolean permission) {
362+
public void onNotificationPermissionChange(@Nullable boolean permission) {
363363
refreshSubscriptionState();
364364
}
365365

Examples/OneSignalDemo/app/src/main/java/com/onesignal/sdktest/model/SplashActivityViewModel.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ private void attemptEnterApplication() {
141141
}
142142

143143
@Override
144-
public void onPermissionChanged(@Nullable boolean permission) {
144+
public void onNotificationPermissionChange(@Nullable boolean permission) {
145145

146146
}
147147
}
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,27 @@
11
package com.onesignal.sdktest.notification;
22

3-
import android.content.Context;
43
import android.util.Log;
54

65
import com.onesignal.notifications.IActionButton;
7-
import com.onesignal.notifications.IMutableNotification;
8-
import com.onesignal.notifications.INotification;
6+
import com.onesignal.notifications.IDisplayableMutableNotification;
97
import com.onesignal.notifications.INotificationReceivedEvent;
10-
import com.onesignal.notifications.IRemoteNotificationReceivedHandler;
8+
import com.onesignal.notifications.INotificationServiceExtension;
119
import com.onesignal.sdktest.R;
1210

13-
public class NotificationServiceExtension implements IRemoteNotificationReceivedHandler {
11+
public class NotificationServiceExtension implements INotificationServiceExtension {
1412

1513
@Override
16-
public void remoteNotificationReceived(Context context, INotificationReceivedEvent notificationReceivedEvent) {
17-
Log.v("MainApplication", "IRemoteNotificationReceivedHandler fired!" + " with INotificationReceivedEvent: " + notificationReceivedEvent.toString());
14+
public void onNotificationReceived(INotificationReceivedEvent event) {
15+
Log.v("MainApplication", "IRemoteNotificationReceivedHandler fired!" + " with INotificationReceivedEvent: " + event.toString());
1816

19-
INotification notification = notificationReceivedEvent.getNotification();
17+
IDisplayableMutableNotification notification = event.getNotification();
2018

2119
if (notification.getActionButtons() != null) {
2220
for (IActionButton button : notification.getActionButtons()) {
2321
Log.v("MainApplication", "ActionButton: " + button.toString());
2422
}
2523
}
2624

27-
IMutableNotification mutableNotification = notification.mutableCopy();
28-
mutableNotification.setExtender(builder -> builder.setColor(context.getResources().getColor(R.color.colorPrimary)));
29-
30-
// If complete isn't call within a time period of 25 seconds, OneSignal internal logic will show the original notification
31-
notificationReceivedEvent.complete(mutableNotification);
25+
notification.setExtender(builder -> builder.setColor(event.getContext().getResources().getColor(R.color.colorPrimary)));
3226
}
3327
}

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/common/events/EventProducer.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import kotlinx.coroutines.withContext
1010
*/
1111
open class EventProducer<THandler> : IEventNotifier<THandler> {
1212

13+
override val hasSubscribers: Boolean
14+
get() = _subscribers.any()
15+
1316
private val _subscribers: MutableList<THandler> = mutableListOf()
1417

1518
override fun subscribe(handler: THandler) {

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/common/events/IEventNotifier.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ package com.onesignal.common.events
1212
* @param THandler The type that the implementor is expecting to raise events to.
1313
*/
1414
interface IEventNotifier<THandler> {
15+
/**
16+
* Whether there are currently any subscribers.
17+
*/
18+
val hasSubscribers: Boolean
1519

1620
/**
1721
* Subscribe to listen for events.

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/common/modeling/Model.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,4 +334,6 @@ open class Model(
334334

335335
override fun subscribe(handler: IModelChangedHandler) = _changeNotifier.subscribe(handler)
336336
override fun unsubscribe(handler: IModelChangedHandler) = _changeNotifier.unsubscribe(handler)
337+
override val hasSubscribers: Boolean
338+
get() = _changeNotifier.hasSubscribers
337339
}

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/common/modeling/ModelStore.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,4 +147,6 @@ abstract class ModelStore<TModel>(
147147

148148
override fun subscribe(handler: IModelStoreChangeHandler<TModel>) = _changeSubscription.subscribe(handler)
149149
override fun unsubscribe(handler: IModelStoreChangeHandler<TModel>) = _changeSubscription.unsubscribe(handler)
150+
override val hasSubscribers: Boolean
151+
get() = _changeSubscription.hasSubscribers
150152
}

0 commit comments

Comments
 (0)