Code: Select all
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)
}
}
Code: Select all
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")
}
}
}
Wenn meine Das Telefon erhält eine Benachrichtigung. Ich erhalte die Protokollmeldung „Übertragung wird gesendet“.
Alles funktioniert also, außer dass die Übertragung nicht von WatchCompanionDeviceService empfangen wird, ich nie Holen Sie sich das Broadcast-Empfangen-Protokoll Nachricht.