android - Not receiving events using a BroadcastReceiver - Stack Overflow

admin2025-04-30  0

I have two classes in my android project:

class NotificationEventListener: NotificationListenerService() {

    override fun onNotificationPosted(sbn: StatusBarNotification?) {
        super.onNotificationPosted(sbn)
        
        Log.i("NotificationEventListener", "Sending broadcast")
        sendBroadcast(
            Intent("com.github.example.TEST")
        )
    }

    override fun onNotificationRemoved(sbn: StatusBarNotification?) {
        super.onNotificationRemoved(sbn)
    }
}

And:

class WatchCompanionDeviceService : CompanionDeviceService() {
    @SuppressLint("MissingPermission")
    override fun onDeviceAppeared(associationInfo: AssociationInfo) {
        super.onDeviceAppeared(associationInfo)
        // Unrelated bluetooth code
    }

    override fun onDeviceDisappeared(associationInfo: AssociationInfo) {
        super.onDeviceDisappeared(associationInfo)
        // Unrelated bluetooth code
    }



    private val notificationReceiver = NotificationReceiver()

    override fun onCreate() {
        super.onCreate()
        Log.i("WatchCompanionDeviceService", "CompanionDeviceService started")

        // Notification Receiver
        val notificationFilter = IntentFilter("com.github.example.TEST")
        registerReceiver(notificationReceiver, notificationFilter, RECEIVER_NOT_EXPORTED)
        Log.i("WatchCompanionDeviceService", "notificationFilter started")
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.i("WatchCompanionDeviceService", "CompanionDeviceService stopped")

        // Notification Receiver
        unregisterReceiver(notificationReceiver)
    }


    class NotificationReceiver: BroadcastReceiver() {
        override fun onReceive(context: Context?, intent: Intent?) {
            Log.i("WatchCompanionDeviceService", "Broadcast received")
        }
    }
}

When I start the app and connect the bluetooth device I get the CompanionDeviceService started and notificationFilter started log messages.
When my phone gets a notification I get the Sending broadcast log message.
So, every part of it is working except that the broadcast isn't being received by WatchCompanionDeviceService, I never get the Broadcast received log message.

I have two classes in my android project:

class NotificationEventListener: NotificationListenerService() {

    override fun onNotificationPosted(sbn: StatusBarNotification?) {
        super.onNotificationPosted(sbn)
        
        Log.i("NotificationEventListener", "Sending broadcast")
        sendBroadcast(
            Intent("com.github.example.TEST")
        )
    }

    override fun onNotificationRemoved(sbn: StatusBarNotification?) {
        super.onNotificationRemoved(sbn)
    }
}

And:

class WatchCompanionDeviceService : CompanionDeviceService() {
    @SuppressLint("MissingPermission")
    override fun onDeviceAppeared(associationInfo: AssociationInfo) {
        super.onDeviceAppeared(associationInfo)
        // Unrelated bluetooth code
    }

    override fun onDeviceDisappeared(associationInfo: AssociationInfo) {
        super.onDeviceDisappeared(associationInfo)
        // Unrelated bluetooth code
    }



    private val notificationReceiver = NotificationReceiver()

    override fun onCreate() {
        super.onCreate()
        Log.i("WatchCompanionDeviceService", "CompanionDeviceService started")

        // Notification Receiver
        val notificationFilter = IntentFilter("com.github.example.TEST")
        registerReceiver(notificationReceiver, notificationFilter, RECEIVER_NOT_EXPORTED)
        Log.i("WatchCompanionDeviceService", "notificationFilter started")
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.i("WatchCompanionDeviceService", "CompanionDeviceService stopped")

        // Notification Receiver
        unregisterReceiver(notificationReceiver)
    }


    class NotificationReceiver: BroadcastReceiver() {
        override fun onReceive(context: Context?, intent: Intent?) {
            Log.i("WatchCompanionDeviceService", "Broadcast received")
        }
    }
}

When I start the app and connect the bluetooth device I get the CompanionDeviceService started and notificationFilter started log messages.
When my phone gets a notification I get the Sending broadcast log message.
So, every part of it is working except that the broadcast isn't being received by WatchCompanionDeviceService, I never get the Broadcast received log message.

Share Improve this question asked Jan 5 at 0:44 doominabox1doominabox1 4586 silver badges16 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

You may need to specify a package name in your broadcast intent like:

sendBroadcast(
  Intent("com.github.example.TEST").apply {
    setPackage(packageName) // or "com.github.example"
  }
)

I just created sample as you have and I received event correcty in both Activity and WatchCompanionDeviceService. Please make sure that you start service correctly.

  • Did you provide all necessary permissions?
  • Did you registered services in manifest?

AndroidManifest.xml:

        <service
            android:name=".NotificationEventListener"
            android:exported="false"
            android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
            <intent-filter>
                <action android:name="android.service.notification.NotificationListenerService" />
            </intent-filter>
        </service>
        <service
            android:name=".WatchCompanionDeviceService"
            android:exported="true"
            android:permission="android.permission.BIND_COMPANION_DEVICE_SERVICE">
            <intent-filter>
                <action android:name="android.companion.CompanionDeviceService" />
            </intent-filter>
        </service>

Activity.onCreate(): (in my case)

startService(Intent(this, WatchCompanionDeviceService::class.java))
startService(Intent(this, NotificationEventListener::class.java))

NotificationEventListener:

class NotificationEventListener : NotificationListenerService() {

    override fun onCreate() {
        super.onCreate()
        Log.i("NotificationEventListener", "Service started")

        // Wait to simulate notification received after onCreate.
        SystemClock.sleep(1000L)
        sendBroadcast(
            Intent("com.github.example.TEST")
        )
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.i("NotificationEventListener", "Service destroyed")
    }

    override fun onNotificationPosted(sbn: StatusBarNotification?) {
        super.onNotificationPosted(sbn)

        Log.i("NotificationEventListener", "Sending broadcast")
        sendBroadcast(
            Intent("com.github.example.TEST")
        )
    }

    override fun onNotificationRemoved(sbn: StatusBarNotification?) {
        super.onNotificationRemoved(sbn)
    }
}

WatchCompanionDeviceService:

class WatchCompanionDeviceService : CompanionDeviceService() {
    
    @SuppressLint("MissingPermission")
    override fun onDeviceAppeared(associationInfo: AssociationInfo) {
        super.onDeviceAppeared(associationInfo)
        // Unrelated bluetooth code
    }

    override fun onDeviceDisappeared(associationInfo: AssociationInfo) {
        super.onDeviceDisappeared(associationInfo)
        // Unrelated bluetooth code
    }
    

    private val notificationReceiver = NotificationReceiver()

    override fun onCreate() {
        super.onCreate()
        Log.i("WatchCompanionDeviceService", "CompanionDeviceService started")

        // Notification Receiver
        val notificationFilter = IntentFilter("com.github.example.TEST")
        registerReceiver(notificationReceiver, notificationFilter, RECEIVER_NOT_EXPORTED)
        Log.i("WatchCompanionDeviceService", "notificationFilter started")
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.i("WatchCompanionDeviceService", "CompanionDeviceService stopped")

        // Notification Receiver
        unregisterReceiver(notificationReceiver)
    }


    class NotificationReceiver: BroadcastReceiver() {
        override fun onReceive(context: Context?, intent: Intent?) {
            Log.i("WatchCompanionDeviceService", "Broadcast received")
        }
    }
}
转载请注明原文地址:http://anycun.com/QandA/1746023172a91480.html