0

First sorry for my bad English.

I'm new on Ionic and I'm trying to pass my app from background to foreground when local notification trigger. I'm using the Katzer API (https://github.com/katzer/cordova-plugin-local-notifications), and I want to show a view (skype style incoming call) for stop or postpone a notification. This example works ok, but I need a method or something in order to show the postpone screen even when the screen is locked.

   cordova.plugins.notification.local.on('trigger', function (notification) {
            alert("triggered");
   }

Thanks in advance.

JPinios
  • 463
  • 1
  • 5
  • 10
  • I think that maybe I need to modify the API but I want to avoid it, because I need the app for Android and IOS. In Android this is the method for launch de app when you click the notification. public void launchApp() { Context context = getApplicationContext(); String pkgName = context.getPackageName(); Intent intent = context .getPackageManager() .getLaunchIntentForPackage(pkgName); intent.addFlags( Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP); context.startActivity(intent); } – JPinios Sep 01 '15 at 18:26
  • Could you explain better what you want? – lifeisfoo Sep 01 '15 at 21:39
  • Hi lifeisfoo thanks for your answer. I'm doing an alarm app and I want that when the notification is triggered the app displays a view for cancel or postpone. This is an example: https://lh5.ggpht.com/1SaisIsRVnu-jhDIoGidxRfbdgkWfP9K3EtzituVGmDgkGmgbtESku7GU9EVLL-6g4wR=h900-rw Thanks in advance. – JPinios Sep 03 '15 at 13:04
  • The local notification plugin works ok, but now I want to pass my app from background to foreground when the notification is triggered. I need a miracle method or something like this. Is this posible? cordova.plugins.notification.local.on('trigger', function (notification) { alert("triggered"); something.miraclemethod(); // this method pass your app to foreground } Thanks. – JPinios Sep 03 '15 at 13:16
  • https://stackoverflow.com/questions/32376247/phonegap-bring-from-background-to-foreground/54786108#54786108 – Suhail Mumtaz Awan Feb 20 '19 at 12:13

2 Answers2

1

After more deeper researching I managed to do it, so I'll answer to myself and leave it here for anybody who needs it.

If you want to show your application in foreground when the notification is triggered you should modify the AbstractTriggerReceiver.java

I added this method in AbstractTriggerReceiver.java:

public void launchApp() {
        Context context = getContextForApp();
        String pkgName  = context.getPackageName();

        Intent intent = context
                .getPackageManager()
                .getLaunchIntentForPackage(pkgName);

        intent.addFlags(
                Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP);

        context.startActivity(intent);
    }

So in your TriggerReceiver.java you can call launchApp(); in your onTrigger() method.

JPinios
  • 463
  • 1
  • 5
  • 10
0

There isn't an easy solution for this, since you need to work mainly in the native layer.

E.g. In Android you should create an activity visible over the default lock screen, show it when the notification is triggered and start the Cordova activity on a button click.

Community
  • 1
  • 1
lifeisfoo
  • 15,478
  • 6
  • 74
  • 115
  • If you think that this is the correct answer to your question, accept it. So others could find a solution to the same problem. – lifeisfoo Sep 11 '15 at 23:21