简体   繁体   English

FCM通知退订

[英]FCM notification unsubscribe

I have implemented Firebase cloud messaging in my app for push notifications.我在我的应用程序中实现了 Firebase 云消息推送通知。 Everything working fine.一切正常。 But notifications are getting even user logged out from app.但是通知甚至让用户从应用程序注销。 If user uninstalled the app directly then how can i unsubscribe the topic notification.如果用户直接卸载了应用程序,那么我该如何取消订阅主题通知。 if i have subscribe the a topic and now i unsubscribe the topic and subscribe the new topic then i also receiving notification for old subscribe topic also.如果我已经订阅了一个主题,现在我取消订阅了这个主题并订阅了新主题,那么我也会收到旧订阅主题的通知。

if your are subscribing to fcm topic then there is way to unsubscribe from that topic to not recieve any notifications for that topic.如果您正在订阅 fcm 主题,那么可以取消订阅该主题,这样就不会收到该主题的任何通知。

below is the method you need:以下是您需要的方法:

unsubscribeFromTopic (String topic) unsubscribeFromTopic(字符串主题)

check google firebase docs here:在此处查看 google firebase 文档:

https://firebase.google.com/docs/reference/android/com/google/firebase/messaging/FirebaseMessaging https://firebase.google.com/docs/reference/android/com/google/firebase/messaging/FirebaseMessaging

When you uninstall the app, the service handling the FCM messages and notifications will also be removed from the device.当您卸载应用程序时,处理 FCM 消息和通知的服务也将从设备中删除。 So you won't be receiving any push notifications once you uninstall.因此,一旦卸载,您将不会收到任何推送通知。

As @Charan mentioned when you uninstall application FCM Push stop automatically. 正如@Charan提到的那样,当您卸载应用程序FCM时,Push自动停止。 However if you want to stop it programmatically, do below: 但是,如果要以编程方式停止它,请执行以下操作:

new Thread(new Runnable() {
    @Override
    public void run() {
        try {
            FirebaseInstanceId.getInstance().deleteInstanceId();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}).start();

I have placed deleteInstanceId() in a separate thread to stop java.io.IOException: MAIN_THREAD W/System.err and wrapped with try / catch to handle IOException . 我将deleteInstanceId()放在单独的线程中以停止java.io.IOException: MAIN_THREAD W/System.err并用try / catch包裹以处理IOException

To unsubscribe from a topic in Firebase Cloud Messaging (FCM), you need to call the unsubscribeFromTopic method of the FirebaseMessaging instance and pass the topic name as an argument.要取消订阅 Firebase Cloud Messaging (FCM) 中的主题,您需要调用 FirebaseMessaging 实例的 unsubscribeFromTopic 方法并将主题名称作为参数传递。

Here's an example in Android:这是 Android 中的示例:

FirebaseMessaging.getInstance().unsubscribeFromTopic("topic_name");

And here's an example in iOS:这是 iOS 中的示例:

Messaging.messaging().unsubscribe(fromTopic: "topic_name")

Regarding the issue with receiving notifications for the old topic after subscribing to a new topic, you should make sure to unsubscribe from the old topic before subscribing to the new topic.关于订阅新主题后收到旧主题通知的问题,请务必先取消订阅旧主题,然后再订阅新主题。 Also, make sure that you are subscribing to the new topic correctly and that the client app is correctly handling the topic subscriptions.此外,请确保您正确订阅了新主题,并且客户端应用程序正确处理了主题订阅。 You can try to test the subscriptions on a test device and check the logs for any errors.您可以尝试在测试设备上测试订阅并检查日志是否有任何错误。

If the problem persists, you may need to check the server-side code that is responsible for sending the notifications and make sure that it is sending the notifications to the correct topics.如果问题仍然存在,您可能需要检查负责发送通知的服务器端代码,并确保它向正确的主题发送通知。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM