Skip to content

Commit

Permalink
Merge pull request #1765 from OneSignal/user-model/notification-events
Browse files Browse the repository at this point in the history
[User Model] Rename notification event listeners
  • Loading branch information
brismithers authored and jinliu9508 committed Jan 31, 2024
2 parents c6e31af + ebd7a6b commit 4039a03
Show file tree
Hide file tree
Showing 57 changed files with 615 additions and 793 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.huawei.hms.push.HmsMessageService;
import com.huawei.hms.push.RemoteMessage;
import com.onesignal.notifications.bridges.OneSignalHmsEventBridge;
import com.onesignal.sdktest.constant.Tag;

public class HmsMessageServiceAppLevel extends HmsMessageService {

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

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

// Forward event on to OneSignal SDK
OneSignalHmsEventBridge.INSTANCE.onNewToken(this, token);
Expand All @@ -44,16 +45,16 @@ public void onNewToken(String token) {
*/
@Override
public void onMessageReceived(RemoteMessage message) {
Log.d("MainApplication", "HMS onMessageReceived: " + message);
Log.d("MainApplication", "HMS onMessageReceived.ttl:" + message.getTtl());
Log.d("MainApplication", "HMS onMessageReceived.data:" + message.getData());
Log.d("MainApplication", "HMS onMessageReceived.title: " + message.getNotification().getTitle());
Log.d("MainApplication", "HMS onMessageReceived.body: " + message.getNotification().getBody());
Log.d("MainApplication", "HMS onMessageReceived.icon: " + message.getNotification().getIcon());
Log.d("MainApplication", "HMS onMessageReceived.color: " + message.getNotification().getColor());
Log.d("MainApplication", "HMS onMessageReceived.channelId: " + message.getNotification().getChannelId());
Log.d("MainApplication", "HMS onMessageReceived.imageURL: " + message.getNotification().getImageUrl());
Log.d("MainApplication", "HMS onMessageReceived.tag: " + message.getNotification().getTag());
Log.d(Tag.LOG_TAG, "HMS onMessageReceived: " + message);
Log.d(Tag.LOG_TAG, "HMS onMessageReceived.ttl:" + message.getTtl());
Log.d(Tag.LOG_TAG, "HMS onMessageReceived.data:" + message.getData());
Log.d(Tag.LOG_TAG, "HMS onMessageReceived.title: " + message.getNotification().getTitle());
Log.d(Tag.LOG_TAG, "HMS onMessageReceived.body: " + message.getNotification().getBody());
Log.d(Tag.LOG_TAG, "HMS onMessageReceived.icon: " + message.getNotification().getIcon());
Log.d(Tag.LOG_TAG, "HMS onMessageReceived.color: " + message.getNotification().getColor());
Log.d(Tag.LOG_TAG, "HMS onMessageReceived.channelId: " + message.getNotification().getChannelId());
Log.d(Tag.LOG_TAG, "HMS onMessageReceived.imageURL: " + message.getNotification().getImageUrl());
Log.d(Tag.LOG_TAG, "HMS onMessageReceived.tag: " + message.getNotification().getTag());

// Forward event on to OneSignal SDK
OneSignalHmsEventBridge.INSTANCE.onMessageReceived(this, message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.main_activity_layout);

viewModel = new MainActivityViewModel();
OneSignal.getNotifications().addPermissionChangedHandler(viewModel);
OneSignal.getNotifications().addPermissionObserver(viewModel);
// TODO("STILL SUPPORT?")
// OneSignal.addSubscriptionObserver(viewModel);
// OneSignal.addEmailSubscriptionObserver(viewModel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
import com.onesignal.inAppMessages.IInAppMessageClickResult;
import com.onesignal.inAppMessages.IInAppMessageLifecycleHandler;
import com.onesignal.debug.LogLevel;
import com.onesignal.notifications.INotification;
import com.onesignal.notifications.IDisplayableNotification;
import com.onesignal.notifications.INotificationLifecycleListener;
import com.onesignal.notifications.INotificationWillDisplayEvent;
import com.onesignal.sdktest.BuildConfig;
import com.onesignal.sdktest.R;
import com.onesignal.sdktest.constant.Tag;
Expand All @@ -24,6 +26,7 @@
import org.json.JSONObject;

public class MainApplication extends MultiDexApplication {
private static final int SLEEP_TIME_TO_MIMIC_ASYNC_OPERATION = 2000;

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

@Override
public void onDidDisplayInAppMessage(@NonNull IInAppMessage message) {
Log.v("MainApplication", "onDidDisplayInAppMessage");
Log.v(Tag.LOG_TAG, "onDidDisplayInAppMessage");
}

@Override
public void onWillDismissInAppMessage(@NonNull IInAppMessage message) {
Log.v("MainApplication", "onWillDismissInAppMessage");
Log.v(Tag.LOG_TAG, "onWillDismissInAppMessage");
}

@Override
public void onDidDismissInAppMessage(@NonNull IInAppMessage message) {
Log.v("MainApplication", "onDidDismissInAppMessage");
Log.v(Tag.LOG_TAG, "onDidDismissInAppMessage");
}
});

OneSignal.getInAppMessages().setInAppMessageClickHandler(new IInAppMessageClickHandler() {
@Override
public void inAppMessageClicked(@Nullable IInAppMessageClickResult result) {
Log.v("MainApplication", "inAppMessageClicked");
Log.v(Tag.LOG_TAG, "INotificationClickListener.inAppMessageClicked");
}
});

OneSignal.getNotifications().setNotificationClickHandler(result ->
{
Log.v("MainApplication", "INotificationOpenedResult: " + result);
});

OneSignal.getNotifications().setNotificationWillShowInForegroundHandler(notificationReceivedEvent ->
{
Log.v("MainApplication", "NotificationWillShowInForegroundHandler fired!" +
" with notification event: " + notificationReceivedEvent.toString());

INotification notification = notificationReceivedEvent.getNotification();
JSONObject data = notification.getAdditionalData();
OneSignal.getNotifications().addClickListener(event ->
{
Log.v(Tag.LOG_TAG, "INotificationClickListener.onClick fired" +
" with event: " + event);
});

notificationReceivedEvent.complete(notification);
});
OneSignal.getNotifications().addForegroundLifecycleListener(new INotificationLifecycleListener() {
@Override
public void onWillDisplay(@NonNull INotificationWillDisplayEvent event) {
Log.v(Tag.LOG_TAG, "INotificationLifecycleListener.onWillDisplay fired" +
" with event: " + event);

IDisplayableNotification notification = event.getNotification();
JSONObject data = notification.getAdditionalData();

//Prevent OneSignal from displaying the notification immediately on return. Spin
//up a new thread to mimic some asynchronous behavior, when the async behavior (which
//takes 2 seconds) completes, then the notification can be displayed.
event.preventDefault();
Runnable r = () -> {
try {
Thread.sleep(SLEEP_TIME_TO_MIMIC_ASYNC_OPERATION);
} catch (InterruptedException ignored) {
}

notification.display();
};

Thread t = new Thread(r);
t.start();
}
});

// OneSignal.getNotifications().setUnsubscribeWhenNotificationsAreDisabled(true);
OneSignal.getInAppMessages().setPaused(true);
OneSignal.getLocation().setShared(false);

Log.d(Tag.DEBUG, Text.ONESIGNAL_SDK_INIT);
Log.d(Tag.LOG_TAG, Text.ONESIGNAL_SDK_INIT);
}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package com.onesignal.sdktest.constant;

public class Tag {

public static final String DEBUG = "DEBUG";
public static final String ERROR = "ERROR";

public static final String LOG_TAG = "sdktest";
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
import android.content.Context;
import androidx.appcompat.app.AppCompatActivity;

import com.onesignal.notifications.IPermissionChangedHandler;
import com.onesignal.notifications.IPermissionObserver;

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

/**
* Casts Context of the given Activity to an Activity object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ public void networkDisconnected() {
}

@Override
public void onPermissionChanged(@Nullable boolean permission) {
public void onNotificationPermissionChange(@Nullable boolean permission) {
refreshSubscriptionState();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private void setupOneSignalSDK() {
boolean isInAppMessagingPaused = SharedPreferenceUtil.getCachedInAppMessagingPausedStatus(context);
OneSignal.getInAppMessages().setPaused(isInAppMessagingPaused);

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

// boolean isEmailCached = attemptSignIn(new EmailUpdateCallback() {
// @Override
Expand Down Expand Up @@ -141,7 +141,7 @@ private void attemptEnterApplication() {
}

@Override
public void onPermissionChanged(@Nullable boolean permission) {
public void onNotificationPermissionChange(@Nullable boolean permission) {

}
}
Original file line number Diff line number Diff line change
@@ -1,33 +1,28 @@
package com.onesignal.sdktest.notification;

import android.content.Context;
import android.util.Log;

import com.onesignal.notifications.IActionButton;
import com.onesignal.notifications.IMutableNotification;
import com.onesignal.notifications.INotification;
import com.onesignal.notifications.IDisplayableMutableNotification;
import com.onesignal.notifications.INotificationReceivedEvent;
import com.onesignal.notifications.IRemoteNotificationReceivedHandler;
import com.onesignal.notifications.INotificationServiceExtension;
import com.onesignal.sdktest.R;
import com.onesignal.sdktest.constant.Tag;

public class NotificationServiceExtension implements IRemoteNotificationReceivedHandler {
public class NotificationServiceExtension implements INotificationServiceExtension {

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

INotification notification = notificationReceivedEvent.getNotification();
IDisplayableMutableNotification notification = event.getNotification();

if (notification.getActionButtons() != null) {
for (IActionButton button : notification.getActionButtons()) {
Log.v("MainApplication", "ActionButton: " + button.toString());
Log.v(Tag.LOG_TAG, "ActionButton: " + button.toString());
}
}

IMutableNotification mutableNotification = notification.mutableCopy();
mutableNotification.setExtender(builder -> builder.setColor(context.getResources().getColor(R.color.colorPrimary)));

// If complete isn't call within a time period of 25 seconds, OneSignal internal logic will show the original notification
notificationReceivedEvent.complete(mutableNotification);
notification.setExtender(builder -> builder.setColor(event.getContext().getResources().getColor(R.color.colorPrimary)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public static void sendDeviceNotification(final Notification notification) {
if (scanner.useDelimiter("\\A").hasNext())
responseStr = scanner.next();
scanner.close();
Log.d(Tag.DEBUG, "Success sending notification: " + responseStr);
Log.d(Tag.LOG_TAG, "Success sending notification: " + responseStr);
}
else {
InputStream inputStream = con.getErrorStream();
Expand All @@ -86,7 +86,7 @@ public static void sendDeviceNotification(final Notification notification) {
if (scanner.useDelimiter("\\A").hasNext())
responseStr = scanner.next();
scanner.close();
Log.d(Tag.ERROR, "Failure sending notification: " + responseStr);
Log.d(Tag.LOG_TAG, "Failure sending notification: " + responseStr);
}
}
} catch (Exception e) {
Expand Down
Loading

0 comments on commit 4039a03

Please sign in to comment.