Skip to content

Commit 15a8d00

Browse files
committed
HmsMessageService.onMessageReceived displaying
* Now if an HMS "Data Message" is recieved in the OneSignal format it will be processed through the normal flow like GCM / FCM messages do. - All features we support for FCM Android notificatons now apply to these types.
1 parent af45ea5 commit 15a8d00

File tree

4 files changed

+50
-1
lines changed

4 files changed

+50
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ private void setAbort() {
131131
return processedResult;
132132
}
133133

134-
private static void startGCMService(Context context, Bundle bundle) {
134+
static void startGCMService(Context context, Bundle bundle) {
135135
// If no remote resources have to be downloaded don't create a job which could add some delay.
136136
if (!NotificationBundleProcessor.hasRemoteResource(bundle)) {
137137
BundleCompat taskExtras = setCompatBundleForServer(bundle, BundleCompatFactory.getInstance());

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.onesignal;
22

33
import com.huawei.hms.push.HmsMessageService;
4+
import com.huawei.hms.push.RemoteMessage;
45

56
public class HmsMessageServiceOneSignal extends HmsMessageService {
67

@@ -16,4 +17,17 @@ public void onNewToken(String token) {
1617
PushRegistratorHMS.fireCallback(token);
1718
}
1819

20+
/**
21+
* This method is called in the following cases:
22+
* 1. "Data messages" - App process is alive when received.
23+
* 2. "Notification Message" - foreground_show = false and app is in focus
24+
* This method callback must be completed in 10 seconds, start a new Job if more time is needed.
25+
* - This may only be a restriction for #1
26+
* - 10 sec limit didn't seem to hit for #2, even when backgrounding the app after the method starts
27+
* @param message RemoteMessage
28+
*/
29+
@Override
30+
public void onMessageReceived(RemoteMessage message) {
31+
NotificationPayloadProcessorHMS.processDataMessageReceived(this, message.getData());
32+
}
1933
}

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.onesignal;
22

33
import android.app.Activity;
4+
import android.content.Context;
45
import android.content.Intent;
56
import android.os.Bundle;
67
import android.support.annotation.NonNull;
@@ -71,4 +72,20 @@ private static void handleProcessJsonOpenData(@NonNull Activity activity, @NonNu
7172
OSNotificationFormatHelper.getOSNotificationIdFromJson(jsonData)
7273
);
7374
}
75+
76+
static void processDataMessageReceived(@NonNull Context context, @NonNull String data) {
77+
OneSignal.setAppContext(context);
78+
Bundle bundle = OSUtils.jsonStringToBundle(data);
79+
if (bundle == null)
80+
return;
81+
82+
NotificationBundleProcessor.ProcessedBundleResult processedResult = NotificationBundleProcessor.processBundleFromReceiver(context, bundle);
83+
84+
// Return if the notification will NOT be handled by normal GcmIntentService display flow.
85+
if (processedResult.processed())
86+
return;
87+
88+
// TODO: 4.0.0 or after - What is in GcmBroadcastReceiver should be split into a shared class to support FCM, HMS, and ADM
89+
GcmBroadcastReceiver.startGCMService(context, bundle);
90+
}
7491
}

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
import java.util.Collection;
6060
import java.util.Collections;
6161
import java.util.HashSet;
62+
import java.util.Iterator;
6263
import java.util.Locale;
6364
import java.util.Set;
6465
import java.util.UUID;
@@ -633,6 +634,23 @@ static boolean hasConfigChangeFlag(Activity activity, int configChangeFlag) {
633634
return result;
634635
}
635636

637+
static @Nullable Bundle jsonStringToBundle(@NonNull String data) {
638+
try {
639+
JSONObject jsonObject = new JSONObject(data);
640+
Bundle bundle = new Bundle();
641+
Iterator iterator = jsonObject.keys();
642+
while (iterator.hasNext()) {
643+
String key = (String)iterator.next();
644+
String value = jsonObject.getString(key);
645+
bundle.putString(key, value);
646+
}
647+
return bundle;
648+
} catch (JSONException e) {
649+
e.printStackTrace();
650+
return null;
651+
}
652+
}
653+
636654
static boolean shouldLogMissingAppIdError(@Nullable String appId) {
637655
if (appId != null)
638656
return false;

0 commit comments

Comments
 (0)