Red-Folder / bgs-core

Core code for the Cordova Background Service
Other
236 stars 105 forks source link

Plugin not working. #65

Closed Voyager2718 closed 7 years ago

Voyager2718 commented 7 years ago

I would like to use create background services for my Ionic 2 app. So that I choose this plugin.

In my com.red_folder.phonegap.plugin.backgroundservice folder, I have

/aidl
/src/android

And in /src/android, I have /beaconListener and a bunch of original background service .java files.

So I wrote BeaconService.java which extends BackgroundService in /beaconListener.

Then I require it by using

var serviceName = 'com.red_folder.phonegap.plugin.backgroundservice.beaconListener.BeaconService';
var factory = cordova.require('com.red_folder.phonegap.plugin.backgroundservice.BackgroundService');
this.myService = factory.create(serviceName);

in my corresponding TypeScript file.

However, I found that cordova.require() returns nothing all the time. So that I can't get the object that I need.

What's wrong with my background service? Why couldn't I get the object that I want?

Red-Folder commented 7 years ago

How have you installed it? Have you installed with the "cordova plugin add ...." command?

It sounds from the above like it cannot find the plugin files - that maybe because they are in the wrong location in the project (should be ok if you used the cordova plugin add command because it handles it all for you).

If you haven't already, have a go with the sample background service plugin - that should help make sure everything is wired up correctly. See https://github.com/Red-Folder/bgs-core/wiki/Using-the-MyService-Sample

Hope that helps

Voyager2718 commented 7 years ago

Of course I have.

The problem is: when I change to cordova.require('com.red_folder.phonegap.plugin.backgroundservice.backgroundService');, Chrome gives me app.bundle.js:34510 EXCEPTION: Error: Uncaught (in promise): module com.red_folder.phonegap.plugin.backgroundservice.backgroundService not found.

But when I use cordova.require('com.red_folder.phonegap.plugin.backgroundservice.BackgroundService');, there's no problem but cordova.require still returns nothing.

Voyager2718 commented 7 years ago

Here's the structure of my folders. screenshot from 2016-07-29 15-55-30 What's more, I've added <service> to my AndroidManifest.xml as well as I've created my own service that extends BackgroundService.

Voyager2718 commented 7 years ago

What may be the cause of the problem? Because I really followed your sample project.

Red-Folder commented 7 years ago

Ok, do you have your code available in a public repo?

When you say require returns nothing - do you mean null or undefined?

It's been a while since I've used the plugin - but my sample isn't prefixing require with cordova:

var serviceName = 'com.red_folder.phonegap.plugin.backgroundservice.sample.MyService';
var factory = require('com.red_folder.phonegap.plugin.backgroundservice.BackgroundService');
module.exports = factory.create(serviceName);
Voyager2718 commented 7 years ago

Actually I'm using it on Inoic 2. Does your project support Ionic 2?

Voyager2718 commented 7 years ago

Here's my testing project. https://github.com/Voyager2718/test_cordova_background_service

Voyager2718 commented 7 years ago

Could you please see how can I use your work in my Ionic 2 project?

Red-Folder commented 7 years ago

I've no experience with Ionic 2. I'd suggest you have a search on Google for advice (if any) on using Cordova Plugins with Ionic. I had previously believed they would just work - but having never used Ionic then I could provide any confidence.

One comment I do have from reading the above; you talk about Chrome. I assume you are using Chrome to develop & test your web app. Please keep in mind that you will not be able to use BGS within Chrome - it is expecting an Android environment to run the Java - this is not possible in Chrome. I doubt that this is your current problem - as you should still get the plugin - you just wouldn't be able to use it,

Red-Folder commented 7 years ago

just reading your code (index.html) - it has the following:

<!-- cordova script (this will be a 404 during development) -->
<script src="cordova.js"></script>

Is the comment being put in by Ionic? Does this mean that cordova isn't available during development (ie. in Chrome as you seem to be working in).

As I say I have zero experience with Ionic, but this would seem to suggest that cordova isn't available during development. And if cordova isn't available, then neither will be the plugins.

Red-Folder commented 7 years ago

One final comment;

I cannot actually see where in the above reference project you are doing anything with the plugin. You appear to installed BGS-Core - but then haven't created a background service to use it.

I'd expect a .js and .java file at minimum which is your background implementation.

Am I missing it? (I only did a quick skim)

Voyager2718 commented 7 years ago

Actually I put my Java files here: https://github.com/Voyager2718/test_cordova_background_service/tree/master/plugins/com.red_folder.phonegap.plugin.backgroundservice/src/android

Is that correct? Do you have a sample project that allows me to have a look? Because I followed your tutorial, but it seems not work for me.

Voyager2718 commented 7 years ago

I used Chrome to inspect App that runs on my device. Because Ionic actually runs on WebView, so that Chrome can inspect them. However, it seems doesn't work for me.

Red-Folder commented 7 years ago

BGS-Core will never work in Chrome. It needs the Android environment

In terms of sample project, use bgs-sample - details of how to use are explained here -> https://github.com/Red-Folder/bgs-core/wiki/Using-the-MyService-Sample

Voyager2718 commented 7 years ago

Finally it works on my device. But another problem appears.

I would like to detect iBeacons and send notifications when have detected an iBeacon that we had provided. I don't know how can I import related libraries such as import android.bluetooth.BluetoothAdapter; or import android.app.Notification;.