当我试图摆脱应用程序滑动时收到通知的时候,大家好,我使用平台频道,但是当我运行代码时遇到一些错误,我在Java中没有任何背景,并且该错误我面对的是Java代码,出现的错误是:
E:\ flutter \ noti \ android \ app \ src \ main \ java \ com \ example \ notiapp \ NotificationExtender.java:24:错误:';'预期 受保护的布尔值onNotificationProcessing(OSNotificationReceivedResult receiveResult){ ^ E:\ flutter \ noti \ android \ app \ src \ main \ java \ com \ example \ notiapp \ NotificationExtender.java:24:错误:';'预期 受保护的布尔值onNotificationProcessing(OSNotificationReceivedResult receiveResult){ ^ 2个错误
失败:构建失败,并出现异常。
另一个问题: 这样,我编写了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);
}
}
答案 0 :(得分:0)
这看起来不正确:
@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(..)
或在其他地方覆盖该方法。