SWM-99-degree / jaribean-fastapi

0 stars 1 forks source link

[Refact] FCM 토큰 전송 유형 변경 #48

Open psy-choi opened 1 year ago

psy-choi commented 1 year ago

목표

FCM 토큰이 잘 보내지지 않음

sendFCM = messaging.Message(
        data = {
            "data" : {
            "userId" : str(userId),
            "peopleNumber" : str(peopleNumber)},
            "type" : "data"
        },
        token = userToken
    )
    response = messaging.send(sendFCM)

해당 코드에서 오류 발견 후 리팩토링 진행하고 함

psy-choi commented 1 year ago

Issue

FCM 토큰이 보내지지 않았다.

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/threading.py", line 1016, in _bootstrap_inner
    self.run()
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/threading.py", line 953, in run
    self._target(*self._args, **self._kwargs)
  File "/Users/kisung/Desktop/jariBean-matching/myapi/app/main.py", line 61, in start_listening
    asyncio.run(listenExpireEvents())
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "uvloop/loop.pyx", line 1517, in uvloop.loop.Loop.run_until_complete
  File "/Users/kisung/Desktop/jariBean-matching/myapi/app/service/expireHandlerService.py", line 16, in listenExpireEvents
    for message in redisSubscriber.listen():
  File "/Users/kisung/venvs/myapi/lib/python3.10/site-packages/redis/client.py", line 1664, in listen
    response = self.handle_message(self.parse_response(block=True))
  File "/Users/kisung/venvs/myapi/lib/python3.10/site-packages/redis/client.py", line 1757, in handle_message
    handler(message)
  File "/Users/kisung/Desktop/jariBean-matching/myapi/app/service/expireHandlerService.py", line 8, in expireCallBack
    sendingCancelMessageToUser(userId)
  File "/Users/kisung/Desktop/jariBean-matching/myapi/app/service/firebaseService.py", line 79, in sendingCancelMessageToUser
    response = messaging.send(sendFCM)
  File "/Users/kisung/venvs/myapi/lib/python3.10/site-packages/firebase_admin/messaging.py", line 119, in send
    return _get_messaging_service(app).send(message, dry_run)
  File "/Users/kisung/venvs/myapi/lib/python3.10/site-packages/firebase_admin/messaging.py", line 407, in send
    data = self._message_data(message, dry_run)
  File "/Users/kisung/venvs/myapi/lib/python3.10/site-packages/firebase_admin/messaging.py", line 519, in _message_data
    data = {'message': _MessagingService.encode_message(message)}
  File "/Users/kisung/venvs/myapi/lib/python3.10/site-packages/firebase_admin/messaging.py", line 403, in encode_message
    return cls.JSON_ENCODER.default(message)
  File "/Users/kisung/venvs/myapi/lib/python3.10/site-packages/firebase_admin/_messaging_encoder.py", line 670, in default
    'data': _Validators.check_string_dict('Message.data', o.data),
  File "/Users/kisung/venvs/myapi/lib/python3.10/site-packages/firebase_admin/_messaging_encoder.py", line 128, in check_string_dict
    raise ValueError('{0} must not contain non-string values.'.format(label))
ValueError: Message.data must not contain non-string values.

이러한 오류가 떴는데, 즉 FCM data는 하나의 json으로 전송될 수는 있지만, key의 값은 무조건 string이어야 한다는 내용이다.


이때 userId와 PeopleNumber을 data 속에 넣어주어야 하고, 심지어 peopleNumber 또한 str 값으로 변경해주어야 했다.

![image](https://github.com/SWM-99-degree/jaribean-fastapi/assets/84831081/1c836c8c-2530-428c-a711-ea49eec22852)

이렇게 하니까 잘 나오는 것을 볼 수 있었다.