【发布时间】:2022-01-03 11:22:30
【问题描述】:
我已经好几天没有尝试解决这个问题了。我已经为推送通知实现了 Firebase 消息传递,因此,我能够在应用处于后台时接收通知,但 在应用处于前台时无法接收强>。我尝试了所有可能的解决方案或在 stackoverflow/github 上,但到目前为止没有任何效果。以下是我尝试过的来源:
当前代码 =>
NotificationService.dart:
class NotificationService {
static final FlutterLocalNotificationsPlugin
_flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
static Future initialize() async {
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
AndroidInitializationSettings androidInitializationSettings =
AndroidInitializationSettings('@mipmap/ic_launcher');
InitializationSettings initializationSettings =
InitializationSettings(android: androidInitializationSettings);
await flutterLocalNotificationsPlugin.initialize(
initializationSettings,
// onSelectNotification: doSomething(),
);
}
static Future showNotification(RemoteMessage message) async {
AndroidNotificationDetails androidDetails = AndroidNotificationDetails(
"default_notification_channel_id",
"channel",
enableLights: true,
enableVibration: true,
priority: Priority.high,
importance: Importance.max,
largeIcon: DrawableResourceAndroidBitmap("ic_launcher"),
styleInformation: MediaStyleInformation(
htmlFormatContent: true,
htmlFormatTitle: true,
),
playSound: true,
);
await _flutterLocalNotificationsPlugin.show(
message.data.hashCode,
message.data['title'],
message.data['body'],
NotificationDetails(
android: androidDetails,
));
}
}
main.dart:
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
await Firebase.initializeApp();
print('Handling a background message ${message.messageId}');
print('Notification Message: ${message.data}');
NotificationService.showNotification(message);
}
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
runApp(
MyApp(),
);
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
void initState() {
super.initState();
NotificationService.initialize();
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
NotificationService.showNotification(message);
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(...)
还有一点要补充的是,当应用程序处于后台(我可以收到通知的情况)时,我得到前两个print 语句的输出,即message.messageId 和@ 987654329@:
I/flutter ( 3363): Handling a background message 0:1637822186109880%5dd28a92f9fd7ecd
I/flutter ( 3363): Notification Message: {body: Price is at Bull! Buy Now!, title: Hello Traders!}
但是当应用程序处于前台(我没有收到通知的情况)时,我得到以下输出:D/FLTFireMsgReceiver( 3363): broadcast received for message
如果有人可以帮助我,那真的很有帮助,已经调试了几天,找到了解决方案。 谢谢!
AndroidManifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.trading_app">
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:label="TradingApp"
android:icon="@mipmap/ic_launcher">
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@mipmap/ic_launcher" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="@string/default_notification_channel_id" />
<receiver android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
</intent-filter>
</receiver>
<receiver android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationReceiver" />
<activity
android:name=".MainActivity"
android:launchMode="singleTop"
android:theme="@style/LaunchTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="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="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<meta-data
android:name="flutterEmbedding"
android:value="2" />
<!-- <meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="high_importance_channel" /> -->
</application>
</manifest>
【问题讨论】:
标签: firebase flutter dart firebase-cloud-messaging