Skip to content

Commit 18103b8

Browse files
committed
Moved event logic into OneSignalHmsEventBridge
* Moved logic from HmsMessageServiceOneSignal into to a new OneSignalHmsEventBridge class. * OneSignalHmsEventBridge need to be called if by the app if: 1. Another push provider library / SDK is in the app. 2. The app has it's own HmsMessageService class. * This is required as HMS only creates one HmsMessageService instance. * See comemnts in this commit for even more details.
1 parent 211cf7f commit 18103b8

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

OneSignalSDK/onesignal/src/main/java/com/onesignal/HmsMessageServiceOneSignal.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,21 @@
33
import com.huawei.hms.push.HmsMessageService;
44
import com.huawei.hms.push.RemoteMessage;
55

6+
/**
7+
* The hms:push library creates an instance of this service based on the
8+
* intent-filter action "com.huawei.push.action.MESSAGING_EVENT".
9+
*
10+
* WARNING: HMS only creates one {@link HmsMessageService} instance.
11+
* This means this class will not be used by the app in the following cases:
12+
* 1. Another push provider library / SDK is in the app.
13+
* - Due to ordering in the AndroidManifest.xml OneSignal may or may not be the winner.
14+
* - App needs to it's own {@link HmsMessageService} and call bridging / forwarding APIs
15+
* on each SDK / library for both to work.
16+
* 2. The app has it's own {@link HmsMessageService} class.
17+
* If either of these are true the app must have it's own {@link HmsMessageService} with AndroidManifest.xml
18+
* entries and call {@link OneSignalHmsEventBridge} for these methods. This is noted in the OneSignal SDK
19+
* Huawei setup guide.
20+
*/
621
public class HmsMessageServiceOneSignal extends HmsMessageService {
722

823
/**
@@ -13,8 +28,7 @@ public class HmsMessageServiceOneSignal extends HmsMessageService {
1328
*/
1429
@Override
1530
public void onNewToken(String token) {
16-
OneSignal.Log(OneSignal.LOG_LEVEL.INFO, "HmsMessageServiceOneSignal.onNewToken - HMS token: " + token);
17-
PushRegistratorHMS.fireCallback(token);
31+
OneSignalHmsEventBridge.onNewToken(this, token);
1832
}
1933

2034
/**
@@ -28,6 +42,6 @@ public void onNewToken(String token) {
2842
*/
2943
@Override
3044
public void onMessageReceived(RemoteMessage message) {
31-
NotificationPayloadProcessorHMS.processDataMessageReceived(this, message.getData());
45+
OneSignalHmsEventBridge.onMessageReceived(this, message);
3246
}
3347
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.onesignal;
2+
3+
import android.content.Context;
4+
import android.support.annotation.NonNull;
5+
6+
import com.huawei.hms.push.RemoteMessage;
7+
8+
/**
9+
* If you have your own {@link com.huawei.hms.push.HmsMessageService} defined in your app please also
10+
* call {@link OneSignalHmsEventBridge#onNewToken} and {@link OneSignalHmsEventBridge#onMessageReceived}
11+
* as this is required for some OneSignal features.
12+
* If you don't have a class that extends from {@link com.huawei.hms.push.HmsMessageService}
13+
* or anther SDK / Library that handles HMS push then you don't need to use this class.
14+
* OneSignal automatically gets these events.
15+
*/
16+
public class OneSignalHmsEventBridge {
17+
18+
public static void onNewToken(@NonNull Context context, @NonNull String token) {
19+
OneSignal.Log(OneSignal.LOG_LEVEL.INFO, "HmsMessageServiceOneSignal.onNewToken - HMS token: " + token);
20+
PushRegistratorHMS.fireCallback(token);
21+
}
22+
23+
public static void onMessageReceived(@NonNull Context context, @NonNull RemoteMessage message) {
24+
NotificationPayloadProcessorHMS.processDataMessageReceived(context, message.getData());
25+
}
26+
}

0 commit comments

Comments
 (0)