为啥在 Android 的 Geoloqi API 中不调用 onPushMessageReceived()?

Posted

技术标签:

【中文标题】为啥在 Android 的 Geoloqi API 中不调用 onPushMessageReceived()?【英文标题】:Why onPushMessageReceived() is not called in Geoloqi API in Android?为什么在 Android 的 Geoloqi API 中不调用 onPushMessageReceived()? 【发布时间】:2012-05-28 06:14:22 【问题描述】:

在我的 android 应用程序中,我使用最新的 Geoloqi API 来实现地理围栏概念。当用户进入他通知的某个区域时,为此我使用 Push Notifications.in GeoReceiver 类三个回调方法正在调用但不是 onPushMessageReceived() .请帮我怎么做? 我正在使用当前位置创建触发器是否需要手动进入区域,或者因为我已经在该位置它没有调用? 注意:我在 assets/geoloqi.properties 文件中提供了所需的凭据。当应用程序在 logcat 中启动时“已成功注册 C2DM 服务”消息也会显示。我的代码:

GeoloqiExampleActivity.java

public class GeoloqiExampleActivity extends Activity
    String TAG = "Geoloqi Example";

     private LQService mService;
     private boolean mBound;
     GeoReceiver geoReceiver = new GeoReceiver();
    @Override
    public void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Intent intent = new Intent(this, LQService.class);
        startService(intent);
    


    @Override
    public void onResume() 
        super.onResume();

        // Bind to the tracking service so we can call public methods on it
        Intent intent = new Intent(this, LQService.class);
        bindService(intent, mConnection, 0);

        // Wire up the sample location receiver
        final IntentFilter filter = new IntentFilter();
        filter.addAction(GeoReceiver.ACTION_LOCATION_CHANGED);
        filter.addAction(GeoReceiver.ACTION_TRACKER_PROFILE_CHANGED);
        filter.addAction(GeoReceiver.ACTION_LOCATION_UPLOADED);
        filter.addAction(GeoReceiver.ACTION_PUSH_MESSAGE_RECEIVED);
        registerReceiver(geoReceiver, filter);

    

    @Override
    public void onPause() 
        super.onPause();

        // Unbind from LQService
        if (mBound) 
            unbindService(mConnection);
            mBound = false;
        
        // Unregister our location receiver
        unregisterReceiver(geoReceiver);
    
    public void sendRequest() 

         // Performing a Trigger POST request
        if (mService != null) 
            LQSession session = mService.getSession();
            LQTracker tracker = mService.getTracker();
            tracker.setSession(session);
            // Build your request
            JSONObject trigger = new JSONObject();
            try 
                trigger.put("text", "Popcornapps");
                trigger.put("type", "message");
                trigger.put("latitude", 17.42557068);
                trigger.put("longitude",  78.42022822);
                trigger.put("radius", 500);
                trigger.put("place_name", "Banjara Hills");
             catch (JSONException e) 
               Log.d(TAG, e.getMessage());
            

            // Send the request
            session.runPostRequest("trigger/create", trigger, new OnRunApiRequestListener() 
                @Override
                public void onSuccess(LQSession session, HttpResponse response) 

                    Toast.makeText(GeoloqiExampleActivity.this, "Success", Toast.LENGTH_SHORT).show();
                    try 
                        BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
                        StringBuilder s = new StringBuilder();
                        String sResponse;
                        while ((sResponse = reader.readLine()) != null) 
                            s = s.append(sResponse);
                        
                    String result = s.toString().trim();
                    Log.d("On success Result", result);

                     catch (UnsupportedEncodingException e) 
                        e.printStackTrace();
                     catch (IllegalStateException e) 
                        e.printStackTrace();
                     catch (IOException e) 
                        e.printStackTrace();
                    
                
                @Override
                public void onFailure(LQSession session, LQException e) 
                    Log.e(TAG, e.getMessage());
                    Toast.makeText(GeoloqiExampleActivity.this, "Fail", Toast.LENGTH_LONG).show();
                
                @Override
                public void onComplete(LQSession session, HttpResponse response, StatusLine status) 
                    Toast.makeText(GeoloqiExampleActivity.this, "Complete", Toast.LENGTH_LONG).show();
                
            );
         else
            Toast.makeText(this, "service null", Toast.LENGTH_LONG).show();
        
    

     /** Defines callbacks for service binding, passed to bindService() */
    private ServiceConnection mConnection = new ServiceConnection() 
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) 
            try 
                // We've bound to LocalService, cast the IBinder and get LocalService instance.
                LQBinder binder = (LQBinder) service;
                mService = binder.getService();
                mBound = true;
                sendRequest();//Sending API Request
             catch (ClassCastException e) 
            
        

        @Override
        public void onServiceDisconnected(ComponentName name) 
            mBound = false;
        
    ;

