首页 > 解决方案 > Ionic 2 地理围栏上的后台服务不会触发

问题描述

我正在开发一个需要地理围栏在后台运行的 Ionic 应用程序。我正在使用cordova-plugin-geofence插件。我了解 JavaScript 不在后台运行,并且插件开发人员提供了本机代码来收听地理围栏转换。

我按照说明进行操作,但似乎不起作用。关闭应用程序时不会触发转换。

这是我的包名:io.ionic.pla.

我将此代码放在我的 Android 清单中

        <receiver android:name="io.ionic.pla.TransitionReceiver">
            <intent-filter>
                <action android:name="com.cowbell.cordova.geofence.TRANSITION" />
            </intent-filter>
        </receiver>

这是我的 TransitionReceiver.java

            package io.ionic.pla;

            import android.content.BroadcastReceiver;
            import android.content.Context;
            import android.content.Intent;
            import android.util.Log;

            import com.cowbell.cordova.geofence.Gson;
            import com.cowbell.cordova.geofence.GeoNotification;

            public class TransitionReceiver extends BroadcastReceiver {

                @Override
                public void onReceive(Context context, Intent intent) {
                    String error = intent.getStringExtra("error");

                    if (error != null) {
                        //handle error
                        Log.println(Log.ERROR, "YourAppTAG", error);
                    } else {
                        String geofencesJson = intent.getStringExtra("transitionData");
                        Log.println(Log.INFO, "xxxYourAppTAG", geofencesJson);
                        GeoNotification[] geoNotifications = Gson.get().fromJson(geofencesJson, GeoNotification[].class);
                        //handle geoNotifications objects
                    }
                }
            }

请帮助大家,我真的需要这个工作。

标签: javaionic2geofencingandroid-geofence

解决方案


推荐阅读