【发布时间】:2022-01-15 03:47:34
【问题描述】:
在我的应用程序中,有些用户可以发送消息,但只能看到传入的消息。消息根据用户的部分进行。到目前为止,没有问题。当消息到来时,它也应该出现在通知中。我有这方面的服务。
import ‘package:askon_notification/model/message.dart’;
import ‘package:askon_notification/model/user.dart’;
import ‘package:http/http.dart’ as http;
class SendNotificationService{
Future<bool> sendNotification(Messages sendMessage, MyUser sendingUser, String topic) async{
var endURL = Uri.parse(‘https://fcm.googleapis.com/fcm/send’);
String firebaseKey =“AAAA3**********************************”;
Map<String, String> headers = {
"Content-Type" : "application/json",
"Authorization" : "key=$firebaseKey"
};
String json = '{ "to" : "topic/$topic", "data" : { "message" : "test", "title" : "baslik" }
}';
http.Response response = await http.post(endURL, headers: headers, body: json);
if(response.statusCode == 200){
print("işlem başarılı");
return true;
}else{
print("işlem başarısız");
return false;
}
}
}
虽然我看到操作成功消息,但应用程序没有收到任何通知。我应该遵循什么方法?
【问题讨论】:
-
firebaser here 调用 FCM REST API 要求您在代码中指定 FCM server* 键。顾名思义,此密钥应仅用于服务器端代码或其他受信任的环境中。这样做的原因是任何拥有 FCM 服务器密钥的人都可以向您的所有用户发送他们想要的任何消息。通过在您的 Android/iOS 应用程序中包含此密钥,恶意用户可以找到它,并且您将用户置于危险之中。请参阅stackoverflow.com/a/37993724 以获得更好的解决方案。
标签: android ios flutter dart google-cloud-firestore