Skip to content

Commit ede53a0

Browse files
authored
Merge pull request #1765 from OneSignal/user-model/notification-events
[User Model] Rename notification event listeners
2 parents e7c9594 + 88559d5 commit ede53a0

File tree

57 files changed

+615
-793
lines changed

Some content is hidden

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

57 files changed

+615
-793
lines changed

Examples/OneSignalDemo/app/src/huawei/java/com/onesignal/sdktest/notification/HmsMessageServiceAppLevel.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.huawei.hms.push.HmsMessageService;
77
import com.huawei.hms.push.RemoteMessage;
88
import com.onesignal.notifications.bridges.OneSignalHmsEventBridge;
9+
import com.onesignal.sdktest.constant.Tag;
910

1011
public class HmsMessageServiceAppLevel extends HmsMessageService {
1112

@@ -19,7 +20,7 @@ public class HmsMessageServiceAppLevel extends HmsMessageService {
1920
*/
2021
@Override
2122
public void onNewToken(String token, Bundle bundle) {
22-
Log.d("MainApplication", "HmsMessageServiceAppLevel onNewToken refresh token:" + token + " bundle: " + bundle);
23+
Log.d(Tag.LOG_TAG, "HmsMessageServiceAppLevel onNewToken refresh token:" + token + " bundle: " + bundle);
2324

2425
// Forward event on to OneSignal SDK
2526
OneSignalHmsEventBridge.INSTANCE.onNewToken(this, token, bundle);
@@ -28,7 +29,7 @@ public void onNewToken(String token, Bundle bundle) {
2829
@Deprecated
2930
@Override
3031
public void onNewToken(String token) {
31-
Log.d("MainApplication", "HmsMessageServiceAppLevel onNewToken refresh token:" + token);
32+
Log.d(Tag.LOG_TAG, "HmsMessageServiceAppLevel onNewToken refresh token:" + token);
3233

3334
// Forward event on to OneSignal SDK
3435
OneSignalHmsEventBridge.INSTANCE.onNewToken(this, token);
@@ -44,16 +45,16 @@ public void onNewToken(String token) {
4445
*/
4546
@Override
4647
public void onMessageReceived(RemoteMessage message) {
47-
Log.d("MainApplication", "HMS onMessageReceived: " + message);
48-
Log.d("MainApplication", "HMS onMessageReceived.ttl:" + message.getTtl());
49-
Log.d("MainApplication", "HMS onMessageReceived.data:" + message.getData());
50-
Log.d("MainApplication", "HMS onMessageReceived.title: " + message.getNotification().getTitle());
51-
Log.d("MainApplication", "HMS onMessageReceived.body: " + message.getNotification().getBody());
52-
Log.d("MainApplication", "HMS onMessageReceived.icon: " + message.getNotification().getIcon());
53-
Log.d("MainApplication", "HMS onMessageReceived.color: " + message.getNotification().getColor());
54-
Log.d("MainApplication", "HMS onMessageReceived.channelId: " + message.getNotification().getChannelId());
55-
Log.d("MainApplication", "HMS onMessageReceived.imageURL: " + message.getNotification().getImageUrl());
56-
Log.d("MainApplication", "HMS onMessageReceived.tag: " + message.getNotification().getTag());
48+
Log.d(Tag.LOG_TAG, "HMS onMessageReceived: " + message);
49+
Log.d(Tag.LOG_TAG, "HMS onMessageReceived.ttl:" + message.getTtl());
50+
Log.d(Tag.LOG_TAG, "HMS onMessageReceived.data:" + message.getData());
51+
Log.d(Tag.LOG_TAG, "HMS onMessageReceived.title: " + message.getNotification().getTitle());
52+
Log.d(Tag.LOG_TAG, "HMS onMessageReceived.body: " + message.getNotification().getBody());
53+
Log.d(Tag.LOG_TAG, "HMS onMessageReceived.icon: " + message.getNotification().getIcon());
54+
Log.d(Tag.LOG_TAG, "HMS onMessageReceived.color: " + message.getNotification().getColor());
55+
Log.d(Tag.LOG_TAG, "HMS onMessageReceived.channelId: " + message.getNotification().getChannelId());
56+
Log.d(Tag.LOG_TAG, "HMS onMessageReceived.imageURL: " + message.getNotification().getImageUrl());
57+
Log.d(Tag.LOG_TAG, "HMS onMessageReceived.tag: " + message.getNotification().getTag());
5758

5859
// Forward event on to OneSignal SDK
5960
OneSignalHmsEventBridge.INSTANCE.onMessageReceived(this, message);

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: 41 additions & 22 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;
@@ -24,6 +26,7 @@
2426
import org.json.JSONObject;
2527

2628
public class MainApplication extends MultiDexApplication {
29+
private static final int SLEEP_TIME_TO_MIMIC_ASYNC_OPERATION = 2000;
2730

2831
public MainApplication() {
2932
// run strict mode default in debug mode to surface any potential issues easier
@@ -50,53 +53,69 @@ public void onCreate() {
5053
OneSignal.getInAppMessages().setInAppMessageLifecycleHandler(new IInAppMessageLifecycleHandler() {
5154
@Override
5255
public void onWillDisplayInAppMessage(@NonNull IInAppMessage message) {
53-
Log.v("MainApplication", "onWillDisplayInAppMessage");
56+
Log.v(Tag.LOG_TAG, "onWillDisplayInAppMessage");
5457
}
5558

5659
@Override
5760
public void onDidDisplayInAppMessage(@NonNull IInAppMessage message) {
58-
Log.v("MainApplication", "onDidDisplayInAppMessage");
61+
Log.v(Tag.LOG_TAG, "onDidDisplayInAppMessage");
5962
}
6063

6164
@Override
6265
public void onWillDismissInAppMessage(@NonNull IInAppMessage message) {
63-
Log.v("MainApplication", "onWillDismissInAppMessage");
66+
Log.v(Tag.LOG_TAG, "onWillDismissInAppMessage");
6467
}
6568

6669
@Override
6770
public void onDidDismissInAppMessage(@NonNull IInAppMessage message) {
68-
Log.v("MainApplication", "onDidDismissInAppMessage");
71+
Log.v(Tag.LOG_TAG, "onDidDismissInAppMessage");
6972
}
7073
});
7174

7275
OneSignal.getInAppMessages().setInAppMessageClickHandler(new IInAppMessageClickHandler() {
7376
@Override
7477
public void inAppMessageClicked(@Nullable IInAppMessageClickResult result) {
75-
Log.v("MainApplication", "inAppMessageClicked");
78+
Log.v(Tag.LOG_TAG, "INotificationClickListener.inAppMessageClicked");
7679
}
7780
});
7881

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();
82+
OneSignal.getNotifications().addClickListener(event ->
83+
{
84+
Log.v(Tag.LOG_TAG, "INotificationClickListener.onClick fired" +
85+
" with event: " + event);
86+
});
9187

92-
notificationReceivedEvent.complete(notification);
93-
});
88+
OneSignal.getNotifications().addForegroundLifecycleListener(new INotificationLifecycleListener() {
89+
@Override
90+
public void onWillDisplay(@NonNull INotificationWillDisplayEvent event) {
91+
Log.v(Tag.LOG_TAG, "INotificationLifecycleListener.onWillDisplay fired" +
92+
" with event: " + event);
93+
94+
IDisplayableNotification notification = event.getNotification();
95+
JSONObject data = notification.getAdditionalData();
96+
97+
//Prevent OneSignal from displaying the notification immediately on return. Spin
98+
//up a new thread to mimic some asynchronous behavior, when the async behavior (which
99+
//takes 2 seconds) completes, then the notification can be displayed.
100+
event.preventDefault();
101+
Runnable r = () -> {
102+
try {
103+
Thread.sleep(SLEEP_TIME_TO_MIMIC_ASYNC_OPERATION);
104+
} catch (InterruptedException ignored) {
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

99-
Log.d(Tag.DEBUG, Text.ONESIGNAL_SDK_INIT);
118+
Log.d(Tag.LOG_TAG, Text.ONESIGNAL_SDK_INIT);
100119
}
101120

102121
}
Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package com.onesignal.sdktest.constant;
22

33
public class Tag {
4-
5-
public static final String DEBUG = "DEBUG";
6-
public static final String ERROR = "ERROR";
7-
4+
public static final String LOG_TAG = "sdktest";
85
}

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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ private void setupOneSignalSDK() {
7171
boolean isInAppMessagingPaused = SharedPreferenceUtil.getCachedInAppMessagingPausedStatus(context);
7272
OneSignal.getInAppMessages().setPaused(isInAppMessagingPaused);
7373

74-
Log.d(Tag.DEBUG, Text.PRIVACY_CONSENT_REQUIRED_SET + ": " + privacyConsent);
74+
Log.d(Tag.LOG_TAG, Text.PRIVACY_CONSENT_REQUIRED_SET + ": " + privacyConsent);
7575

7676
// boolean isEmailCached = attemptSignIn(new EmailUpdateCallback() {
7777
// @Override
@@ -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,28 @@
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;
10+
import com.onesignal.sdktest.constant.Tag;
1211

13-
public class NotificationServiceExtension implements IRemoteNotificationReceivedHandler {
12+
public class NotificationServiceExtension implements INotificationServiceExtension {
1413

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

19-
INotification notification = notificationReceivedEvent.getNotification();
18+
IDisplayableMutableNotification notification = event.getNotification();
2019

2120
if (notification.getActionButtons() != null) {
2221
for (IActionButton button : notification.getActionButtons()) {
23-
Log.v("MainApplication", "ActionButton: " + button.toString());
22+
Log.v(Tag.LOG_TAG, "ActionButton: " + button.toString());
2423
}
2524
}
2625

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);
26+
notification.setExtender(builder -> builder.setColor(event.getContext().getResources().getColor(R.color.colorPrimary)));
3227
}
3328
}

Examples/OneSignalDemo/app/src/main/java/com/onesignal/sdktest/notification/OneSignalNotificationSender.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public static void sendDeviceNotification(final Notification notification) {
7777
if (scanner.useDelimiter("\\A").hasNext())
7878
responseStr = scanner.next();
7979
scanner.close();
80-
Log.d(Tag.DEBUG, "Success sending notification: " + responseStr);
80+
Log.d(Tag.LOG_TAG, "Success sending notification: " + responseStr);
8181
}
8282
else {
8383
InputStream inputStream = con.getErrorStream();
@@ -86,7 +86,7 @@ public static void sendDeviceNotification(final Notification notification) {
8686
if (scanner.useDelimiter("\\A").hasNext())
8787
responseStr = scanner.next();
8888
scanner.close();
89-
Log.d(Tag.ERROR, "Failure sending notification: " + responseStr);
89+
Log.d(Tag.LOG_TAG, "Failure sending notification: " + responseStr);
9090
}
9191
}
9292
} catch (Exception e) {

0 commit comments

Comments
 (0)