Я хочу отправить уведомление на другое устройство. Я использовал HTTP-запрос на отправку на сервер FCM с типом метода: POST и URL: https://fcm.googleapis.com/fcm/send. Когда я проверял его с почтальоном, я получил уведомление вмое устройство, но когда я внедрил службу FCM с модернизацией, я получил ответ 200, но все уведомления были получены. Может ли кто-нибудь помочь мне, пожалуйста?
//My ApiService
@Headers("Authorization:key=******")
@POST("https://fcm.googleapis.com/fcm/send")
fun sendNotification(@Body body:NotificationRequest):Call<ResponseBody>
val fm=FirebaseMessaging.getInstance()
var token=FirebaseInstanceId.getInstance().token
val datanoty=DataContent("my_custom_value",true)
val noty=NotificationContent("title","body text","ic_notification")
val notificationRequest=NotificationRequest(token!!,datanoty,noty)
// tt.sendRegistrationToServer(FirebaseInstanceId.getInstance().token)
RetrofitClient.instance.sendNotification(
notificationRequest)
.enqueue(object : Callback<ResponseBody> {
override fun onResponse(call: Call<ResponseBody>, response: Response<ResponseBody>) {
if( response.code()==200)
Toast.makeText(context,"sendedYes",Toast.LENGTH_SHORT).show()
else if(response.code()==400)
Toast.makeText(context,"sendedNo",Toast.LENGTH_SHORT).show()
response.body()
}
override fun onFailure(call: Call<ResponseBody>, t: Throwable) {
}
//My notification Request
class NotificationRequest(
var to:String,
data:DataContent,
notification:NotificationContent ):Serializable
class DataContent(my_custom_key:String,my_custom_key2:Boolean):Serializable
class NotificationContent(title:String,body:String,icon:String):Serializable
//myFirebaseMessagingService
override fun onMessageReceived(remoteMessage: RemoteMessage?) {
if (remoteMessage?.notification != null) {
showNotification(remoteMessage.notification?.title, remoteMessage.notification?.body)
}
super.onMessageReceived(remoteMessage)
}
private fun showNotification(title: String?, body: String?) {
val channelId=""
val notificationId=101
val manager = this?.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
createNotificationChannel(manager);
// val intent=FragmentTransactionUtilities.pushFragment(fragmentManager as FragmentManager,BlankFragment2.newInstance("",""))
val intent= Intent(this, MapsActivity::class.java)
val pendingIntent= PendingIntent.getActivity(this,0,intent, PendingIntent.FLAG_UPDATE_CURRENT)
val soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
var builder = this?.let {
NotificationCompat.Builder(it,"default_channel")
//.setSmallIcon(R.drawable.notification_icon)
.setContentText("")
.setSmallIcon(com.example.rahma.alerteaccidentapp.R.drawable.navigation_empty_icon)
.setContentTitle("New Accident Added")
//.setStyle(Notification.BigTextStyle().bigText("Much longer text that cannot fit one line..."))
.setStyle(NotificationCompat.BigTextStyle()
.bigText("Much longer text that cannot fit one line..."))
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(pendingIntent)
.setAutoCancel(true)//close auto noty after click
.setSound(soundUri)
.build()
}
if (builder != null) {
with(this?.let { NotificationManagerCompat.from(it) }) {
// notificationId is a unique int for each notification that you must define
manager?.notify(1, builder)
}
}
}
private fun createNotificationChannel(notificationManager: NotificationManager) {
NotificationChannel.DEFAULT_CHANNEL_ID
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//Create channel only if it is not already created
if (notificationManager.getNotificationChannel(DEFAULT_CHANNEL_ID) == null) {
notificationManager.createNotificationChannel( NotificationChannel(
DEFAULT_CHANNEL_ID, DEFAULT_CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT
));
}
}
}
override fun onNewToken(token: String?) {
super.onNewToken(token)
Toast.makeText(this, "Refreshed token: " + token,Toast.LENGTH_SHORT).show()
sendRegistrationToServer(token)
}