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

I use the Firebase console to send notifications to users. However, I want users to turn off notifications. How to disable them?

I can only process (and stop) notifications through the FirebaseMessagingService when the application is in the foreground. But I can not stop notifications when the application is in the background.

+9
source share
5 answers

I donโ€™t know why this happens to you (maybe a piece of code?), But there is a solution for a lot of weight: define your own c2dm receiver in the manifest and set high priority (> 0), and then stop the notification processing.

GCM messages are processed as ordered mailings, so you can stop the processing chain by calling this method:

https://developer.android.com/reference/android/content/BroadcastReceiver.html#abortBroadcast ()

If you are not doing anything in your receiver, then the next handler will execute the processing, which will be the Firebase receiver.

Note : Firebase sends a push notification of changes to Remote Config. Thus, if you use Remote Config, then it is not recommended to suppress any data.

+3
source

I could not comment yet, but the racs comment helped me a lot.

In fact, Firebase docs recommends using push data instead of notifications if you want full control over how it is handled: firebase.google.com/docs/cloud-messaging / ... - quote: "Use notifications when you want, so that FCM handles the display of notifications for your client application. Use data messages if you want your application to handle the display or process messages on your Android client application ... "

So basically, I changed the body for the push request request:

{ "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 application calls onMessageReceived () every time even in the background, and I just changed the methods of using the received notification header and message in the push data.

+7
source

I am having this problem. And decided to use the code below:

Enable Firebase Notifications:

 FirebaseInstanceId.getInstance().getToken(); 

Disable Firebase notifications: deleteInstanceId () is a blocking call. It cannot be called in the main thread.

 FirebaseInstanceId.getInstance().deleteInstanceId(); 
+3
source

You can handle all push events from all difference services (with Firebase). First create YouUniversalFCM extends WakefulBroadcastReceiver

For instance:

 public class YourUniversalFCM extends WakefulBroadcastReceiver { private static final String ACTION_REGISTRATION = "com.google.android.c2dm.intent.REGISTRATION"; private static final String ACTION_RECEIVE = "com.google.android.c2dm.intent.RECEIVE"; @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); for (String key : intent.getExtras().keySet()) { Log.d( "TAG", "{UniversalFCM}->onReceive: key->" + key + ", value->" + intent.getExtras().get(key) ); } switch (action) { case ACTION_REGISTRATION: // TODO: 2016-08-06 break; case ACTION_RECEIVE: // TODO: 2016-08-06 break; default: } abortBroadcast(); } } 

Next in AndroidManifest.xml

 <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.WAKE_LOCK"/> <application ...> ... <receiver android:name=".fcm.receivers.UniversalFCM" android:permission="com.google.android.c2dm.permission.SEND"> <intent-filter> <action android:name="com.google.android.c2dm.intent.RECEIVE"/> <action android:name="com.google.android.c2dm.intent.REGISTRATION"/> <category android:name="YOUR_PACKAGE"/> </intent-filter> </receiver> </application> 

Replace YOUR_PACKAGE with your application package. All this. If you want to subscribe to another push service, you must add YourTokenService

 public class YourTokenService extends FirebaseInstanceIdService { @Override public void onTokenRefresh() { String refreshedToken = FirebaseInstanceId.getInstance().getToken(); Ld(TokenService.class, "onTokenRefresh() Refreshed token: " + refreshedToken); // TODO: 2016-08-06 super.onTokenRefresh(); } } 

And declared in AndroidManifest.xml

 <service android:name="YourTokenService"> <intent-filter> <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/> </intent-filter> </service> 
+2
source

I answer the same question. I use the subscribeToTopic API to subscribe to a notification.

 FirebaseMessaging.getInstance().subscribeToTopic("APP"); 

And I found another unsubscribeFromTopic API to unsubscribe.

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

Perhaps this would be useful for APP with a topic notification. He will not receive notification in the foreground and in the background.

+1
source

All Articles