首先,我已经对其进行搜索并找到了以下资源: How to send device to device messages using Firebase Cloud Messaging?我使用了改造方案,该资源也https://firebase.google.com/docs/cloud-messaging/migrate-v1,所以我对其进行了一些更改,这是我的代码
class NotificationActivity : AppCompatActivity() {
var token: String?= null
companion object {
val channelId = "notificationChannelID"
val channelName = "notificationChannelName"
val channelDesc = "notificationChannelDesc"
}
var notificationService:NotificationService?= null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_notification)
notificationService = ApiClient().getClient()?.create(NotificationService::class.java)
if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
val notificationChannel = NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_DEFAULT)
notificationChannel.description = channelDesc
val notificationManager = getSystemService(NotificationManager::class.java)
notificationManager.createNotificationChannel(notificationChannel)
}
getFirebaseToken()
notificationButton.setOnClickListener { createNotification() }
}
private fun createNotification() {
val notification = Notification("notification", "it's work")
val collapseKey = ""
val data = Data("Hello2", "title2", "key1", "key2")
val message = Message(token!!, collapseKey, notification, data)
val call = notificationService?.sendMessage("Bearer key", message)
call?.enqueue(object: retrofit2.Callback<Message> {
override fun onFailure(call: Call<Message>, t: Throwable) {
Toast.makeText(this@NotificationActivity, t.message, Toast.LENGTH_LONG).show()
}
override fun onResponse(call: Call<Message>, response: Response<Message>) {
}
})
}
private fun getFirebaseToken() {
FirebaseInstanceId.getInstance().instanceId.addOnCompleteListener { task ->
if (task.isSuccessful) {
token = task.result?.token
notificationButton.visibility = View.VISIBLE
}
}
}
我也有firebase消息服务,以防在前台应用程序发送通知时
class FirebaseMessagesService: FirebaseMessagingService() {
override fun onMessageReceived(remoteMessage: RemoteMessage?) {
super.onMessageReceived(remoteMessage)
if (remoteMessage?.notification != null){
val title = remoteMessage.notification?.title
val message = remoteMessage.notification?.body
NotificationHelper.displayNotification(applicationContext, title, message)
}
}
}
这是我的notificationhelper显示通知代码
companion object {
public fun displayNotification(context: Context, notificationText: String?, notificationTitle: String?) {
val notifiationIntent = Intent(context, NotificationActivity::class.java)
val notificationPendingIntent = PendingIntent.getActivity(
context,
100,
notifiationIntent,
PendingIntent.FLAG_CANCEL_CURRENT)
val notificationBuilder = NotificationCompat.Builder(context, NotificationActivity.channelId)
.setSmallIcon(R.drawable.whatsapp)
.setColor(Color.GREEN)
.setContentTitle(notificationTitle)
.setContentText(notificationText)
.setContentIntent(notificationPendingIntent)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
val notificationManagerCompact = NotificationManagerCompat.from(context)
notificationManagerCompact.notify(1, notificationBuilder.build())
}
}
这是我的改装API
@POST("/messages:send")
fun sendMessage(@Header("Authorization") token: String, @Body message: Message): Call<Message>
我从firebase->设置-> serverKey获得了令牌,当我单击按钮时,什么也没有发生,也没有显示通知!
我在firebase上看到了一些文档,但是始终有服务器代码node.js和google凭据,所以我要再次澄清我的问题,是否可以通过无服务器应用将通知从android设备发送到android设备>