Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add onNewToken with Bundle HMS method support #1433

Merged
merged 3 commits into from
Nov 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Examples/OneSignalDemo/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ dependencies {
// Test local OneSignal SDK
gmsImplementation(project(':onesignal'))

huaweiImplementation 'com.huawei.hms:push:5.0.0.300'
huaweiImplementation 'com.huawei.hms:push:5.3.0.304'
huaweiImplementation 'com.huawei.hms:location:4.0.0.300'
// Omit Google / Firebase libraries for Huawei builds.
huaweiImplementation(project(':onesignal')) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.onesignal.sdktest.notification;

import android.os.Bundle;

import com.huawei.hms.push.HmsMessageService;
import com.huawei.hms.push.RemoteMessage;
import com.onesignal.OneSignal;
Expand All @@ -13,10 +15,20 @@ public class HmsMessageServiceAppLevel extends HmsMessageService {
* This method callback must be completed in 10 seconds. Otherwise, you need to start a new Job for callback processing.
*
* @param token token
* @param bundle bundle
*/
@Override
public void onNewToken(String token, Bundle bundle) {
OneSignal.onesignalLog(OneSignal.LOG_LEVEL.DEBUG, "HmsMessageServiceAppLevel onNewToken refresh token:" + token + " bundle: " + bundle);

// Forward event on to OneSignal SDK
OneSignalHmsEventBridge.onNewToken(this, token, bundle);
}

@Deprecated
@Override
public void onNewToken(String token) {
OneSignal.onesignalLog(OneSignal.LOG_LEVEL.DEBUG, "HMS onNewToken refresh token:" + token);
OneSignal.onesignalLog(OneSignal.LOG_LEVEL.DEBUG, "HmsMessageServiceAppLevel onNewToken refresh token:" + token);

// Forward event on to OneSignal SDK
OneSignalHmsEventBridge.onNewToken(this, token);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.onesignal;

import android.os.Bundle;

import com.huawei.hms.push.HmsMessageService;
import com.huawei.hms.push.RemoteMessage;

Expand All @@ -24,10 +26,22 @@ public class HmsMessageServiceOneSignal extends HmsMessageService {
* When an app calls the getToken method to apply for a token from the server,
* if the server does not return the token during current method calling, the server can return the token through this method later.
* This method callback must be completed in 10 seconds. Otherwise, you need to start a new Job for callback processing.
*
* @param token token
* @param bundle bundle
*/
@Override
public void onNewToken(String token, Bundle bundle) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Jeasmine What happens if the app is using an old hms:push library in their app/build.gradle, a version before this method signature was added? Would they get runtime error? Or would this method just not be called?

If the above is an issue, is there anything that could be done to handle it? Or do we just need to instruct developers to upgrade their hms.push when they update OneSignal?

Copy link
Contributor Author

@Jeasmine Jeasmine Nov 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jkasten2 There won't be a problem because they will have implemented the

public void onNewToken(String token) that will be calling the

    public static void onNewToken(@NonNull Context context, @NonNull String token) {
        onNewToken(context, token, null);
    }

The new method won't be called unless the user overrides it. But we should recommend updating

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The older hms:push doesn't know about the new onNewToken(String, Bundle), only onNewToken(String). If the app includes our new OneSignal-Android-SDK (with this change) AND the app has the old hms:push version 4 I believe HMS is going to try to call onNewToken(String) on our HmsMessageServiceOneSignal and it's not going to find it. It think it might crash, or OneSignal won't get a push token.

If the app developer has their own class that extends HmsMessageService this won't be an issue, as HmsMessageServiceOneSignal won't be called by HMS. We instead instruct the app developer to call OneSignalHmsEventBridge from their class which I see you have kept both methods for backwards compatibility as expected.

OneSignal.onesignalLog(OneSignal.LOG_LEVEL.DEBUG, "HmsMessageServiceOneSignal onNewToken refresh token:" + token);

OneSignalHmsEventBridge.onNewToken(this, token, bundle);
}

@Deprecated
@Override
public void onNewToken(String token) {
OneSignal.onesignalLog(OneSignal.LOG_LEVEL.DEBUG, "HmsMessageServiceOneSignal onNewToken refresh token:" + token);

OneSignalHmsEventBridge.onNewToken(this, token);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package com.onesignal;

import android.content.Context;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.huawei.hms.push.RemoteMessage;

import java.util.concurrent.atomic.AtomicBoolean;

/**
* If you have your own {@link com.huawei.hms.push.HmsMessageService} defined in your app please also
* call {@link OneSignalHmsEventBridge#onNewToken} and {@link OneSignalHmsEventBridge#onMessageReceived}
Expand All @@ -16,9 +20,27 @@
*/
public class OneSignalHmsEventBridge {

private static final AtomicBoolean firstToken = new AtomicBoolean(true);

/**
* Method used by last HMS push version 5.3.0.304 and upper
*/
public static void onNewToken(@NonNull Context context, @NonNull String token, @Nullable Bundle bundle) {
if (firstToken.compareAndSet(true, false)) {
OneSignal.Log(OneSignal.LOG_LEVEL.INFO, "OneSignalHmsEventBridge onNewToken - HMS token: " + token + " Bundle: " + bundle);
PushRegistratorHMS.fireCallback(token);
} else {
OneSignal.Log(OneSignal.LOG_LEVEL.INFO, "OneSignalHmsEventBridge ignoring onNewToken - HMS token: " + token + " Bundle: " + bundle);
}
}

/**
* This method is being deprecated
* @see OneSignalHmsEventBridge#onNewToken(Context, String, Bundle)
*/
@Deprecated
public static void onNewToken(@NonNull Context context, @NonNull String token) {
OneSignal.Log(OneSignal.LOG_LEVEL.INFO, "HmsMessageServiceOneSignal.onNewToken - HMS token: " + token);
PushRegistratorHMS.fireCallback(token);
onNewToken(context, token, null);
}

public static void onMessageReceived(@NonNull Context context, @NonNull RemoteMessage message) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ private synchronized void getHMSTokenTask(@NonNull Context context, @NonNull Reg
if (!TextUtils.isEmpty(pushToken)) {
OneSignal.Log(LOG_LEVEL.INFO, "Device registered for HMS, push token = " + pushToken);
callback.complete(pushToken, UserState.PUSH_STATUS_SUBSCRIBED);
}
else
} else {
waitForOnNewPushTokenEvent(callback);
}
}

private static void doTimeOutWait() {
Expand All @@ -82,7 +82,7 @@ private static void doTimeOutWait() {

// If EMUI 9.x or older getToken will always return null.
// We must wait for HmsMessageService.onNewToken to fire instead.
void waitForOnNewPushTokenEvent(@NonNull RegisteredHandler callback) {
private void waitForOnNewPushTokenEvent(@NonNull RegisteredHandler callback) {
doTimeOutWait();
if (!callbackSuccessful) {
OneSignal.Log(OneSignal.LOG_LEVEL.ERROR, "HmsMessageServiceOneSignal.onNewToken timed out.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static void doTimeOutWait() {
if (backgroundSuccessful) {
// prepare required since doTimeOutWait will be run from a new background thread.
Looper.prepare();
new HmsMessageServiceOneSignal().onNewToken(ShadowHmsInstanceId.DEFAULT_MOCK_HMS_TOKEN_VALUE);
new HmsMessageServiceOneSignal().onNewToken(ShadowHmsInstanceId.DEFAULT_MOCK_HMS_TOKEN_VALUE, null);
}
}
}