【发布时间】:2020-01-11 01:17:26
【问题描述】:
我在我的 Flutter 应用程序中使用 firebase_messaging 库进行 Firebase 推送通知。
目前我的 firebase_messaging 版本是 firebase_messaging: ^5.1.5,最近更新为最新版本。
我正在尝试在后台以及应用程序终止时接收通知。
我已按照 firebase_messaging 文档中提到的所有步骤进行操作,但不幸的是,我在 Flutter 中遇到了上述错误。
这是我在 dart 中的通知处理程序类
notification_handler.dart
import 'dart:async';
import 'dart:io';
import 'dart:math';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:path_provider/path_provider.dart';
import 'package:http/http.dart' as http;
class NotificationHandler {
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin;
FirebaseMessaging _fcm = FirebaseMessaging();
StreamSubscription iosSubscription;
static final NotificationHandler _singleton =
new NotificationHandler._internal();
factory NotificationHandler() {
return _singleton;
}
NotificationHandler._internal();
Future<dynamic> myBackgroundMessageHandler(
Map<String, dynamic> message) async {
print("onLaunch: $message");
_showBigPictureNotification(message);
// Or do other work.
}
initializeFcmNotification() async {
flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin();
var initializationSettingsAndroid =
new AndroidInitializationSettings('ic_launcher');
var initializationSettingsIOS = new IOSInitializationSettings(
onDidReceiveLocalNotification: onDidReceiveLocalNotification);
var initializationSettings = new InitializationSettings(
initializationSettingsAndroid, initializationSettingsIOS);
flutterLocalNotificationsPlugin.initialize(initializationSettings,
onSelectNotification: onSelectNotification);
if (Platform.isIOS) {
iosSubscription = _fcm.onIosSettingsRegistered.listen((data) {
// save the token OR subscribe to a topic here
});
_fcm.requestNotificationPermissions(IosNotificationSettings());
} else {
_saveDeviceToken();
}
_fcm.configure(
onMessage: (Map<String, dynamic> message) async {
print("onMessage: $message");
_showBigPictureNotification(message);
},
onBackgroundMessage: myBackgroundMessageHandler,
onLaunch: (Map<String, dynamic> message) async {
print("onLaunch: $message");
},
onResume: (Map<String, dynamic> message) async {
print("onResume: $message");
},
);
}
/// Get the token, save it to the database for current user
_saveDeviceToken() async {
String fcmToken = await _fcm.getToken();
print("FCM_TOKEN: $fcmToken");
}
Future<void> _showBigPictureNotification(message) async {
var rng = new Random();
var notifId = rng.nextInt(100);
var largeIconPath = await _downloadAndSaveImage(
'https://cdn.pixabay.com/photo/2019/04/21/21/29/pattern-4145023_960_720.jpg',
'largeIcon');
var bigPicturePath = await _downloadAndSaveImage(
'https://cdn.pixabay.com/photo/2019/04/21/21/29/pattern-4145023_960_720.jpg',
'bigPicture');
var bigPictureStyleInformation = BigPictureStyleInformation(
bigPicturePath, BitmapSource.FilePath,
largeIcon: largeIconPath,
largeIconBitmapSource: BitmapSource.FilePath,
contentTitle: message['data']['title'],
htmlFormatContentTitle: true,
summaryText: message['data']['body'],
htmlFormatSummaryText: true);
var androidPlatformChannelSpecifics = AndroidNotificationDetails(
'12', 'trading_id', message['data']['body'],
importance: Importance.High,
priority: Priority.High,
style: AndroidNotificationStyle.BigPicture,
styleInformation: bigPictureStyleInformation);
var platformChannelSpecifics =
NotificationDetails(androidPlatformChannelSpecifics, null);
await flutterLocalNotificationsPlugin.show(
notifId,
message['data']['title'],
message['data']['body'],
platformChannelSpecifics,
payload: message['data']['body']);
}
Future<void> _showBigTextNotification(message) async {
var rng = new Random();
var notifId = rng.nextInt(100);
var bigTextStyleInformation = BigTextStyleInformation(
message['data']['body'],
htmlFormatBigText: true,
contentTitle: message['data']['title'],
htmlFormatContentTitle: true,
summaryText: message['data']['body'],
htmlFormatSummaryText: true);
var androidPlatformChannelSpecifics = AndroidNotificationDetails(
'12', 'trading_id', '',
importance: Importance.High,
priority: Priority.High,
style: AndroidNotificationStyle.BigText,
styleInformation: bigTextStyleInformation);
var platformChannelSpecifics =
NotificationDetails(androidPlatformChannelSpecifics, null);
await flutterLocalNotificationsPlugin.show(
notifId,
message['data']['title'],
message['data']['body'],
platformChannelSpecifics,
payload: message['data']['body']);
}
Future onSelectNotification(String payload) async {
if (payload != null) {
debugPrint('notification payload: ' + payload);
}
// await Navigator.push(
// context,
// new MaterialPageRoute(builder: (context) => new SecondScreen(payload)),
// );
}
Future<void> onDidReceiveLocalNotification(
int id, String title, String body, String payload) async {
// display a dialog with the notification details, tap ok to go to another page
}
Future<String> _downloadAndSaveImage(String url, String fileName) async {
var directory = await getApplicationDocumentsDirectory();
var filePath = '${directory.path}/$fileName';
var response = await http.get(url);
var file = File(filePath);
await file.writeAsBytes(response.bodyBytes);
return filePath;
}
}
我在主屏幕上这样称呼它
@override
void initState() {
// TODO: implement initState
super.initState();
new NotificationHandler().initializeFcmNotification();
}
【问题讨论】:
-
我面临同样的问题,有什么解决方案吗,我的 Flutter 应用程序支持 Kotlin,所以我的 mainactivity 类位于 Kotlin 文件夹中,我从该文件夹中的 firebase 文档中添加了应用程序类并转换它对 Kotlin 来说,有点不巧,它给出了同样的错误。
标签: android flutter firebase-cloud-messaging