如何读取设备令牌以允许使用Postman(Kotlin)的云消息传递



我正在尝试通过邮递员工作从服务器发送的简单通知服务,我认为我在活动中以正确的方式设置了所有内容。

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:" 在Postman中的通知中的定义中,或者说,我知道有必要插入设备的令牌,但我不知道如何获得它。

{
  "to":
    "Add your device token",
  "data": {
    "title": "Android4Dev",
    "message": "Learn Firebase PushNotification From Android4Dev Blog"
  }
}

解决了问题,我在主要活动中添加了:

FirebaseInstanceId.getInstance().instanceId.addOnSuccessListener(this@SplashActivity,
            OnSuccessListener<InstanceIdResult> { instanceIdResult ->
                val newToken = instanceIdResult.token
                Log.e("newToken", newToken)
            })

最新更新