Skip to content

Commit 6a46bd4

Browse files
author
lastpeony
committed
refactor
1 parent 0f25b0e commit 6a46bd4

File tree

7 files changed

+102
-40
lines changed

7 files changed

+102
-40
lines changed

webrtc-android-framework/src/main/java/io/antmedia/webrtcandroidframework/api/IWebRTCClient.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.antmedia.webrtcandroidframework.api;
22

3+
import org.json.JSONArray;
4+
import org.json.JSONObject;
35
import org.webrtc.DataChannel;
46
import org.webrtc.RtpParameters;
57
import org.webrtc.SurfaceViewRenderer;
@@ -303,7 +305,7 @@ void publish(String streamId, String token, boolean videoCallEnabled, boolean au
303305
/**
304306
* Send push notification to subscribers.
305307
*/
306-
void sendPushNotification(String subscriberId, String authToken, String pushNotificationContent, String subscriberIdsToNotify);
308+
void sendPushNotification(String subscriberId, String authToken, JSONObject pushNotificationContent, JSONArray subscriberIdsToNotify);
307309

308310
StatsCollector getStatsCollector();
309311

webrtc-android-framework/src/main/java/io/antmedia/webrtcandroidframework/core/WebRTCClient.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
import androidx.annotation.NonNull;
2121

22+
import org.json.JSONArray;
23+
import org.json.JSONObject;
2224
import org.webrtc.AddIceObserver;
2325
import org.webrtc.AudioSource;
2426
import org.webrtc.AudioTrack;
@@ -1028,9 +1030,9 @@ public void registerPushNotificationToken(String subscriberId, String authToken,
10281030
}
10291031
}
10301032

1031-
public void sendPushNotification(String subscriberId, String authToken, String pushNotificationContent, String subscriberIdsToNotify) {
1033+
public void sendPushNotification(String subscriberId, String authToken, JSONObject pushNotificationContent, JSONArray receiverSubscriberIdArray) {
10321034
if (wsHandler != null && wsHandler.isConnected()) {
1033-
wsHandler.sendPushNotification(subscriberId, authToken, pushNotificationContent, subscriberIdsToNotify);
1035+
wsHandler.sendPushNotification(subscriberId, authToken, pushNotificationContent, receiverSubscriberIdArray);
10341036
}
10351037
}
10361038

webrtc-android-framework/src/main/java/io/antmedia/webrtcandroidframework/websocket/WebSocketHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,15 +364,15 @@ public void registerPushNotificationToken(String subscriberId, String authToken,
364364
}
365365
}
366366

