自定义GcmListenerService无法正常工作

时间:2016-02-05 15:11:49

标签: android google-cloud-messaging

这是我的清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="be.customapp" >
    <uses-permission android:name="android.permission.INTERNET" />
    <!-- GCM: keep device awake while receiving a notification -->
    <uses-permission android:name="android.permission.WAKE_LOCK"/>
    <!-- GCM: receive push notifications -->
    <permission android:name="be.customapp.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />
    <uses-permission android:name="be.customapp.permission.C2D_MESSAGE" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <!-- Meta data -->
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="@string/google_maps_key"/>

        <!-- Activities -->
        <activity
            android:name=".ui.activities.MainActivity"
            android:label="@string/app_name"
            android:launchMode="singleTop"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <!-- Services -->
        <service
            android:exported="false"
            android:name=".services.gcm.MyGcmListenerService">
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            </intent-filter>
        </service>
        <service
            android:exported="false"
            android:name=".services.gcm.GcmRegisterService"/>
        <service
            android:name=".services.gcm.MyInstanceIDListenerService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.android.gms.iid.InstanceID"/>
            </intent-filter>
        </service>


        <!-- Broadcastreceivers -->        
        <receiver
            android:name="com.google.android.gms.gcm.GcmReceiver"
            android:exported="true"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <category android:name="be.prior_it.evapp" />
            </intent-filter>
        </receiver>
    </application>

</manifest>

这是获取/注册我的注册ID的服务;

InstanceID instanceID = InstanceID.getInstance(this);
String token =      instanceID.getToken(getString(R.string.gcm_defaultSenderId),
                    GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);

MyGcmListenerService;

public class MyGcmListenerService extends GcmListenerService {

    @Override
    public void onMessageReceived(String from, Bundle data) {
        super.onMessageReceived(from, data);
        Log.i(Constants.LOG_TAG, "Got GCM message " + data.getString("message"));
    }
}

服务器使用PushJack并发送到注册ID。我可以看到服务器可以发送消息,我可以获取我的注册ID并将其注册到我们的服务器,但onMessageReceived永远不会被调用...我也使用网站上的链接制作了google-services.json,所以那也应该没事。

欢迎任何或所有想法。

1 个答案:

答案 0 :(得分:1)

你错过了

<uses-permission android:name="android.permission.INTERNET" />

在您的权限中。