Я пытаюсь сделать простой сервис уведомлений, отправляемый с сервера через почтальона, я думаю, что все правильно настроил в действиях.
FirebaseNotificationActivity:
class FirebasePushNotificationActivity : BaseActivity<FirebasePushNotificationContract.FirebasePushNotificationView, FirebasePushNotificationContract.FirebasePushNotificationPresenter>(),
FirebasePushNotificationContract.FirebasePushNotificationView {
private val TAG = "MyFirebaseToken"
override val layoutResId: Int
get() = R.layout.activity_firebase_push_notification
override fun createPresenter(): FirebasePushNotificationContract.FirebasePushNotificationPresenter {
return FirebasePushNotificationContract.FirebasePushNotificationPresenter()
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_firebase_push_notification)
initView()
}
private fun initView() {
//This method will use for fetching Token
Thread(Runnable {
try {
Log.i(TAG, FirebaseInstanceId.getInstance().getToken(getString(R.string.SENDER_ID), "FCM"))
} catch (e: IOException) {
e.printStackTrace()
}
}).start()
}
}
MyFirebaseMessagingService
class MyFirebaseMessagingService: FirebaseMessagingService() {
private val TAG = "MyFirebaseToken"
private lateinit var notificationManager: NotificationManager
private val ADMIN_CHANNEL_ID = "Android4Dev"
override fun onNewToken(token: String?) {
super.onNewToken(token)
Log.i(TAG, token)
}
override fun onMessageReceived(remoteMessage: RemoteMessage?) {
super.onMessageReceived(remoteMessage)
remoteMessage?.let { message ->
Log.i(TAG, message.getData().get("message"))
notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
//Setting up Notification channels for android O and above
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
setupNotificationChannels()
}
val notificationId = Random().nextInt(60000)
val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
val notificationBuilder = NotificationCompat.Builder(this, ADMIN_CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_launcher) //a resource for your custom small icon
.setContentTitle(message.data["title"]) //the "title" value you sent in your notification
.setContentText(message.data["message"]) //ditto
.setAutoCancel(true) //dismisses the notification on click
.setSound(defaultSoundUri)
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.notify(notificationId /* ID of notification */, notificationBuilder.build())
}
}
@RequiresApi(api = Build.VERSION_CODES.O)
private fun setupNotificationChannels() {
val adminChannelName = getString(R.string.notifications_admin_channel_name)
val adminChannelDescription = getString(R.string.notifications_admin_channel_description)
val adminChannel: NotificationChannel
adminChannel = NotificationChannel(ADMIN_CHANNEL_ID, adminChannelName, NotificationManager.IMPORTANCE_LOW)
adminChannel.description = adminChannelDescription
adminChannel.enableLights(true)
adminChannel.lightColor = Color.RED
adminChannel.enableVibration(true)
notificationManager.createNotificationChannel(adminChannel)
}
}
Проблема в том, что следуя руководству, я плохо себя чувствуюпонял, что вставить в поле "to:" в рамках определения уведомления в Почтальоне, точнее, я понимаю, что необходимо вставить токен устройства, но я не знаю, как получитьэто.
{
"to":
"Add your device token",
"data": {
"title": "Android4Dev",
"message": "Learn Firebase PushNotification From Android4Dev Blog"
}
}