我的问题是关于使用Flutter和firebase_messaging插件的推送通知
问题:
我已将firebase_messaging插件集成到我的flutter应用程序中,以进行推送通知。据我所知,我可以保证在我收到推送通知时,该设置是正确的。仅当我的应用程序在后台运行时才收到推送消息(如最小化但可在系统内存中使用),就会出现问题。当应用程序处于前台或被终止时,不会收到推送。
为了提供我尝试过的解决方案,我不知道实际需要完成什么。
我遵循了有关此问题的教程,并应用了每个步骤来克服该问题,但无济于事。
我正在使用nodeJS来处理firebase-admin和serviceaccountkey文件,因为我需要从数据库中获取device_tokens。
节点JS
const firebase = require('firebase-admin');
const serviceAccount = require('../controller/firebase/serviceAccountKey.json');
firebase.initializeApp({
credential: firebase.credential.cert(serviceAccount)
});
//Function to actually implement the push
const pushNotificationInitiatorSubscription = (resultValue) => {
let devicesTokenString = resultValue[0]['device_token'];
const firebaseToken = devicesTokenString;
const payLoad = {
notification: {
title: 'New Subscription',
body: 'You have a new subscription to your material ' + resultValue[0]['course_name']
}
};
const option = {
priority: 'high'
};
firebase.messaging().sendToDevice(firebaseToken, payLoad, option).then(success => {
// console.log(success.results[0]['error']);
// console.log(success.results[1]['error']);
// console.log(success);
}).catch(err => {
console.log(err);
})
扑
import 'package:firebase_messaging/firebase_messaging.dart';
class FirebaseCloudMessage {
static FirebaseCloudMessage _instance = new FirebaseCloudMessage.internal();
FirebaseCloudMessage.internal();
factory FirebaseCloudMessage() => _instance;
final FirebaseMessaging _firebaseMessaging = new FirebaseMessaging();
configureFirebaseListeners() {
print('Here');
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
print("Message $message");
// return message;
}, onLaunch: (Map<String, dynamic> message) async {
print("Message $message");
// return message;
}, onResume: (Map<String, dynamic> message) async {
print("Message $message");
// return message;
});
}
}
帮助将不胜感激。谢谢
这是当前从Firebase通知服务收到的通知的默认行为。如果您想在应用程序处于前台时显示通知,则必须手动编写代码。
这是一个使用flutter_local_notifications包在flutter中显示通知的演示。
注意:这是一个使用flutter_local_notification包在flutter中显示通知的非常基本的示例。您可以配置很多东西。有关详细说明,请访问此软件包的主页或阅读此非常好的中篇文章
步骤1:在您的pubspec.yaml中安装flutter_local_notifications软件包
步骤2:在initState()中启动FlutterLocalNotifications:
@override
void initState() {
super.initState();
var initializationSettingsAndroid =
new AndroidInitializationSettings('@mipmap/ic_launcher');
var initializationSettingsIOS = new IOSInitializationSettings();
var initializationSettings = new InitializationSettings(
initializationSettingsAndroid, initializationSettingsIOS);
flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin();
flutterLocalNotificationsPlugin.initialize(initializationSettings,
onSelectNotification: onSelectNotification);
}
步骤3:创建一个函数来处理通知上的单击事件。当用户点击通知时,将调用此功能。
Future<dynamic> onSelectNotification(String payload) async {
/*Do whatever you want to do on notification click. In this case, I'll show an alert dialog*/
showDialog(
context: context,
builder: (_) => AlertDialog(
title: Text(payload),
content: Text("Payload: $payload"),
),
);
}
步骤4:编写一个显示通知的函数:
Future<void> _showNotification(
int notificationId,
String notificationTitle,
String notificationContent,
String payload, {
String channelId = '1234',
String channelTitle = 'Android Channel',
String channelDescription = 'Default Android Channel for notifications',
Priority notificationPriority = Priority.High,
Importance notificationImportance = Importance.Max,
}) async {
var androidPlatformChannelSpecifics = new AndroidNotificationDetails(
channelId,
channelTitle,
channelDescription,
playSound: false,
importance: notificationImportance,
priority: notificationPriority,
);
var iOSPlatformChannelSpecifics =
new IOSNotificationDetails(presentSound: false);
var platformChannelSpecifics = new NotificationDetails(
androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.show(
notificationId,
notificationTitle,
notificationContent,
platformChannelSpecifics,
payload: payload,
);
}
步骤5:调用_showNotification()函数:
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
//print("Message $message");
_showNotification(1234, "GET title FROM message OBJECT", "GET description FROM message OBJECT", "GET PAYLOAD FROM message OBJECT");
return;
}
}
之后,即使您的应用程序位于前台,您也可以显示通知。希望这会很有用。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句