Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase on Android - I want to disable firebase notifications on Android client

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.

like image 739
martinodecarlo Avatar asked May 29 '16 14:05

martinodecarlo


People also ask

What is FCM notification in Android?

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.

How do I handle the Firebase notification when an app is in foreground?

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.

Can I send push notifications without FCM?

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.

Why do I need Firebase for push notifications?

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.

How do I Turn Off monitoring in Firebase?

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.

How to send push notification using Firebase notification Android?

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

How do I set up Firebase Cloud Messaging on Android?

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.

Why am I getting a build failure when adding Firebase SDK?

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.


4 Answers

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.

like image 45
racs Avatar answered Oct 10 '22 21:10

racs


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.

like image 55
Alvin Rusli Avatar answered Oct 10 '22 21:10

Alvin Rusli


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.

like image 3
Fantasy Fang Avatar answered Oct 10 '22 23:10

Fantasy Fang


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();
like image 3
Better Avatar answered Oct 10 '22 22:10

Better