[英]Firebase Cloud Messaging not working for programmatically sent messages using Python
如果我 go 進入我的 firebase 控制台並設置活動,我的終端設備會很好地收到通知,但是對於使用設備的注冊令牌從 django/python 發送到特定設備的消息,我的移動設備上沒有收到通知。
不確定這是否重要,但我的應用程序仍在開發中,尚未投入生產,所以如果這很重要,請告訴我。
我的前端是 flutter,這是我用來獲取注冊令牌並將其發送到后端的 flutter 代碼:
Future<StreamedResponse> AddProductPut(context, pk, name, quantity, cost, selling, XFile? chosenImage) async {
String id_token = await FirebaseAuth.instance.currentUser!.getIdToken();
late String? fcm_token;
await FirebaseMessaging.instance.getToken().then((token) async {
fcm_token = token!;
}).catchError((e) {
print(e);
});
print(fcm_token);
var url = backend + "/pm/createproduct/" + pk.toString() + "/";
var request = http.MultipartRequest('PUT', Uri.parse(url));
print("FCM TOKEN");
print(fcm_token);
request.headers["Authorization"] = "Token " + id_token;
request.fields["name"] = name;
request.fields["quantity"] = quantity.toString();
request.fields["cost_price"] = cost.toString();
request.fields["selling_price"] = selling.toString();
request.fields["barcode"] = "11111";
request.fields["token"] = fcm_token!;
request.files.add(
await http.MultipartFile.fromPath(
'image',
chosenImage!.path
)
);
return await request.send();
}
這是我的 django 序列化程序中用於發送通知消息的 python 代碼:
registration_token = self.context['request'].data["token"],
print(registration_token[0])
print(type(registration_token[0]))
# See documentation on defining a message payload.
message = Message(
notification=Notification(
title='New Product Added',
body='A new product called ' + validated_data['name'] + ' has been added to your account.',
),
token=registration_token[0]
)
# Send a message to the device corresponding to the provided
# registration token.
response = send(message)
print(response)
發送此消息時,我的 django/python 控制台沒有收到任何錯誤,並且響應打印如下內容:
projects/<project name>/messages/16641.......0329
我的注冊令牌是這樣的:
cmLYCAbL0EsJrxppKzXvhF:APA..............qdt5ySYLbkQC_bpqwL6RdCwSzK_tX8iclp-e0QZB................lgw9g2eFNzfXpn2C4U................UnMphyWa6L9d-wUg
不確定是什么問題,但我現在在 Apple 和 Android 上收到 FCM 消息...
registration_token = subuser.fcm
print(registration_token)
message = Message(
notification=Notification('Added As A Sub-User', f'A user with the phone {instance.userprofile.phone} has added you to their account as a sub user.'),
token=registration_token
)
和我的 flutter 代碼,它使用用戶的 fcm 注冊令牌更新數據庫......
FirebaseMessaging.instance.getToken().then((token) async {
print(token);
String id_token = await FirebaseAuth.instance.currentUser!.getIdToken();
String uid = await FirebaseAuth.instance.currentUser!.uid;
final result = await http.put(
Uri.parse(backend + "/pm/setfcm/" + uid + "/"),
headers: {
"Content-Type": "application/json",
"Authorization": "Token " + id_token
},
body: jsonEncode({
"fcm": token,
})
);
}).catchError((e) {
print(e);
});
FirebaseMessaging.instance.onTokenRefresh.listen((newToken) async {
print(newToken);
String id_token = await FirebaseAuth.instance.currentUser!.getIdToken();
String uid = await FirebaseAuth.instance.currentUser!.uid;
final result = await http.put(
Uri.parse(backend + "/pm/setfcm/" + uid + "/"),
headers: {
"Content-Type": "application/json",
"Authorization": "Token " + id_token
},
body: jsonEncode({
"fcm": newToken,
})
);
});
response = send(message)
print(response)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.