Flutter 功能中的 Firebase Cloud Messaging 有效但未收到通知
Firebase Cloud Messaging in flutter function works but didn't receive notification
我想向设备发送 fcm 通知,但问题是它完全达到 api 成功,但推送通知未发送到应用程序。
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<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);
}
消息发送后我的控制台:
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
在我的控制台中,我可以看到所有打印语句部分 fcm 函数,但我没有在我的应用程序中收到任何通知。帮助赞赏
看来我的令牌在 fcm 函数中变为 null 已纠正它。
我想向设备发送 fcm 通知,但问题是它完全达到 api 成功,但推送通知未发送到应用程序。
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<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);
}
消息发送后我的控制台:
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
在我的控制台中,我可以看到所有打印语句部分 fcm 函数,但我没有在我的应用程序中收到任何通知。帮助赞赏
看来我的令牌在 fcm 函数中变为 null 已纠正它。