我想用FCM和这个包实现本地通知,我是这样实现的:首先:我在我的项目上安装了FCM和Flutter本地通知包,之后,我在android -> app -> src -> main -> ... Application:
中创建了一个ApplicationKotlin类
应用程序代码:
package com.example.onion
import io.flutter.app.FlutterApplication
import io.flutter.plugin.common.PluginRegistry
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback
import io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin
import io.flutter.view.FlutterMain
import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService
import com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin
class Application : FlutterApplication(), PluginRegistrantCallback {
override fun onCreate() {
super.onCreate()
FlutterFirebaseMessagingService.setPluginRegistrant(this);
FlutterMain.startInitialization(this)
}
override fun registerWith(registry: PluginRegistry?) {
if (!registry!!.hasPlugin("io.flutter.plugins.firebasemessaging")) {
FirebaseMessagingPlugin.registerWith(registry!!.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"));
}
if (!registry!!.hasPlugin("com.dexterous.flutterlocalnotifications")) {
FlutterLocalNotificationsPlugin.registerWith(registry!!.registrarFor("com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin"));
}
}
}
舱单代码:
<application
android:name=".Application"
android:icon="@mipmap/ic_launcher"
android:label="onion">
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="AIza..." />
<activity
android:name=".MainActivity"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:launchMode="singleTop"
android:showWhenLocked="true"
android:theme="@style/LaunchTheme"
android:turnScreenOn="true"
android:windowSoftInputMode="adjustResize">
<meta-data
android:name="io.flutter.embedding.android.NormalTheme"
android:resource="@style/NormalTheme" />
<meta-data
android:name="io.flutter.embedding.android.SplashScreenDrawable"
android:resource="@drawable/launch_background" />
<intent-filter>
<action android:name="FLUTTER_NOTIFICATION_CLICK" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.yalantis.ucrop.UCropActivity"
android:screenOrientation="portrait"
android:theme="@style/Theme.AppCompat.Light.NoActionBar" />
<meta-data
android:name="flutterEmbedding"
android:value="2" />
</application>
而且我在稳定的频道上,但它不起作用,我把我的频道改为测试版,但仍然不起作用
扑动医生:
[√] Flutter (Channel beta, 1.24.0-10.2.pre, on Microsoft Windows [Version 10.0.14393], locale en-US)
• Flutter version 1.24.0-10.2.pre at C:srcflutter
• Framework revision 022b333a08 (3 weeks ago), 2020-11-18 11:35:09 -0800
• Engine revision 07c1eed46b
• Dart version 2.12.0 (build 2.12.0-29.10.beta)
[√] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
• Android SDK at C:UsersMahdiAppDataLocalAndroidSdk
• Platform android-30, build-tools 30.0.2
• ANDROID_HOME = C:UsersMahdiAppDataLocalAndroidSdk
• Java binary at: C:Program FilesAndroidAndroid Studiojrebinjava
• Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)
• All Android licenses accepted.
[√] Android Studio (version 4.0)
• Android Studio at C:Program FilesAndroidAndroid Studio
• Flutter plugin version 51.0.1
• Dart plugin version 193.7547
• Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)
[√] VS Code (version 1.51.1)
• VS Code at C:UsersMahdiAppDataLocalProgramsMicrosoft VS Code
• Flutter extension version 3.17.0
[√] Connected device (1 available)
• Nexus 6 (mobile) • 127.0.0.1:62025 • android-x86 • Android 5.1.1 (API 22)
• No issues found!
颤动pubspec.yaml
文件:
firebase_messaging: ^7.0.3
flutter_local_notifications: ^3.0.2
main.dart
代码:
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
new FlutterLocalNotificationsPlugin();
Future onSelectNotification(String payload) {
print("Mahdi: onSelectNotification");
}
@override
void initState() {
super.initState();
_fcm.getToken().then((value) => print("FCM TOKEN: $value"));
var initializationSettingsAndroid =
new AndroidInitializationSettings('@mipmap/ic_launcher');
var initializationSettingsIOS = new IOSInitializationSettings();
var initializationSettings = new InitializationSettings(
android: initializationSettingsAndroid,
iOS: initializationSettingsIOS,
);
flutterLocalNotificationsPlugin.initialize(
initializationSettings,
onSelectNotification: onSelectNotification,
);
_fcm.configure(
onMessage: (Map<String, dynamic> message) async {
showNotification(
message['notification']['title'],
message['notification']['body'],
);
print("onMessage: $message");
},
onLaunch: (Map<String, dynamic> message) async {
print("onLaunch: $message");
// TODO optional
Navigator.pushNamed(
context,
message['data']['status'],
);
},
onResume: (Map<String, dynamic> message) async {
print("onResume: $message");
// TODO optional
Navigator.pushNamed(
context,
message['data']['status'],
);
},
);
if (Platform.isIOS) {
dynamic iosSubscription = _fcm.onIosSettingsRegistered.listen((data) {});
_fcm.requestNotificationPermissions(IosNotificationSettings());
}
}
void showNotification(String title, String body) async {
await _demoNotification(title, body);
}
Future<void> _demoNotification(String title, String body) async {
var androidPlatformChannelSpecifics = AndroidNotificationDetails(
'channel_ID', 'channel name', 'channel description',
importance: Importance.max,
playSound: true,
// sound: 'sound',
showProgress: true,
priority: Priority.high,
ticker: 'test ticker');
var iOSChannelSpecifics = IOSNotificationDetails();
var platformChannelSpecifics = NotificationDetails(
android: androidPlatformChannelSpecifics,
iOS: iOSChannelSpecifics,
);
await flutterLocalNotificationsPlugin
.show(0, title, body, platformChannelSpecifics, payload: 'test');
}
但我得到了这个错误:
no implementation found for method show on channel dexterous.com/flutter/local_notifications
我在谷歌上搜索了很多,发现了一些类似的问题,比如这个。但这对我不起作用。
错误"no implementation found for method show on channel dexterous.com..."
发生在需要Android平台特定代码的旧版本SDK上。如本指南中所述,如果您使用的是最新的Flutter SDK版本和firebase_messaging
软件包,则无需执行这些步骤。