I'm using Firebase console to send notifications to users. However I want to let users disable notifications. How can I disable them?
I am able to handle (and stop) notifications through FirebaseMessagingService only when the app is in foreground. But I cannot stop notifications when the app is in background.
Firebase Cloud Messaging (FCM), formerly known as Google Cloud Messaging (GCM), is a cross-platform cloud solution for messages and notifications for Android, iOS, and web applications, which as of June 2022 can be used at no cost.
Firebase notifications behave differently depending on the foreground/background state of the receiving app. If you want foregrounded apps to receive notification messages or data messages, you'll need to write code to handle the onMessageReceived callback.
Without FCM, you'd need to build more than three notification systems. You'd need one each for iOS and Android, but then you'd also need to accommodate all the different types of browsers for the web application to deliver push notifications without a hitch.
Firebase Cloud Messaging (FCM) provides a reliable and battery-efficient connection between your server and devices that allows you to deliver and receive messages and notifications on iOS, Android, and the web at no cost.
Complete the following in the Firebase console: To disable all automatic (out-of-the-box) monitoring, create a perf_disable_auto parameter in your app's project, then set its value to true. To disable all custom monitoring, create a perf_disable_manual parameter in your app's project, then set its value to true.
Push Notification is sent to the user which contains the message you inputted First download android studio and install. Name your project anything, I named mine Firebase Notification Android Create 7 new packages name them — di, firebase, helper, model, view, viewmodel, network Choose a Google Account — Select Default Account For Firebase
Set up a Firebase Cloud Messaging client app on Android. To write your Firebase Cloud Messaging Android client app, use the FirebaseMessaging API and Android Studio 1.4 or higher with Gradle. The instructions in this page assume that you have completed the steps for adding Firebase to your Android project.
Gradle builds that use Android Gradle plugin (AGP) v4.2 or earlier need to enable Java 8 support. Otherwise, these Android projects get a build failure when adding a Firebase SDK. Add the listed compileOptions from the error message to your app-level build.gradle file.
Not sure why this happens to you (code snippet maybe?), but there is a heavy weight ultimate solution: define your own c2dm receiver in the manifest and set the priority high (>0) then stop the notification from processing.
GCM messages are processed as ordered broadcasts, so you can stop the processing chain by calling this method:
https://developer.android.com/reference/android/content/BroadcastReceiver.html#abortBroadcast()
If you don't do anything in your receiver then the next receiver will do the processing, which will be the Firebase receiver.
Please note: Firebase is sending push notification for Remote Config changes. So, if you use Remote Config then it is not advisable to suppress any data push.
I couldn't comment yet, but racs' comment helped me a lot.
Actually, Firebase docs recommend to use data push instead of notifications if you want to have complete control over how it is handled: firebase.google.com/docs/cloud-messaging/… - quote: "Use notifications when you want FCM to handle displaying a notification on your client app's behalf. Use data messages when you want your app to handle the display or process the messages on your Android client app..."
So basically, I changed the body for the push notification request from:
{
"data": {
"type" : "mytype"
},
"notification": {
"title": "My Title",
"body": "My Notification Message"
},
"to": "/topics/all"
}
To:
{
"data": {
"type" : "mytype",
"title": "My Title",
"body": "My Notification Message"
},
"to": "/topics/all"
}
Now my app calls onMessageReceived() everytime even in background, and I just changed the methods to use the obtained notification title and message in the push data.
I meet the same question. I use subscribeToTopic API to subscribe the notification.
FirebaseMessaging.getInstance().subscribeToTopic("APP");
And I find the other API unsubscribeFromTopic to unsubscribe.
FirebaseMessaging.getInstance().unsubscribeFromTopic("APP");
Maybe it would be useful for APP using topic notification. It would not receive notification in foreground and background.
I had this problem.and solved by use below code:
Enable Firebase Notifications:
FirebaseInstanceId.getInstance().getToken();
Disable Firebase Notifications: deleteInstanceId() is a blocking call. It cannot be called on the main thread.
FirebaseInstanceId.getInstance().deleteInstanceId();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With