在MainActivity外部颤振调用MethodChannel和EventChannel

时间:2020-04-25 02:34:31

标签: android flutter dart native

我进行了很多搜索,但未能解决我的问题。我想通过 MethodChannel EventChannel 将数据从本机Android发送到Flutter。我编写了一个 NotificationGrabber 类,该类扩展 NotificationListenerService ,以便在Android上捕获通知。覆盖 onNotificationPosted 方法,我成功捕获了传入的通知。现在,我尝试使用 MethodChannel EventChannel 将这些通知发送到Flutter,但是这些构造函数需要 BinaryMessenger 。我从各种使用 flutterView 作为BinaryMessenger的教程中看到(如果我错了,请纠正我)。由于MainActivity中没有 onNotificationsPosted 方法,我该如何从NotificationGrabber类中做到这一点?如何使用另一个正确的BinaryMessenger?谢谢大家!

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 );

    }
    .
    .
    .
    .
}

0 个答案:

没有答案