gpt4 book ai didi

node.js - Flutter 推送通知仅在应用程序处于后台时才有效

转载 作者:行者123 更新时间:2023-12-03 12:11:00 27 4
gpt4 key购买 nike

我的问题是关于使用 Flutter 和 firebase_messaging 插件的推送通知
问题:
我已集成 firebase_messaging插件到我的 flutter 应用程序以推送通知。
我可以保证设置是正确的,因为我收到推送通知。仅当我的应用程序在后台运行时才收到推送(例如最小化但在系统内存中可用)时会出现问题。 未收到推送 当应用程序在前台或被杀死时。
为了提供我尝试过的解决方案,我无法弄清楚实际上需要做什么。
我遵循了有关此的教程并应用了每一步来克服该问题,但无济于事。
我正在使用 nodeJS 来处理 firebase-admin 和 serviceaccountkey 文件,因为我需要来自我的数据库的 device_tokens。
Node

 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);
})
flutter
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 中显示通知的一个非常基本的示例包裹。您可以配置很多东西。详细解释请访问homepage这个包或读这个真的很好medium article
第一步:安装flutter_local_notifications在您的 pubspec.yaml 中打包
第 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;
}
}
在此之后,即使您的应用程序在前台,您也可以显示通知。希望这将是有用的。

关于node.js - Flutter 推送通知仅在应用程序处于后台时才有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62688519/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com