BroadcastReceiver未接收本地广播

时间:2018-10-31 11:01:27

标签: android kotlin

我正在尝试扩展FCM服务,并在调用MainActivity后广播到onNewToken()。确实调用了onNewToken(),但没有调用onReceive()方法。据我所知,我不需要在清单中定义其他任何内容,因为这是本地广播。这些是我的课程:

MainActivity.kt

package com.reali.app.mymessagingapp

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v4.content.LocalBroadcastManager
import android.widget.TextView
import com.google.android.gms.tasks.OnCompleteListener
import com.reali.app.mymessagingapp.MyFirebaseMessagingService.Companion.TOKEN_REFRESHED_EVENT
import com.google.firebase.iid.FirebaseInstanceId
import com.google.firebase.iid.InstanceIdResult

class MainActivity : AppCompatActivity() {
    private lateinit var broadcastReceiver: BroadcastReceiver
    private lateinit var tvTitle: TextView
    private lateinit var tvToken: TextView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        tvTitle = findViewById(R.id.tvTitle)
        tvToken = findViewById(R.id.tvToken)

        broadcastReceiver = object : BroadcastReceiver() {
            override fun onReceive(context: Context, intent: Intent) {
                tvTitle.text = resources.getString(R.string.token_refreshed)
                refreshTokenText()
            }
        }

        LocalBroadcastManager.getInstance(this).registerReceiver(broadcastReceiver, IntentFilter(TOKEN_REFRESHED_EVENT))
        refreshTokenText()
    }

    private fun refreshTokenText() {
        FirebaseInstanceId.getInstance().instanceId.addOnCompleteListener(OnCompleteListener<InstanceIdResult> { task ->
            if (!task.isSuccessful) {
                return@OnCompleteListener
            }

            val token = task.result?.token
            tvToken.text = token
        })
    }

}

MyFirebaseMessagingService.kt

package com.reali.app.mymessagingapp

import android.content.Intent
import android.util.Log
import com.google.firebase.messaging.FirebaseMessagingService

class MyFirebaseMessagingService : FirebaseMessagingService()
{
    companion object {
        const val TOKEN_REFRESHED_EVENT = "token_refreshed_event"
    }

    override fun onNewToken(token: String?) {
        Log.d("MyFcmMessagingService", "Refreshed token: " + token!!)
        applicationContext.sendBroadcast(Intent(TOKEN_REFRESHED_EVENT))
    }
}

1 个答案:

答案 0 :(得分:3)

通过Context.sendBroadcast()发送的广播不是本地广播,这就是为什么您没有收到它们。

您还需要使用LocalBroadcastManager进行发送。

代替以下内容:

applicationContext.sendBroadcast(Intent(TOKEN_REFRESHED_EVENT))

您应该执行以下操作:

LocalBroadcastManager.getInstance(this)
        .sendBroadcast(Intent(TOKEN_REFRESHED_EVENT))

此外,为避免内存泄漏,请不要忘记在适当的时候注销接收器:

// most likely in onDestroy() (since the registration is in onCreate())
LocalBroadcastManager.getInstance(this)
        .unregisterReceiver(broadcastReceiver)