我想向其他设备发送通知。我向FCM服务器使用了HTTP发布请求,其方法类型为: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)
}