367-
public void sendPushNotification(String subscriberId, String authToken, String pushNotificationContent, String subscriberIdsToNotify) {
367+
public void sendPushNotification(String subscriberId, String authToken, JSONObject pushNotificationContent, JSONArray receiverSubscriberIdArray) {
368368
checkIfCalledOnValidThread();
369369
JSONObject json = new JSONObject();
370370
try {
371371
json.put(WebSocketConstants.COMMAND, WebSocketConstants.SEND_PUSH_NOTIFICATION_COMMAND);
372372
json.put(WebSocketConstants.SUBSCRIBER_ID, subscriberId);
373373
json.put(WebSocketConstants.TOKEN, authToken);
374374
json.put(WebSocketConstants.PUSH_NOTIFICATION_CONTENT, pushNotificationContent);
375-
json.put(WebSocketConstants.SUBSCRIBER_IDS_TO_NOTIFY, subscriberIdsToNotify);
375+
json.put(WebSocketConstants.SUBSCRIBER_IDS_TO_NOTIFY, receiverSubscriberIdArray);
376376
sendTextMessage(json.toString());
377377
} catch (JSONException e) {
378378
e.printStackTrace();

webrtc-android-framework/src/test/java/io/antmedia/webrtcandroidframework/WebRTCClientTest.java

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,18 +1129,41 @@ public void registerPushNotificationToken_doesNotRegisterTokenWhenWebSocketHandl
11291129
public void sendPushNotification_sendsNotificationWhenWebSocketHandlerIsConnected() {
11301130
when(wsHandler.isConnected()).thenReturn(true);
11311131

1132-
webRTCClient.sendPushNotification("subscriberId", "authToken", "pushNotificationContent", "subscriberIdsToNotify");
1132+
JSONObject pushNotificationContent = new JSONObject();
1133+
JSONArray receiverSubscriberIdArray = new JSONArray();
11331134

1134-
verify(wsHandler, times(1)).sendPushNotification("subscriberId", "authToken", "pushNotificationContent", "subscriberIdsToNotify");
1135+
try{
1136+
pushNotificationContent.put("Caller","Caller1");
1137+
pushNotificationContent.put("StreamId", "stream1");
1138+
receiverSubscriberIdArray.put("subscriber1");
1139+
}catch (Exception e){
1140+
e.printStackTrace();
1141+
}
1142+
1143+
1144+
webRTCClient.sendPushNotification("subscriberId", "authToken", pushNotificationContent, receiverSubscriberIdArray);
1145+
1146+
verify(wsHandler, times(1)).sendPushNotification("subscriberId", "authToken", pushNotificationContent, receiverSubscriberIdArray);
11351147
}
11361148

11371149
@Test
11381150
public void sendPushNotification_doesNotSendNotificationWhenWebSocketHandlerIsNotConnected() {
11391151
when(wsHandler.isConnected()).thenReturn(false);
11401152

1141-
webRTCClient.sendPushNotification("subscriberId", "authToken", "pushNotificationContent", "subscriberIdsToNotify");
1153+
JSONObject pushNotificationContent = new JSONObject();
1154+
JSONArray receiverSubscriberIdArray = new JSONArray();
1155+
1156+
try{
1157+
pushNotificationContent.put("Caller","Caller1");
1158+
pushNotificationContent.put("StreamId", "stream1");
1159+
receiverSubscriberIdArray.put("subscriber1");
1160+
}catch (Exception e){
1161+
e.printStackTrace();
1162+
}
1163+
1164+
webRTCClient.sendPushNotification("subscriberId", "authToken", pushNotificationContent, receiverSubscriberIdArray);
11421165

1143-
verify(wsHandler, times(0)).sendPushNotification("subscriberId", "authToken", "pushNotificationContent", "subscriberIdsToNotify");
1166+
verify(wsHandler, times(0)).sendPushNotification("subscriberId", "authToken", pushNotificationContent, receiverSubscriberIdArray);
11441167
}
11451168

11461169
@Test

webrtc-android-sample-app/src/main/java/io/antmedia/webrtc_android_sample_app/advanced/notification/CallNotificationActivity.java

Lines changed: 59 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,49 @@
11
package io.antmedia.webrtc_android_sample_app.advanced.notification;
22

3-
import static androidx.activity.result.ActivityResultCallerKt.registerForActivityResult;
43

5-
import android.app.Activity;
4+
import android.content.SharedPreferences;
65
import android.os.Bundle;
76

7+
import org.json.JSONArray;
8+
import org.json.JSONException;
9+
import org.json.JSONObject;
810
import org.webrtc.SurfaceViewRenderer;
911

10-
import io.antmedia.webrtc_android_sample_app.MainActivity;
1112
import io.antmedia.webrtc_android_sample_app.R;
13+
import io.antmedia.webrtc_android_sample_app.basic.SettingsActivity;
14+
import io.antmedia.webrtcandroidframework.api.DefaultWebRTCListener;
1215
import io.antmedia.webrtcandroidframework.api.IWebRTCClient;
16+
import io.antmedia.webrtcandroidframework.api.IWebRTCListener;
1317

1418
import android.Manifest;
15-
import android.annotation.SuppressLint;
1619
import android.content.pm.PackageManager;
1720
import android.os.Build;
18-
import android.os.Bundle;
1921

2022
import androidx.activity.ComponentActivity;
2123
import androidx.activity.result.ActivityResultLauncher;
2224
import androidx.activity.result.contract.ActivityResultContracts;
23-
import androidx.annotation.NonNull;
24-
import androidx.annotation.RequiresApi;
25-
import androidx.appcompat.app.AppCompatActivity;
2625
import androidx.core.content.ContextCompat;
26+
import androidx.preference.PreferenceManager;
2727

28-
import android.util.Log;
29-
import android.widget.Toast;
30-
31-
import com.google.android.gms.tasks.OnCompleteListener;
32-
import com.google.android.gms.tasks.Task;
3328
import com.google.firebase.FirebaseApp;
34-
import com.google.firebase.messaging.FirebaseMessaging;
35-
import com.google.firebase.messaging.RemoteMessage;
3629

37-
import java.util.concurrent.atomic.AtomicInteger;
30+
public class CallNotificationActivity extends ComponentActivity {
3831

32+
String streamId;
3933

40-
public class CallNotificationActivity extends ComponentActivity {
34+
String subscriberId;
35+
36+
String receiverSubscriberId;
37+
38+
String authToken;
39+
40+
String pushNotificationToken;
41+
42+
String tokenType;
43+
44+
JSONObject pushNotificationContent;
45+
46+
JSONArray receiverSubscriberIdArray;
4147

4248
@Override
4349
protected void onCreate(Bundle savedInstanceState) {
@@ -46,42 +52,69 @@ protected void onCreate(Bundle savedInstanceState) {
4652

4753
FirebaseApp.initializeApp(this);
4854

55+
SharedPreferences sharedPreferences =
56+
PreferenceManager.getDefaultSharedPreferences(this);
57+
4958
//FirebaseMessaging.getInstance().setAutoInitEnabled(true);
5059

5160
SurfaceViewRenderer fullScreenRenderer = findViewById(R.id.full_screen_renderer);
61+
String serverUrl = sharedPreferences.getString(getString(R.string.serverAddress), SettingsActivity.DEFAULT_WEBSOCKET_URL);
5262

5363
IWebRTCClient webRTCClient = IWebRTCClient.builder()
5464
.setActivity(this)
65+
.setInitiateBeforeStream(true)
66+
.setWebRTCListener(createWebRTCListener())
5567
.setLocalVideoRenderer(fullScreenRenderer)
56-
.setServerUrl("wss://ovh36.antmedia.io:5443/LiveApp/websocket")
68+
.setServerUrl(serverUrl)
5769
.build();
5870

59-
String streamId = "streamId" + (int)(Math.random()*9999);
71+
streamId = "streamId" + (int)(Math.random()*9999);
6072

6173
PeerForNotificationActivity.streamId = streamId;
6274

6375
//Define the subscriberId and it can be any subscriber Id
64-
String subscriberId = "[email protected]";
76+
subscriberId = "[email protected]";
6577

6678
//Define the receiverSubscriberId and it can be any subscriber Id
67-
String receiverSubscriberId = "[email protected]";
79+
receiverSubscriberId = "[email protected]";
6880

6981
//Get auth token for Ant Media Server to authenticate the user.
7082
//it's JWT token generated with Subscription Authentication Key(subscriptionAuthenticationKey) in Application settings with subscriberId claim and it's value.
7183
//PushNotificationRestService can also be used to generate the authToken
72-
String authToken = "";
84+
authToken = "";
7385

74-
String pushNotificationToken = AntMediaFirebaseMessagingService.fcmToken;
86+
pushNotificationToken = AntMediaFirebaseMessagingService.fcmToken;
7587

76-
String tokenType = "fcm";
88+
tokenType = "fcm";
7789

78-
webRTCClient.registerPushNotificationToken(subscriberId, authToken, pushNotificationToken, tokenType);
90+
pushNotificationContent = new JSONObject();
91+
receiverSubscriberIdArray = new JSONArray();
92+
93+
try {
94+
pushNotificationContent.put("Caller", subscriberId);
95+
pushNotificationContent.put("StreamId", streamId);
96+
receiverSubscriberIdArray.put(receiverSubscriberId);
97+
} catch (JSONException e) {
98+
throw new RuntimeException(e);
99+
}
79100

80-
webRTCClient.sendPushNotification(subscriberId, authToken, "{\"Caller\":\""+subscriberId+"\",\"StreamId\":\""+streamId+"\"}", receiverSubscriberId);
81101

82102
askNotificationPermission();
83103
}
84104

105+
private IWebRTCListener createWebRTCListener() {
106+
return new DefaultWebRTCListener() {
107+
@Override
108+
public void onWebSocketConnected() {
109+
super.onWebSocketConnected();
110+
webRTCClient.registerPushNotificationToken(subscriberId, authToken, pushNotificationToken, tokenType);
111+
webRTCClient.sendPushNotification(subscriberId, authToken, pushNotificationContent, receiverSubscriberIdArray
112+
);
113+
}
114+
115+
};
116+
}
117+
85118
// [START ask_post_notifications]
86119
// Declare the launcher at the top of your Activity/Fragment:
87120
private final ActivityResultLauncher<String> requestPermissionLauncher =

webrtc-android-sample-app/src/main/java/io/antmedia/webrtc_android_sample_app/advanced/notification/PeerForNotificationActivity.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import android.os.Bundle;
77
import android.util.Log;
88
import android.view.View;
9-
import android.widget.Button;
109
import android.widget.EditText;
1110
import android.widget.TextView;
1211
import android.widget.Toast;
@@ -29,7 +28,7 @@
2928
import io.antmedia.webrtcandroidframework.api.IWebRTCListener;
3029

3130
public class PeerForNotificationActivity extends TestableActivity {
32-
private View broadcastingView;
31+
private TextView broadcastingTextView;
3332
public static String streamId;
3433
private IWebRTCClient webRTCClient;
3534

@@ -42,7 +41,7 @@ protected void onCreate(Bundle savedInstanceState) {
4241
SurfaceViewRenderer fullScreenRenderer = findViewById(R.id.full_screen_renderer);
4342
SurfaceViewRenderer pipRenderer = findViewById(R.id.pip_view_renderer);
4443

45-
broadcastingView = findViewById(R.id.broadcasting_text_view);
44+
broadcastingTextView = findViewById(R.id.broadcasting_text_view);
4645

4746
String serverUrl = sharedPreferences.getString(getString(R.string.serverAddress), SettingsActivity.DEFAULT_WEBSOCKET_URL);
4847

@@ -74,14 +73,14 @@ private IWebRTCListener createWebRTCListener() {
7473
@Override
7574
public void onPlayStarted(String streamId) {
7675
super.onPlayStarted(streamId);
77-
broadcastingView.setVisibility(View.VISIBLE);
76+
broadcastingTextView.setVisibility(View.VISIBLE);
7877
decrementIdle();
7978
}
8079

8180
@Override
8281
public void onPlayFinished(String streamId) {
8382
super.onPlayFinished(streamId);
84-
broadcastingView.setVisibility(View.GONE);
83+
broadcastingTextView.setVisibility(View.GONE);
8584
decrementIdle();
8685
}
8786
};

webrtc-android-sample-app/src/main/java/io/antmedia/webrtc_android_sample_app/basic/PublishActivity.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ protected void onCreate(Bundle savedInstanceState) {
4646
String generatedStreamId = "streamId" + (int)(Math.random()*9999);
4747
streamIdEditText.setText(generatedStreamId);
4848

49+
50+
51+
4952
webRTCClient = IWebRTCClient.builder()
5053
.setLocalVideoRenderer(fullScreenRenderer)
5154
.setServerUrl(serverUrl)

0 commit comments

Comments
 (0)