Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cordova ibeacon; Send local notification after the app got killed, but does not work on android

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.

like image 750
semirturgay Avatar asked Apr 29 '15 10:04

semirturgay


1 Answers

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.

like image 175
ProllyGeek Avatar answered Oct 13 '22 01:10

ProllyGeek