fechanique / cordova-plugin-fcm

Google FCM Push Notifications Cordova Plugin
624 stars 996 forks source link

Clear notification tray within app? #294

Open camden-kid opened 7 years ago

camden-kid commented 7 years ago

Is there a way with this plugin to clear the notification tray from within the app. My requirement is to clear any notifications if the user access a certain part of the app.

The only way I can see to clear the notification is to tap on it or tap the clear button (on Android).

nagubabu commented 7 years ago

Same here!! When the user opens the app directly without tapping the notifications on the notification tray, then how to clear the system tray notifications?? Plz help me if you get any solutions on this @camden-kid

ibonkonesa commented 7 years ago

You have to rewrite the MainActivity in order to clean them.

this is my MainActivity (you can find it in platforms>android>nameofpackage


import android.os.Bundle;
import org.apache.cordova.*;
import android.app.NotificationManager;
import android.content.Context;

public class MainActivity extends CordovaActivity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        Bundle extras = getIntent().getExtras();
        if (extras != null && extras.getBoolean("cdvStartInBackground", false)) {
            moveTaskToBack(true);
        }

           final NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
                   notificationManager.cancelAll();
        loadUrl(launchUrl);
    }

  @Override
    protected void onResume() {
        super.onResume();
        final NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.cancelAll();

    }
}
nagubabu commented 7 years ago

Thanks for the update. But what about iOS? I request the author, plz implement this feature in the plugin itself.

Hadjerkhd commented 6 years ago

Hello, I'm having the same problem here, the thing is that the installed plugin already includes this update but this is not working for me. And the push notifications in the system tray aren't cleared after i access the application. Any workaround , or a solution for this problem ?

coolvasanth commented 6 years ago

Is there any way to achieve this on iOS ?

laberg commented 5 years ago

I cannot get @ibonkonesa solution to work. I get a build failure due to symbol "launchUrl" not being found. Does anyone knows how to fix this? Thanks

Kunj-Choksi commented 4 years ago

A slight modification in @ibonkonesa solution.

For version 2.1.2 (Android only)

Notification can be cleared by adding below snippet in

plugins > cordova-plugin-fcm > android > FCMPluginActivity.java

Code snippet:

final NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancelAll();

Add this in oncreate method before forceMainActivityReload().

Thanks to @ibonkonesa for reference.