Android 主活动外部的颤振调用方法通道和事件通道
我找了很多,但没能解决我的问题。我想通过MethodChannel或EventChannel将数据从本机Android发送到Flatter。我编写了一个类NotificationGrabber,它扩展了NotificationListenerService,以便在Android上捕获通知。覆盖onNotificationPosted方法后,我成功捕获了传入通知。现在我正试图使用MethodChannel或EventChannel将这些通知发送到flifter,但这些构造函数需要一个二进制Messenger。我从使用flatterview作为BinaryMessenger的各种教程中看到(如果我错了,请纠正我)。由于onNotificationsPosted方法不在MainActivity中,我如何从NotificationGrabber类中执行此操作?如何使用另一个正确的BinaryMessenger?谢谢大家!Android 主活动外部的颤振调用方法通道和事件通道,android,flutter,dart,native,Android,Flutter,Dart,Native,我找了很多,但没能解决我的问题。我想通过MethodChannel或EventChannel将数据从本机Android发送到Flatter。我编写了一个类NotificationGrabber,它扩展了NotificationListenerService,以便在Android上捕获通知。覆盖onNotificationPosted方法后,我成功捕获了传入通知。现在我正试图使用MethodChannel或EventChannel将这些通知发送到flifter,但这些构造函数需要一个二进制Mess
public class NotificationGrabber extends NotificationListenerService {
private static final String CHANNEL = "mynotifications/getnotifications";
private String TAG = this .getClass().getSimpleName() ;
@Override
public void onNotificationPosted (StatusBarNotification sbn) {
Log. d ( TAG , "onNotificationPosted" ) ;
sendNotificationsToFlutter(sbn);
}
private void sendNotificationsToFlutter(StatusBarNotification sbn) {
Gson tempGson = new Gson();
String jsonNotifications = tempGson.toJson(sbn);
MethodChannel channel = new MethodChannel( <What could I put here?>, CHANNEL);
channel.invokeMethod("returnedNotification", jsonNotifications );
}
.
.
.
.
}
公共类NotificationGrabber扩展了NotificationListenerService{
私有静态最终字符串通道=“mynotifications/getnotifications”;
私有字符串标记=this.getClass().getSimpleName();
@凌驾
公告发布时的公共作废(状态公告sbn){
Log.d(标签“onNotificationPosted”);
向颤振发送通知(sbn);
}
私有无效发送通知至颤振(状态通知sbn){
Gson tempGson=新的Gson();
字符串jsonNotifications=tempGson.toJson(sbn);
MethodChannel=新的MethodChannel(,channel);
invokeMethod(“returnedNotification”,jsonNotifications);
}
.
.
.
.
}