【问题标题】:I got some errors when I'm running native code with flutter使用颤振运行本机代码时出现一些错误
【发布时间】:2019-07-02 19:51:03
【问题描述】:

大家好,当我想在应用程序滑开时接收通知时,我使用了平台通道,但是在运行代码时出现了一些错误,我没有任何 java 背景,这是错误我面对的是java代码,出现的错误:

E:\flutter\noti\android\app\src\main\java\com\example\notiapp\NotificationExtender.java:24: 错误:';'预期的 受保护的布尔 onNotificationProcessing(OSNotificationReceivedResult receivedResult) { ^ E:\flutter\noti\android\app\src\main\java\com\example\notiapp\NotificationExtender.java:24: 错误:';'预期的 受保护的布尔 onNotificationProcessing(OSNotificationReceivedResult receivedResult) { ^ 2 个错误

FAILURE:构建失败并出现异常。

还有一个问题: 这样我写的naitve代码(Java代码)是否正确解决后台通知请指导我,我是初学者

看下面的飞镖代码:

class WebviewUi extends StatefulWidget {

  @override
  _WebviewState createState() => _WebviewState();
}

class _WebviewState extends State<WebviewUi> {

  static const platform = const MethodChannel('example.app.com/notifications');


  Future<void> _receiveNotifications() async {
    try {
      platform.invokeMethod('notification');
    } on PlatformException catch (e) {
      print('${e.message}');
    }


  }


  @override
  initState()  {
    initPlatformState();
    _receiveNotifications();
    super.initState();

    );


  }

  Future<void> initPlatformState() async {
    if (!mounted) return;

    OneSignal.shared.setLogLevel(OSLogLevel.verbose, OSLogLevel.none);

    OneSignal.shared.setRequiresUserPrivacyConsent(true);
    OneSignal.shared.consentGranted(true);


    var settings = {
      OSiOSSettings.autoPrompt: false,
      OSiOSSettings.promptBeforeOpeningPushUrl: true
    };



    OneSignal.shared.setNotificationReceivedHandler((notification) {
      this.setState(() {
        print('Notifiaction received');
      });
    });

    OneSignal.shared
        .setNotificationOpenedHandler((OSNotificationOpenedResult result) {
      this.setState(() {
        nUrl = result.notification.payload.additionalData['url'].toString();
      });
      Navigator.of(context).pushReplacement(
          MaterialPageRoute(builder: (context) =>  WebNotification(nUrl)));

    });
    // NOTE: Replace with your own app ID from https://www.onesignal.com
    await OneSignal.shared
        .init("xxxx-xxxx-xxxx-xxxx-xxxx", iOSSettings: settings);

    OneSignal.shared
        .setInFocusDisplayType(OSNotificationDisplayType.notification);
    OneSignal.shared.inFocusDisplayType();

  }



  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: PreferredSize(
        preferredSize: Size.fromHeight(0),
        child: AppBar(
        ),

      ),

      body:   Text("Hello")

    );
  }

}

java NotificationExtender 类:

import android.support.v4.app.NotificationCompat;
import com.onesignal.OSNotificationPayload;
import com.onesignal.NotificationExtenderService;
import java.math.BigInteger;

public class NotificationExtender extends NotificationExtenderService {
    private static NotificationExtender notificationExtender;
    public NotificationExtender(){
        notificationExtender = this;
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        GeneratedPluginRegistrant.registerWith(this);

        new MethodChannel(getFlutterView(), CHANNEL).setMethodCallHandler(
                new MethodCallHandler(){
                    @Override
                    public void onMethodCall(MethodCall call, Result result) {
                        if (call.method.equals("notification")) {

                            @Override
                            protected boolean onNotificationProcessing(OSNotificationReceivedResult receivedResult) {
                                OverrideSettings overrideSettings = new OverrideSettings();
                                overrideSettings.extender = new NotificationCompat.Extender() {
                                    @Override
                                    public NotificationCompat.Builder extend(NotificationCompat.Builder builder) {
                                        // Sets the background notification color to Green on Android 5.0+ devices.
                                        return builder.setColor(new BigInteger("FF00FF00", 16).intValue());
                                    }
                                };

                                OSNotificationDisplayedResult displayedResult = displayNotification(overrideSettings);
                                Log.d("OneSignalExample", "Notification displayed with id: " + displayedResult.androidNotificationId);

                                return true;
                            }

                        } else {
                            result.notImplemented();
                        }
                    }
                });
    }



    public synchronized NotificationExtender getInctance()
    {
        return notificationExtender ;
    }
}

Java MainActivity 类:

package com.noti.notieapp;
import java.math.BigInteger;

import android.os.Bundle;
import io.flutter.app.FlutterActivity;
import io.flutter.plugins.GeneratedPluginRegistrant;

public class MainActivity extends FlutterActivity {
    NotificationExtender notificationExtender;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
      notificationExtender = NotificationExtender.getInstance();
    GeneratedPluginRegistrant.registerWith(this);

  }

}

【问题讨论】:

    标签: java android flutter dart onesignal


    【解决方案1】:

    这看起来不正确:

    @Override
    public void onMethodCall(MethodCall call, Result result) {
       if (call.method.equals("notification")) {
    
           // Overriding onNotificationProcessing(..) is not allowed here:
    
           @Override
           protected boolean onNotificationProcessing(OSNotificationReceivedResult receivedResult) {
               // ....
           }                            
    };
    

    您在另一个方法中覆盖了一个方法,这在 Java 中不起作用。

    我不知道您在那里使用的库的具体细节,但从语法的角度来看,应该调用 onNotificationProcessing(..) 或者应该在其他地方覆盖该方法。

    【讨论】:

    • 我应该怎么做而不是那个兄弟。
    • 我试图从该方法中覆盖该方法,然后我在 onMethodCall 中调用它,但仍然是同样的错误
    猜你喜欢
    • 2020-12-27
    • 2021-12-24
    • 2019-03-31
    • 2021-10-31
    • 2020-03-15
    • 2022-11-04
    • 1970-01-01
    • 2021-03-18
    • 2022-01-09
    相关资源
    最近更新 更多