Android 我使用fcm向设备发送通知,但我可以';我没有收到通知
我想向其他设备发送通知。我向FCM服务器使用了HTTP post请求,方法类型为:post,URL:。当我使用postman进行测试时,我在设备中收到了通知,但当我通过改装实现FCM服务时,我收到了响应200,但收到了任何通知。有人能帮我吗Android 我使用fcm向设备发送通知,但我可以';我没有收到通知,android,kotlin,google-cloud-messaging,retrofit2,Android,Kotlin,Google Cloud Messaging,Retrofit2,我想向其他设备发送通知。我向FCM服务器使用了HTTP post请求,方法类型为:post,URL:。当我使用postman进行测试时,我在设备中收到了通知,但当我通过改装实现FCM服务时,我收到了响应200,但收到了任何通知。有人能帮我吗 //My ApiService @Headers("Authorization:key=******") @POST("https://fcm.googleapis.com/fcm/send") fun sendNot
//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)
}
//我的API服务
@标题(“授权:key=*******”)
@职位(”https://fcm.googleapis.com/fcm/send")
fun sendNotification(@Body:NotificationRequest):调用
val fm=FirebaseMessaging.getInstance()
var token=FirebaseInstanceId.getInstance().token
val datanoty=DataContent(“我的自定义值”,true)
val noty=通知内容(“标题”、“正文”、“ICU通知”)
val notificationRequest=notificationRequest(令牌!!,数据通知,通知)
//tt.sendRegistrationToServer(FirebaseInstanceId.getInstance().token)
更新client.instance.sendNotification(
通知请求)
.enqueue(对象:回调{
覆盖fun onResponse(调用:调用,响应:响应){
if(response.code()==200)
Toast.makeText(上下文“sendedYes”,Toast.LENGTH\u SHORT.show())
else if(response.code()==400)
Toast.makeText(上下文,“sendedNo”,Toast.LENGTH\u SHORT.show())
答复.正文()
}
覆盖失效时的乐趣(调用:调用,t:可丢弃){
}
//我的通知请求
类通知请求(
var to:String,
数据:数据内容,
通知:NotificationContent):可序列化
类DataContent(my_custom_key:String,my_custom_key 2:Boolean):可序列化
类NotificationContent(标题:字符串、正文:字符串、图标:字符串):可序列化
//myFirebaseMessagingService
覆盖onMessageReceived(remoteMessage:remoteMessage?){
if(remoteMessage?.notification!=null){
showNotification(remoteMessage.notification?标题、remoteMessage.notification?正文)
}
super.onMessageReceived(remoteMessage)
}
私人娱乐秀通知(标题:String?,正文:String?){
val channelId=“”
val notificationId=101
val manager=此?.getSystemService(Context.NOTIFICATION\u服务)作为NotificationManager
createNotificationChannel(管理器);
//val intent=FragmentTransactionUtilities.pushFragment(fragmentManager作为fragmentManager,BlankFragment2.newInstance(“,”))
val intent=intent(这是MapsActivity::class.java)
val pendingent=pendingent.getActivity(this,0,intent,pendingent.FLAG_UPDATE_CURRENT)
val soundUri=ringtonemager.getDefaultUri(ringtonemager.TYPE_通知)
var builder=这个?。让我们{
NotificationCompat.Builder(它是“默认频道”)
//.setSmallIcon(R.drawable.notification_图标)
.setContentText(“”)
.setSmallIcon(com.example.rahma.alerteaccidentapp.R.drawable.navigation\u empty\u图标)
.setContentTitle(“新增事故”)
//.setStyle(Notification.BigTextStyle().bigText(“无法容纳一行的更长文本…”))
.setStyle(NotificationCompat.BigTextStyle()
.bigText(“无法容纳一行的更长文本…”)
.setPriority(NotificationCompat.PRIORITY_默认值)
.setContentIntent(挂起内容)
.setAutoCancel(true)//单击后关闭自动通知
.setSound(soundUri)
.build()
}
如果(生成器!=null){
使用(this?.let{NotificationManagerCompat.from(it)}){
//notificationId是必须定义的每个通知的唯一int
经理?通知(1,建筑商)
}
}
}
private fun createNotificationChannel(notificationManager:notificationManager){
NotificationChannel.DEFAULT\u CHANNEL\u ID
if(Build.VERSION.SDK\u INT>=Build.VERSION\u code.O){
//仅在尚未创建频道时创建频道
if(notificationManager.getNotificationChannel(默认信道ID)==null){
notificationManager.createNotificationChannel(NotificationChannel(
默认频道ID、默认频道名称、NotificationManager.IMPORTANCE\u默认
));
}
}
}
重写fun onNewToken(令牌:字符串?){
super.onNewToken(代币)
Toast.makeText(这个,“刷新的标记:+标记,Toast.LENGTH\u SHORT.show())
sendRegistrationToServer(令牌)
}
尝试先打印邮件。不要那样公开api密钥。由于通知中包含数据内容,您需要编写代码来显示您的通知。我如何显示我的通知?onMessageReceived无法显示我的通知?请检查在您的应用打开时是否收到通知。我认为通知将当应用程序关闭时不显示。请尝试在应用程序打开时发送通知,并让我知道。请尝试先打印您的消息。不要这样公开api密钥。由于通知中包含数据内容,您需要编写代码来显示您的通知。我如何显示我的通知?onMessageReceived无法显示我的通知?che检查是否在应用程序打开时收到通知。我认为在应用程序关闭时不会显示通知。请尝试在应用程序打开时发送通知并通知我