14

I am using Cordova / Phonegap iBeacon plugin with ionicframework at my cordova project. I am tryin to send a local notification both on android and ios with cordova local notification plugin while entering monitored region , when the app is killed.

Here is my code :

document.addEventListener("deviceready", onDeviceReady, false);

    function didDetermineStateForRegion(pluginResult) {
    }

    function didStartMonitoringForRegion (pluginResult) {
    }
    function didExitRegion(pluginResult) {
        $cordovaLocalNotification.add({
        id: 30244234234,
        title: "Good By!",
        text: "Hope to see you again."
            }).then(function () {
            });
    }

    function didEnterRegion (pluginResult) {
        $cordovaLocalNotification.add({
        title: "Welcome",
        text: "Tap to launch app"
            }).then(function () {

            });

    };
    function didRangeBeaconsInRegion (pluginResult) {

    }

    function onDeviceReady() {
        // Now safe to use device APIs
        function createBeacon(uuid,nofiyState) {

            var uuid = uuid; // mandatory
            var identifier = 'estimote'; // mandatory

            // throws an error if the parameters are not valid
            var beaconRegion = new cordova.plugins.locationManager.BeaconRegion(identifier, uuid);
            beaconRegion.notifyEntryStateOnDisplay = true;
            return beaconRegion;
        }
        var delegate = new cordova.plugins.locationManager.Delegate();
        delegate.didDetermineStateForRegion = didDetermineStateForRegion;

        delegate.didStartMonitoringForRegion = didStartMonitoringForRegion;

        delegate.didRangeBeaconsInRegion = didRangeBeaconsInRegion;
        delegate.didEnterRegion = didEnterRegion;
        delegate.didExitRegion = didExitRegion;

        var beaconRegion = createBeacon('02681445-8D1B-4F58-99D4-B25F4B129A58',true);
        // var beaconRegionBlue = createBeacon('02681445-8D1B-4F58-99D4-B25F4B129A58',1,,true);
        cordova.plugins.locationManager.setDelegate(delegate);

        // required in iOS 8+
        //cordova.plugins.locationManager.requestWhenInUseAuthorization();
        cordova.plugins.locationManager.requestAlwaysAuthorization();
        cordova.plugins.locationManager.startMonitoringForRegion(beaconRegion)
        .fail(console.error)
        .done();

    }

cordova plugins :

com.unarin.cordova.beacon 3.3.0 "Proximity Beacon Plugin"
de.appplant.cordova.plugin.local-notification 0.8.1 "LocalNotification"
nl.x-services.plugins.socialsharing 4.3.16 "SocialSharing"
org.apache.cordova.console 0.2.13 "Console"
org.apache.cordova.device 0.3.0 "Device"

cordova version : 4.3.0

this works fine for ios even if the app is killed but on android notifications cames only if app in the background. When i kill the app from task manager on android i never seen any local notification.

Is it possible to receive notification on android even if app is killed ?

thanks for help.

bviale
  • 5,245
  • 3
  • 28
  • 48
semirturgay
  • 4,151
  • 3
  • 30
  • 50
  • If you kill the App via Settings->App->MyApp->Force Stop then the app and all of tis processes are stopped even the one listening the background. Otherwise you need background service in Android to get the message/local notification. – Morrison Chang May 04 '15 at 22:31
  • 1
    I dont think you can get notifications after you have killed the app through task manager. I have a similar application using ionic framework and cordova, To get notifications from the app even when it is running on the background, I am using a cordova background mode plugin at https://github.com/katzer/cordova-plugin-background-mode . It will allow you to run your logic even when the app is not running actively on the foreground. – nthapa May 05 '15 at 05:51
  • @MorrisonChang i never force the app to stop like you said; i just stop it by long press home button that is i mean by saying task manager. – semirturgay May 05 '15 at 06:20
  • @nthapa13 without any plugin i already get local notifiactions when app is on background the ibeacon plugin already has ability to run. why you said i can not get notification after i killed the app ? – semirturgay May 05 '15 at 06:27
  • Could You please share the project with me in Dropbox or somewhere else. – Vinod Kumar Marupu Apr 15 '16 at 12:37

1 Answers1

4

lets clear some stuff , there are states that yo are confusing :

  1. App as a service
  2. App running in background (i.e minimized).
  3. App killed (not running at all)

in all cases the 3rd state when you kill app ( via long press back button in custom roms , or force stop from app menu in your OS ) , the app is simply removed from memory , no code is being excuted !

what usually is done in this case is automatically relaunching the service after it has been stopped check this answer , and as you can read :

it is really very bad pattern to run service forcefully against the user's willingness.

there are so many cordova plugins to create BroadcasteReceiver , however the simple answer to your question , if app is killed it is not possible to receive notification .

But you should consider this: if user kills your app , it means it was done intentionally , so you shouldnt really worry if your app will work or not , as this is the user's issue , and not yours as a developer.

Community
  • 1
  • 1
ProllyGeek
  • 15,517
  • 9
  • 53
  • 72
  • first of all thank you for your answer.I know it is a bad pattern but as you know it is an ibeacon app so what is the meaning of using beacons if i can not send a welcome and some campaign notifications even if the users phone is in his or her packet. now i am working on creating my own service as soon as i found the solution i will share it. – semirturgay May 10 '15 at 11:01
  • @semirturgay glad to help you , but it is not about creating your own service or not , as service can be killed exactly like killling the app , what if user kills your service ? – ProllyGeek May 10 '15 at 13:55
  • 1
    @ProllyGeek I disagree. What about push notifications? They are designed to send users notifications even if the app is closed (=not running in background). Local notifications can execute too if they were scheduled, even if the app is killed. The same should be possible for listening for ibeacon signals. If a user doesnt want to receive an alert when in range, it should revoke the notification permission of the app. – binoculars Jan 22 '16 at 13:45
  • @binoculars I made a lof of researches on that but i did not find any solution.that seems to me impossible right now, So i decide to move with only ios app. – semirturgay Jan 22 '16 at 13:50
  • @binoculars As a user if I kill an app ( not close it) , this would be as if I uninstall this app temporarily , it is not about what I think , or what you think , these are terms and conditions for android OS , please check this question for GCM http://stackoverflow.com/questions/19402104/gcm-terms-and-conditions – ProllyGeek Jan 22 '16 at 14:38
  • @semirturgay why dont you just make a listener service , and never worry about being killed , I think even facebook didnt worry about such an issue. – ProllyGeek Jan 22 '16 at 14:41
  • @ProllyGeek Are you saying it is possible to send a local notification when in range of a beacon, even if the app is closed (in Cordova)? With closed I mean swiped away (so not in background anymore). Cause Ive never seen anyone do this using Cordova. How would this work? – binoculars Jan 22 '16 at 21:29
  • @binoculars yes indeed , try swiping your facebook app from background apps , and you will still get notified , simply there is a background service ( that handles notifications ) and this service can only be stopped from app manager , if you are using cordova check this out :https://github.com/phpsa/cbsp – ProllyGeek Jan 23 '16 at 02:15
  • @ProllyGeek Hmm... looks like a though nut to crack https://github.com/petermetz/cordova-plugin-ibeacon/issues/141 – binoculars Jan 23 '16 at 08:38
  • @binoculars oh .. sounds a bigger issue cause it involves ibeacon Bluetooth , not just a running background service , but interesting , if I had time I could contribute , and help with solving that issue. Wish I could. – ProllyGeek Jan 23 '16 at 13:30
  • @ProllyGeek I have neither the time nor the skills unfortunately. – binoculars Jan 23 '16 at 16:58