如何解决Firebase 推送通知未显示
我正在尝试使用 firebase 消息传递和 Flutter 本地通知从 firebase 推送通知,我已将我的项目与 firebase 连接并添加了所需的插件。我可以在我的 firebase 中发布通知,但它们无法出现在应用程序中。 请问如何解决这个问题。
我的代码
main.dart
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:Flutter/material.dart';
import 'package:Flutter_local_notifications/Flutter_local_notifications.dart';
const AndroidNotificationChannel channel = AndroidNotificationChannel(
'high_important_channel','High Important Notification','This Channel is used for important notifications',importance: Importance.high,playSound: true);
final FlutterlocalnotificationsPlugin FlutterlocalnotificationsPlugin =
FlutterlocalnotificationsPlugin();
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
await Firebase.initializeApp();
print('A bg message just showed up : ${message.messageId}');
}
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
await FlutterlocalnotificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterlocalnotificationsPlugin>()
?.createNotificationChannel(channel);
await FirebaseMessaging.instance.setForegroundNotificationPresentationoptions(
alert: true,badge: true,sound: true);
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',theme: ThemeData(
primarySwatch: Colors.blue,),home: MyHomePage(title: 'Flutter Demo Home Page'),);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key,this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
@override
void initState() {
super.initState();
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
RemoteNotification notification = message.notification;
AndroidNotification android = message.notification?.android;
if (notification != null && android != null) {
FlutterlocalnotificationsPlugin.show(
notification.hashCode,notification.title,notification.body,NotificationDetails(
android: AndroidNotificationDetails(
channel.id,channel.name,channel.description,color: Colors.blue,playSound: true,icon: '@mipmap/ic_launcher')));
}
});
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
print('A new onMessageOpenedApp was published!');
RemoteNotification notification = message.notification;
AndroidNotification android = message.notification?.android;
if (notification != null && android != null) {
showDialog(
context: context,builder: (_) {
return AlertDialog(
title: Text(notification.title),content: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,children: [Text(notification.body)],);
});
}
});
}
void showNotification() {
setState(() {
_counter++;
});
FlutterlocalnotificationsPlugin.show(
0,'Testing $_counter','Meeting tomorrow 8:30',NotificationDetails(
android: AndroidNotificationDetails(
channel.id,icon: '@mipmap/ic_launcher'),));
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,children: <Widget>[
Text(
'You have pushed the button this many times:',Text(
'$_counter',style: Theme.of(context).textTheme.headline4,],floatingActionButton: FloatingActionButton(
onpressed: showNotification,tooltip: 'Increment',child: Icon(Icons.add),// This trailing comma makes auto-formatting nicer for build methods.
);
}
}
build.gradle
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
localPropertiesFile.withReader('UTF-8') { reader ->
localProperties.load(reader)
}
}
def FlutterRoot = localProperties.getProperty('Flutter.sdk')
if (FlutterRoot == null) {
throw new GradleException("Flutter SDK not found. Define location with Flutter.sdk in the local.properties file.")
}
def FlutterVersionCode = localProperties.getProperty('Flutter.versionCode')
if (FlutterVersionCode == null) {
FlutterVersionCode = '1'
}
def FlutterVersionName = localProperties.getProperty('Flutter.versionName')
if (FlutterVersionName == null) {
FlutterVersionName = '1.0'
}
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'com.google.gms.google-services'
apply from: "$FlutterRoot/packages/Flutter_tools/gradle/Flutter.gradle"
android {
compileSdkVersion 30
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
defaultConfig {
// Todo: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "com.example.notification"
minSdkVersion 16
targetSdkVersion 30
versionCode FlutterVersionCode.toInteger()
versionName FlutterVersionName
}
buildTypes {
release {
// Todo: Add your own signing config for the release build.
// Signing with the debug keys for Now,so `Flutter run --release` works.
signingConfig signingConfigs.debug
}
}
}
Flutter {
source '../..'
}
dependencies {
implementation 'com.google.firebase:firebase-analytics'
implementation platform('com.google.firebase:firebase-bom:28.2.0')
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。