How to disable FCM push notification using toggle button?

无人久伴 提交于 2020-06-28 03:35:08

问题


I'm develop mobile app push notification using xamarin and firebase. I want to put a toggle button on my application to turn off the alarm. I tried a lot but i could not solve it.

There are many words I want to say, but I can not speak English and I am using Google Translator. Please help me.

  1. I tried disable push notification using fcm topic named 'allowPushNotification'. When turn on toggle button, subscribed topic, and unsubscribed topic when turn off toggle button. But when i send to push notification using 'TOKEN', A push is sent regardless of the user's 'allowPushNotification' options. Is it possible to do the following?

{ "to":"TOKENTOKENTOKENTOKEN", AND "condition":"'allowPushNotification' in topics" }

  1. I tried to control display push alarm on 'OnPushReceived' event, but it failed. On this event, i checked allow push option, and use clear Notification method. but received -> clear -> display push....

  2. I thought about user's allow push notification option data to my database and filtered the user target using db query. but this way, There is a lot of unnecessary database access when every time the user presses the toggle button.

If this resolves, I want to reject the push notification at a certain time. help me...


回答1:


Well if you read the FCM Docs it says the following:

Use notification messages when you want FCM to handle displaying a notification on your client app's behalf. Use data messages when you want to process the messages on your client app.

FCM can send a notification message including an optional data payload. In such cases, FCM handles displaying the notification payload, and the client app handles the data payload.

Since you want to handle all the notifications as per your toggle button.

I would suggest you make the required changes in the payload. Once you do that the OnMessageReceived method in the FirebaseMessagingService class will be called every time, Hence providing you with the control over showing notifications. Which you can show or not show based on a boolean set by your toggle.

For a better understanding of how to work with the payloads and FCM in general check this detailed blog.




回答2:


If you send a message to a device by token, it will not follow your topic setting. If you want to block this kind of notification, you need to go to system setting. You could refer to this link.

When the app is in the background, the OnMessageReceived() method will not be fired. I think you'd better not to send the message by Token. Using Topic messaging if it possible.



来源:https://stackoverflow.com/questions/53895727/how-to-disable-fcm-push-notification-using-toggle-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!