Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- export const onCharactersMessageCreate = functions.firestore.document(`/${User.entity}/{userId}/${Message.entity}/{messageId}`).onCreate(async event => {
- const userId = event.params!.userId;
- const userRef = firestore.collection(User.entity).doc(userId);
- const tokenSnapshot = await userRef.collection(FcmToken.entity).get();
- const tokens = tokenSnapshot.docs.map(doc => { return doc.id; });
- const message = <Message> event.data.data();
- // TODO: Add sender user name
- // TODO: fix locale
- const title = (await new I18N('ja').notification())('newMessageTitle');
- const payload = notification.createPayload(
- title,
- message.text);
- const response = await admin.messaging().sendToDevice(tokens, payload);
- // 無効なtokenを削除
- // TODO: 動作確認
- // TODO: 別メソッドにする
- const results = response.results.map((response, i) => { return { response, token: tokens[i] }; });
- const errorCodes = [
- 'messaging/invalid-registration-token',
- 'messaging/registration-token-not-registered',
- ];
- for (const r of results) {
- const error = r.response.error;
- if (error == null) {
- continue;
- }
- console.error(error.code);
- if (errorCodes.indexOf(error.code) != null) {
- await userRef.collection(FcmToken.entity).doc(r.token).delete();
- }
- }
- });
Add Comment
Please, Sign In to add comment