大家好,当我试图在应用程序滑走时接收通知时,我使用了平台 channel ,但是当我运行代码时出现了一些错误,我在 java 中没有任何背景,并且错误是我在 java 代码中遇到了出现的错误:
E:\flutter\noti\android\app\src\main\java\com\example\notiapp\NotificationExtender.java:24: error: ';' expected protected boolean onNotificationProcessing(OSNotificationReceivedResult receivedResult) { ^ E:\flutter\noti\android\app\src\main\java\com\example\notiapp\NotificationExtender.java:24: error: ';' expected protected boolean onNotificationProcessing(OSNotificationReceivedResult receivedResult) { ^ 2 errors
FAILURE: Build failed with an exception.
还有一个问题: 我这样写的naitve代码(Java代码)是否可以解决后台通知问题,请指导我,我是初学者
看下面的 dart 代码:
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")
);
}
}
javaNotificationExtender类:
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);
}
}
最佳答案
这看起来不正确:
@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(..)
或者应该在某处重写该方法否则。
关于java - 当我使用 flutter 运行 native 代码时出现一些错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56859184/