Guest User

Untitled

a guest
Nov 24th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. export const onCharactersMessageCreate = functions.firestore.document(`/${User.entity}/{userId}/${Message.entity}/{messageId}`).onCreate(async event => {
  2. const userId = event.params!.userId;
  3. const userRef = firestore.collection(User.entity).doc(userId);
  4. const tokenSnapshot = await userRef.collection(FcmToken.entity).get();
  5. const tokens = tokenSnapshot.docs.map(doc => { return doc.id; });
  6. const message = <Message> event.data.data();
  7. // TODO: Add sender user name
  8. // TODO: fix locale
  9. const title = (await new I18N('ja').notification())('newMessageTitle');
  10. const payload = notification.createPayload(
  11. title,
  12. message.text);
  13. const response = await admin.messaging().sendToDevice(tokens, payload);
  14. // 無効なtokenを削除
  15. // TODO: 動作確認
  16. // TODO: 別メソッドにする
  17. const results = response.results.map((response, i) => { return { response, token: tokens[i] }; });
  18. const errorCodes = [
  19. 'messaging/invalid-registration-token',
  20. 'messaging/registration-token-not-registered',
  21. ];
  22. for (const r of results) {
  23. const error = r.response.error;
  24. if (error == null) {
  25. continue;
  26. }
  27. console.error(error.code);
  28. if (errorCodes.indexOf(error.code) != null) {
  29. await userRef.collection(FcmToken.entity).doc(r.token).delete();
  30. }
  31. }
  32. });
Add Comment
Please, Sign In to add comment