撤销通知权限与匿名 FCM 令牌
Revoking Notification permission vs anonymous FCM token
我很想知道应用程序代码应如何结合 Firebase 云消息传递 (FCM) 处理通知权限令牌的撤销。该问题与 部分相似,尽管他们的案例不包括 FCM。
所以我们有以下场景:
应用程序不需要用户登录(因此我们存储在数据库中的令牌是匿名的 , 与任何 uid
)
无关
该应用程序提供了一组 opt-in/opt-out 按钮来授予和撤销通知权限。
该应用程序在数据库中收集令牌,供 sendToDevice(tokens, notifcation)
稍后在 firebase 函数中使用。
应用程序在令牌已发出 (getToken
) 或使用 localStorage
刷新 (onTokenRefresh
) 后保留令牌,以便稍后提供它deleteToken(token)
并在 UI 中指示通知订阅的当前状态。
现在,用户选择撤销曾经授予的权限 - 如果他们干净地这样做(通过选择退出按钮),将调用 deleteToken(token)
方法并适当的本地存储项目被清除。一切都应该同步,FCM 将不再发送给该用户。
问题 如果用户在全局浏览器范围内以更 administrative/bulk 的方式清除所有权限或本地存储会怎样?然后事情变得不同步。令牌从浏览器中消失,UI 将显示 opted out,但令牌尚未从数据库中删除,因此它仍然有效,用户将要不断收到通知。发送结果不会包含该用户的任何错误,因此我们将无法识别情况并删除令牌。
这里最常见的做法是什么?不鼓励匿名通知吗?我对 Notification 和 FCM 的理解是否遗漏了什么?
令牌最终会失效。您将能够分辨出来,因为向该令牌发送消息将产生 "error:NotRegistered" 的 error code。来自链接文档:
An existing registration token may cease to be valid in a number of
scenarios, including:
- If the client app unregisters with FCM.
- If the client app is automatically unregistered, which can happen if the user uninstalls the application. For example, on iOS, if the
APNS Feedback Service reported the APNS token as invalid.
- If the registration token expires (for example, Google might decide to refresh registration tokens, or the APNS token has expired
for iOS devices).
- If the client app is updated but the new version is not configured to receive messages.
For all these cases, remove this registration token from the app
server and stop using it to send messages.
如果您使用 Admin SDK 或不同版本的 FCM 服务器 API 发送消息,情况会类似。
虽然您可能不会立即收到有关此的反馈,但您最终会收到此错误消息,这就是您将其从数据库中删除的线索。
我很想知道应用程序代码应如何结合 Firebase 云消息传递 (FCM) 处理通知权限令牌的撤销。该问题与
所以我们有以下场景:
应用程序不需要用户登录(因此我们存储在数据库中的令牌是匿名的 , 与任何
uid
) 无关
该应用程序提供了一组 opt-in/opt-out 按钮来授予和撤销通知权限。
该应用程序在数据库中收集令牌,供
sendToDevice(tokens, notifcation)
稍后在 firebase 函数中使用。应用程序在令牌已发出 (
getToken
) 或使用localStorage
刷新 (onTokenRefresh
) 后保留令牌,以便稍后提供它deleteToken(token)
并在 UI 中指示通知订阅的当前状态。现在,用户选择撤销曾经授予的权限 - 如果他们干净地这样做(通过选择退出按钮),将调用
deleteToken(token)
方法并适当的本地存储项目被清除。一切都应该同步,FCM 将不再发送给该用户。问题 如果用户在全局浏览器范围内以更 administrative/bulk 的方式清除所有权限或本地存储会怎样?然后事情变得不同步。令牌从浏览器中消失,UI 将显示 opted out,但令牌尚未从数据库中删除,因此它仍然有效,用户将要不断收到通知。发送结果不会包含该用户的任何错误,因此我们将无法识别情况并删除令牌。
这里最常见的做法是什么?不鼓励匿名通知吗?我对 Notification 和 FCM 的理解是否遗漏了什么?
令牌最终会失效。您将能够分辨出来,因为向该令牌发送消息将产生 "error:NotRegistered" 的 error code。来自链接文档:
An existing registration token may cease to be valid in a number of scenarios, including:
- If the client app unregisters with FCM.
- If the client app is automatically unregistered, which can happen if the user uninstalls the application. For example, on iOS, if the APNS Feedback Service reported the APNS token as invalid.
- If the registration token expires (for example, Google might decide to refresh registration tokens, or the APNS token has expired for iOS devices).
- If the client app is updated but the new version is not configured to receive messages.
For all these cases, remove this registration token from the app server and stop using it to send messages.
如果您使用 Admin SDK 或不同版本的 FCM 服务器 API 发送消息,情况会类似。
虽然您可能不会立即收到有关此的反馈,但您最终会收到此错误消息,这就是您将其从数据库中删除的线索。