Android Twilio 视频通话,唤醒应用程序并将其置于前台
Android Twilio Video Call, wake up app and bring to foreground
我正在尝试使用 Twilio 视频通话提供原生视频通话体验。场景如下:
- AAA 称 BBB。
- 人BBB没有打开应用,在后台或前台,应用处于被杀死状态,phone甚至可能被锁定
- 当 AAA 打来电话时,应用打开时会显示视频 ui 并带有接听按钮。就像在 WhatsApp、Google Duo、Skype...
我们有 FCM 并正在接收推送通知。试图在来电时打开视频通话接听按钮,而不是点击通知,就像在 Whatsapp 中一样,Google Duo...(在 Android phones 中)
我们试图在后台打开一个套接字服务 运行。当传入呼叫事件发送到套接字时,套接字将侦听传入呼叫并打开 VideoCallActivity。
这是我们最好的选择,但到目前为止还没有成功。您将如何实现此功能?
所以我们已经找到了这个解决方案(当通知到达时,将应用程序调到前台)并且我发布它虽然已经有一段时间了:
FCM通知(firebase云消息通知)只需要在通知中发送“data”即可。所以通知的 JSON 结构中没有 Notification 对象,只有数据。通过这种方式,通知由您应用的 FirebaseMessagingService.java class 处理。请详细阅读以下内容以了解如何处理 2 种 FCM 通知类型。
https://firebase.google.com/docs/cloud-messaging/android/receive
https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages
在 FirebaseMessagingService.java class 中,启动带有 Intent 的视频通话 activity。不要忘记将此服务添加到 Manifest.xml
Intent intent = new Intent(this, VideoCallActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
getApplicationContext().startActivity(intent);
在 VideoCall activity 中,确保在 onCreate() 的开头有以下代码:
// These flags ensure that the activity can be launched when the screen is locked.
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
// to wake up the screen
PowerManager pm = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wakeLock = pm.newWakeLock((PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), "TAG");
wakeLock.acquire();
// to release the screen lock
KeyguardManager keyguardManager = (KeyguardManager) getApplicationContext().getSystemService(Context.KEYGUARD_SERVICE);
KeyguardManager.KeyguardLock keyguardLock = keyguardManager.newKeyguardLock("TAG");
keyguardLock.disableKeyguard();
使用适当的 intent-filter 将 VideoCallActivity 添加到 Manifest.xml:
<!-- Video Call -->
<activity
android:name=".ui.activities.video_call.VideoCallActivity"
android:launchMode="singleTop"
android:screenOrientation="portrait"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<!-- Note: these actions are notification actions -->
<action android:name="VIDEO_CALLING" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
可选:
要使 phone 响铃和振动:
// For Incoming Call
// 1. declare
private MediaPlayer incomingCallMediaPlayer;
// .2 in onCreate, if I'm the person that's calling, ring the phone
incomingCallMediaPlayer = MediaPlayer.create(this, R.raw.incoming);
incomingCallMediaPlayer.setLooping(true);
incomingCallMediaPlayer.start();
// 3. when I pick up, stop the player
incomingCallMediaPlayer.stop();
// I play R.raw.incoming if I'm being called.
// I play R.raw.outgoing when I'm calling.
// I understand if I'm the one calling from the number of participants in the "room" (this is a video call terminology) and bypassing in a variable through the intent
// For Outgoing Call
// 1. declare
private MediaPlayer callingMediaPlayer;
// 2. in onCreate, if I'm being called, ring the phone
callingMediaPlayer = MediaPlayer.create(this, R.raw.outgoing);
callingMediaPlayer.setLooping(true);
callingMediaPlayer.start();
// 3. when another "participant" (this is a video call terminology) joins the "room" I stop playing the sound
callingMediaPlayer.stop();
// to Vibrate, add the code with the media players and stop vibrate with media players
//
我正在尝试使用 Twilio 视频通话提供原生视频通话体验。场景如下:
- AAA 称 BBB。
- 人BBB没有打开应用,在后台或前台,应用处于被杀死状态,phone甚至可能被锁定
- 当 AAA 打来电话时,应用打开时会显示视频 ui 并带有接听按钮。就像在 WhatsApp、Google Duo、Skype...
我们有 FCM 并正在接收推送通知。试图在来电时打开视频通话接听按钮,而不是点击通知,就像在 Whatsapp 中一样,Google Duo...(在 Android phones 中)
我们试图在后台打开一个套接字服务 运行。当传入呼叫事件发送到套接字时,套接字将侦听传入呼叫并打开 VideoCallActivity。
这是我们最好的选择,但到目前为止还没有成功。您将如何实现此功能?
所以我们已经找到了这个解决方案(当通知到达时,将应用程序调到前台)并且我发布它虽然已经有一段时间了:
FCM通知(firebase云消息通知)只需要在通知中发送“data”即可。所以通知的 JSON 结构中没有 Notification 对象,只有数据。通过这种方式,通知由您应用的 FirebaseMessagingService.java class 处理。请详细阅读以下内容以了解如何处理 2 种 FCM 通知类型。 https://firebase.google.com/docs/cloud-messaging/android/receive https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages
在 FirebaseMessagingService.java class 中,启动带有 Intent 的视频通话 activity。不要忘记将此服务添加到 Manifest.xml
Intent intent = new Intent(this, VideoCallActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); getApplicationContext().startActivity(intent);
在 VideoCall activity 中,确保在 onCreate() 的开头有以下代码:
// These flags ensure that the activity can be launched when the screen is locked. Window window = getWindow(); window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); // to wake up the screen PowerManager pm = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE); PowerManager.WakeLock wakeLock = pm.newWakeLock((PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), "TAG"); wakeLock.acquire(); // to release the screen lock KeyguardManager keyguardManager = (KeyguardManager) getApplicationContext().getSystemService(Context.KEYGUARD_SERVICE); KeyguardManager.KeyguardLock keyguardLock = keyguardManager.newKeyguardLock("TAG"); keyguardLock.disableKeyguard();
使用适当的 intent-filter 将 VideoCallActivity 添加到 Manifest.xml:
<!-- Video Call --> <activity android:name=".ui.activities.video_call.VideoCallActivity" android:launchMode="singleTop" android:screenOrientation="portrait" android:theme="@style/AppTheme.NoActionBar"> <intent-filter> <!-- Note: these actions are notification actions --> <action android:name="VIDEO_CALLING" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>
可选: 要使 phone 响铃和振动:
// For Incoming Call
// 1. declare
private MediaPlayer incomingCallMediaPlayer;
// .2 in onCreate, if I'm the person that's calling, ring the phone
incomingCallMediaPlayer = MediaPlayer.create(this, R.raw.incoming);
incomingCallMediaPlayer.setLooping(true);
incomingCallMediaPlayer.start();
// 3. when I pick up, stop the player
incomingCallMediaPlayer.stop();
// I play R.raw.incoming if I'm being called.
// I play R.raw.outgoing when I'm calling.
// I understand if I'm the one calling from the number of participants in the "room" (this is a video call terminology) and bypassing in a variable through the intent
// For Outgoing Call
// 1. declare
private MediaPlayer callingMediaPlayer;
// 2. in onCreate, if I'm being called, ring the phone
callingMediaPlayer = MediaPlayer.create(this, R.raw.outgoing);
callingMediaPlayer.setLooping(true);
callingMediaPlayer.start();
// 3. when another "participant" (this is a video call terminology) joins the "room" I stop playing the sound
callingMediaPlayer.stop();
// to Vibrate, add the code with the media players and stop vibrate with media players
//