Update routes/sendnotifications.py
Browse files
routes/sendnotifications.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
import os
|
2 |
import aiohttp
|
|
|
3 |
from fastapi import APIRouter, HTTPException, Header
|
4 |
from pydantic import BaseModel
|
5 |
from google.oauth2 import service_account
|
@@ -36,6 +37,10 @@ class NotificationRequest(BaseModel):
|
|
36 |
keyword: str
|
37 |
target_user_id: str
|
38 |
|
|
|
|
|
|
|
|
|
39 |
async def verify_user_token(user_token: str) -> str:
|
40 |
headers = {
|
41 |
"Authorization": f"Bearer {user_token}",
|
@@ -168,7 +173,8 @@ async def send_notification(
|
|
168 |
title = "🎉 New Follower!"
|
169 |
body = f"{follower_name} started following you."
|
170 |
|
171 |
-
|
|
|
172 |
payload = {
|
173 |
"message": {
|
174 |
"notification": {
|
@@ -177,11 +183,11 @@ async def send_notification(
|
|
177 |
},
|
178 |
"token": target_user["token_fcm"],
|
179 |
"android": {
|
180 |
-
"collapse_key":
|
181 |
},
|
182 |
"apns": {
|
183 |
"headers": {
|
184 |
-
"apns-collapse-id":
|
185 |
}
|
186 |
}
|
187 |
}
|
|
|
1 |
import os
|
2 |
import aiohttp
|
3 |
+
import hashlib
|
4 |
from fastapi import APIRouter, HTTPException, Header
|
5 |
from pydantic import BaseModel
|
6 |
from google.oauth2 import service_account
|
|
|
37 |
keyword: str
|
38 |
target_user_id: str
|
39 |
|
40 |
+
def short_collapse_key(follower_id: str, target_user_id: str) -> str:
|
41 |
+
raw = f"follow:{follower_id}:{target_user_id}"
|
42 |
+
return hashlib.sha1(raw.encode()).hexdigest()[:20] # 20 chars = 40 hex bytes
|
43 |
+
|
44 |
async def verify_user_token(user_token: str) -> str:
|
45 |
headers = {
|
46 |
"Authorization": f"Bearer {user_token}",
|
|
|
173 |
title = "🎉 New Follower!"
|
174 |
body = f"{follower_name} started following you."
|
175 |
|
176 |
+
collapse_id = short_collapse_key(follower_id, target_user_id)
|
177 |
+
|
178 |
payload = {
|
179 |
"message": {
|
180 |
"notification": {
|
|
|
183 |
},
|
184 |
"token": target_user["token_fcm"],
|
185 |
"android": {
|
186 |
+
"collapse_key": collapse_id
|
187 |
},
|
188 |
"apns": {
|
189 |
"headers": {
|
190 |
+
"apns-collapse-id": collapse_id
|
191 |
}
|
192 |
}
|
193 |
}
|