-
Notifications
You must be signed in to change notification settings - Fork 32
07.Notification and Message
Use FcMessageFeature.sendNotification to send a message notification to the device.
You can send a variety of different message notifications, such as QQ, WeChat, Facebook, etc. But at first, you should use FcDeviceInfo.isSupportNotification to ensure this type is supported. And ensure that the type is enabled in FcNotificationConfig.
The configuration items in FcNotificationConfig are not one-to-one corresponding to the message types in FcNotificationType. This is because there are three types of notifications for telephony types: incoming calls, answering and hanging up.
Because Android does not support ANCS, so these notification messages need to be captured by yourself. Like monitor phone rings and hangs up, listen to the SMS. Other third-party notifications such as QQ, WeChat messages, etc., consider using NotificationListenerService to capture.
sample project:
MyTelephonyControl.kt
When FcNotificationConfig.Flag.TELEPHONY is enabled, you can send telephony notification to device. There are 4 types of telephony notifications:
FcNotificationType.TELEPHONY_INCOMINGFcNotificationType.TELEPHONY_ANSWERED-
FcNotificationType.TELEPHONY_REJECTED:Whether it is an active hang up or a missed call, it needs to be sent. -
FcNotificationType.TELEPHONY_MISSED:Need send afterTELEPHONY_REJECTEDif it is a missed call.
Usually, we use TelephonyManager and PhoneStateListener to monitor the telephony status.
In addition, users can hang up the telephony on the device and need to use FcMessageFeature.observer Message to listen to FcMessageType.TELEPHONY_ HANG_UP and FcMessageType.TELEPHONY_HANG_UP_SMS message to handle hang ups from the device. Like this:
private val messageDisposable = connector.messageFeature().observerMessage().subscribe {
if (it.type == FcMessageType.TELEPHONY_HANG_UP) {//hang up
doHangUp()
} else if (it.type == FcMessageType.TELEPHONY_HANG_UP_SMS) {//hang up and send a sms
val sms = it.data as? String
if (!sms.isNullOrEmpty()) {
doHangUpSms()
}
}
}
In addition, if you listen to FcMessageType.MEDIA_SILENT_MODE message and mute the device, you shoule exit the mute mode when the telephony enters an idle state. Such as exit mute mode in PhoneStateListener.onCallStateChanged when state is TelephonyManager.CALL_STATE_IDLE.
We strongly recommend using FcBuiltInFeatures.telephonyControl or the util class AbsTelephonyControl to implement this feature. You only need to handle permissions in a few places.
Use FcBuiltInFeatures.telephonyControl:
//1. enable FcBuiltInFeatures.telephonyControl
FcSDK.Builder(context).setBuiltInFeatures(
FcBuiltInFeatures(
telephonyControl= true,
)
)
//2. Check after request runtime permissions
requestPermission(fragment, getTelephony(), descriptors, listener = {
if (hasPermissions(context, listOf(Manifest.permission.READ_PHONE_STATE))) {
fcSDK.connector.telephonyControlPhoneStatePermission()
}
})
//3. Check after app enter foreground
ProcessLifecycleOwner.get().lifecycle.addObserver(object : DefaultLifecycleObserver {
override fun onStart(owner: LifecycleOwner) {
fcSDK.connector.telephonyControlPhoneStatePermission()
}
override fun onStop(owner: LifecycleOwner) {
}
})
Use AbsTelephonyControl:
//1. extend AbsTelephonyControl and AbsPhoneStateListener
class MyTelephonyControl(
context: Context,
connector: FcConnector,
factory: PhoneStateListenerFactory<MyPhoneStateListener>,
) : AbsTelephonyControl<MyPhoneStateListener>(context, connector, factory)
class MyPhoneStateListener(
context: Context,
connector: FcConnector,
) : AbsPhoneStateListener(context, connector) {
override fun isTelephonyEnabled(context: Context): Boolean {
return connector.configFeature().getNotificationConfig().isFlagEnabled(FcNotificationConfig.Flag.TELEPHONY)
}
}
//2. Init and hold reference in Application
var myTelephonyControl: MyTelephonyControl? = null
myTelephonyControl= MyTelephonyControl(this, fcSDK.connector, object : PhoneStateListenerFactory<MyPhoneStateListener> {
override fun createInstance(context: Context, connector: FcConnector): MyPhoneStateListener {
return MyPhoneStateListener(context, connector)
}
})
//3. Check after request runtime permissions
requestPermission(fragment, getTelephony(), descriptors, listener = {
if (hasPermissions(context, listOf(Manifest.permission.READ_PHONE_STATE))) {
myTelephonyControl.checkInitialize()
}
})
//4. Check after app enter foreground
ProcessLifecycleOwner.get().lifecycle.addObserver(object : DefaultLifecycleObserver {
override fun onStart(owner: LifecycleOwner) {
myTelephonyControl.checkInitialize()
}
override fun onStop(owner: LifecycleOwner) {
}
})
The device may send various messages to the phone, and use FcMessageFeature.observerMessage to listen for these messages.
The message type define in FcMessageType
sample project: Refer to
FindPhoneManager.kt
When receiving FcMessageType.FINE_PHONE type message indicating that the device is finding for the phone.
Usually, after receiving this message on your phone, you need to make a ring or vibration to respond to the device's finding. And when you start responding, you need to send FcMessageFeature.replayFindPhone.
After the finding starts, both parties can actively stop the finding process. Listen FcMessageType.STOP_FINE_PHONE message to stop phone finding, and send FcMessageFeature.stopFindPhone to stop device finding.
sample project: Refer to
OtherFeaturesFragment.kt
Use FcMessageFeature.findDevice to find device.
sample project: Refer to
CameraActivity.kt
Use FcMessageFeature.observerMessage to listen the camera message:
- FcMessageType.CAMERA_WAKE_UP: Launch your camera activity
- FcMessageType.CAMERA_EXIT: Exit your camera activity
- FcMessageType.CAMERA_TAKE_PHOTO: Take photo in your camera activity.
Use FcMessageFeature.setCameraStatus to send you camera status to device. Such as setCameraStatus(true) on activity resume, setCameraStatus(false) on activity pause.
Use FcMessageFeature.observerMessage to listen for the following messages to for media control
- FcMessageType.MEDIA_PLAY_PAUSE
- FcMessageType.MEDIA_NEXT
- FcMessageType.MEDIA_PREVIOUS
- FcMessageType.MEDIA_VOLUME_UP
- FcMessageType.MEDIA_VOLUME_DOWN
- FcMessageType.MEDIA_SILENT_MODE
For example, when you receive FcMessageType.MEDIA_VOLUME_UP message, use AudioManager.adjustVolume to increase the ringer volume.
You can use FcBuiltInFeatures.mediaControl without having to handle the messages yourself. See also Built-in features
When receiving FcMessageType.MUSIC_STATE and FcMessageType.MUSIC_INFO messages, you should use FcMessageFeature.setMusicState and FcMessageFeature.setMusicInfo to send music state/info to device. Usually, MediaSessionManager is used to obtain the current music info and status of the phone.
You can use FcBuiltInFeatures.musicControl without having to handle the message yourself. See also Built-in features.
This feature relies on NotificationListenerService, you need to use FcConnector.musicControlNotificationListenerService to pass in this instance in your NotificationListenerService.Like this:
class MyNotificationListenerService : NotificationListenerService() {
override fun onCreate() {
super.onCreate()
getFcSDK(this).connector.musicControlNotificationListenerService(null)
}
override fun onListenerConnected() {
super.onListenerConnected()
getFcSDK(this).connector.musicControlNotificationListenerService(this)
}
override fun onListenerDisconnected() {
super.onListenerDisconnected()
getFcSDK(this).connector.musicControlNotificationListenerService(null)
}
}
If you extend the FitCloud SDK util class AbsNotificationListenerService, you don't need to call musicControlNotificationListenerService.