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.
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.
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")
}
}
}