GeoReceiver.java

public class GeoReceiver extends LQBroadcastReceiver 

    @Override
    public void onLocationChanged(Context arg0, Location arg1) 
        Toast.makeText(arg0, "Loc Changed ", Toast.LENGTH_SHORT).show();
    

    @Override
    public void onPushMessageReceived(Context context, Bundle data) 
        Toast.makeText(context, "Push Msg Received ", Toast.LENGTH_LONG).show();

    

    @Override
    public void onLocationUploaded(Context arg0, int arg1) 
        Toast.makeText(arg0, "Location Uploaded ", Toast.LENGTH_SHORT).show();
    

    @Override
    public void onTrackerProfileChanged(Context arg0, LQTrackerProfile oldp,
            LQTrackerProfile newp) 
        Toast.makeText(arg0, "onTrackerProfileChanged ",Toast.LENGTH_SHORT).show();
    

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.pop.geoloqi"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />

    <permission
        android:name="com.pop.geoloqi.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.pop.geoloqi.permission.C2D_MESSAGE" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >

         <activity
            android:name=".GeoloqiExampleActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service
            android:name="com.geoloqi.android.sdk.service.LQService"
            android:exported="false" />

        <receiver
            android:name=".GeoReceiver"
            android:enabled="false"
            android:exported="false" >
            <intent-filter>
                <action android:name="com.geoloqi.android.sdk.action.LOCATION_CHANGED" />
            </intent-filter>
        </receiver>
        <receiver
            android:name="com.geoloqi.android.sdk.receiver.LQDeviceMessagingReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

                <category android:name="com.pop.geoloqi" />
            </intent-filter>
        </receiver>

    </application>
</manifest>

【问题讨论】:

如果您使用的是 wifi,请确保它没有防火墙。检查的简单方法是查看 gtalk 是否能够让您登录。请确保您使用的是 android 2.2+ 设备并安装了市场应用程序并正确配置了一个 gmail id。 @KKD 我已经检查过 wifi 没有防火墙。设备是安装了一个 gmail id 的 Android 3.1 市场应用程序。 您可以登录 gtalk 吗?联系 c2dm 服务器后,您的服务器是否收到 STATUS_OK 消息? 是的,我可以直接登录 gtalk..我没有与 c2dm 它的 geologi api 通信..请访问以下链接一次。 developers.geoloqi.com/android/push-notifications 那我没用过geoloqi恐怕帮不上什么忙。不过,这很棒。 【参考方案1】:

Geoloqi Android SDK 的早期版本中存在错误。如果你更新到latest version这个问题应该可以解决。

【讨论】:

以上是关于为啥在 Android 的 Geoloqi API 中不调用 onPushMessageReceived()?的主要内容,如果未能解决你的问题,请参考以下文章

在 Android 12/API 31 中,地理围栏不适用于 IMMUTABLE 未决意图。为啥?

为啥我的 API 和 JSON 应用无法在 Android Studio 中运行

为啥合并排序用于 Android/Java API 中的对象?

为啥在android中调用新浪微博接口发表的gif图片不动?

为啥在android中调用新浪微博接口发表的gif图片不动?

PhoneGap - 为啥 Android 虚拟设备不显示更改?