Flatter功能中的Firebase云消息功能正常,但没有';我没有收到通知
我想向设备发送fcm通知,但问题是它完全命中api,但推送通知未发送到应用程序Flatter功能中的Firebase云消息功能正常,但没有';我没有收到通知,firebase,flutter,push-notification,firebase-cloud-messaging,Firebase,Flutter,Push Notification,Firebase Cloud Messaging,我想向设备发送fcm通知,但问题是它完全命中api,但推送通知未发送到应用程序 Future<bool> callOnFcmApiSendPushNotifications( Future<String> userToken, Message message) async { print(message.message); print(userToken); final postUrl = 'https://fcm.googleap
Future<bool> callOnFcmApiSendPushNotifications(
Future<String> userToken, Message message) async {
print(message.message);
print(userToken);
final postUrl = 'https://fcm.googleapis.com/fcm/send';
final data = {
"notification": {"body": message.message, "title": "New Notification"},
"priority": "high",
"data": {
"click_action": "FLUTTER_NOTIFICATION_CLICK",
"id": "1",
"status": "done"
},
"to": "$userToken"
};
final headers = {
'content-type': 'application/json',
'Authorization':
'key=Server key'
};
print('hit till response');
final response = await Dio()
.post(postUrl, data: data, options: Options(headers: headers));
print(response.statusCode);
if (response.statusCode == 200) {
// on success do sth
print('test ok push CFM');
return true;
} else {
print(' CFM error');
// on failure do sth
return false;
}
}
static Future<String> getToken(userId) async {
final Firestore _db = Firestore.instance;
var token;
await _db
.collection('users')
.document(userId)
.collection('tokens')
.getDocuments()
.then((snapshot) {
snapshot.documents.forEach((doc) {
token = doc.documentID;
});
});
return token;
}
Future callonfcmapisendpush通知(
未来用户令牌(消息)异步{
打印(message.message);
打印(用户令牌);
最后姿势https://fcm.googleapis.com/fcm/send';
最终数据={
“通知”:{“正文”:message.message,“标题”:“新通知”},
“优先级”:“高”,
“数据”:{
“点击动作”:“颤振通知点击”,
“id”:“1”,
“状态”:“完成”
},
“至”:“$userToken”
};
最终标题={
“内容类型”:“应用程序/json”,
“授权”:
'密钥=服务器密钥'
};
打印(“点击直到响应”);
最终响应=等待Dio()
.post(姿势,数据:数据,选项:选项(标题:标题));
打印(响应状态码);
如果(response.statusCode==200){
//论成功做某事
打印(“测试正常推送CFM”);
返回true;
}否则{
打印(“CFM错误”);
//因失败而做某事
返回false;
}
}
静态未来getToken(userId)异步{
final Firestore _db=Firestore.instance;
var代币;
等待
.collection('用户')
.document(userId)
.collection(‘代币’)
.getDocuments()
.然后((快照){
snapshot.documents.forEach((doc){
token=doc.documentID;
});
});
返回令牌;
}
我的信息发送代码
Future<void> addMessageToDb(
Message message, User sender, User receiver) async {
var map = message.toMap();
await firestore
.collection("messages")
.document(message.senderId)
.collection(message.receiverId)
.add(map);
var token = getToken(message.receiverId);
print('token setted for fcm');
callOnFcmApiSendPushNotifications(token, message);
print('fcm worked');
return await firestore
.collection("messages")
.document(message.receiverId)
.collection(message.senderId)
.add(map);
}
Future addMessageToDb(
消息(消息、用户发送方、用户接收方)异步{
var map=message.toMap();
等待火库
.收集(“信息”)
.document(message.senderId)
.collection(message.receiverId)
.add(地图);
var token=getToken(message.receiverId);
打印(“为fcm设置的令牌”);
callOnFcmApiSendPushNotifications(令牌、消息);
打印(“fcm已处理”);
返回等待火场
.收集(“信息”)
.document(message.receiverId)
.collection(message.senderId)
.add(地图);
}
发送消息后的我的控制台:
I/flutter (14894): hari
I/flutter (14894): token setted for fcm
I/flutter (14894): let's see
I/flutter (14894): Instance of 'Future<String>'
I/flutter (14894): hit till response
I/flutter (14894): fcm worked
I/flutter (14894): 200
I/flutter (14894): test ok push CFM
I/颤振(14894):哈里
I/颤振(14894):为fcm设置标记
我/弗利特(14894):让我们看看
I/flatter(14894)‘未来’实例
I/颤振(14894):击中直到响应
I/颤振(14894):fcm工作
I/颤振(14894):200
I/颤振(14894):测试正常推送CFM
在我的控制台中,我可以看到所有的打印语句部分fcm功能,但我的应用程序中没有收到任何通知。帮助感谢看来我的令牌在fcm函数中变为空