简体   繁体   English

自定义GcmListenerService无法正常工作

[英]Custom GcmListenerService not working

This is my manifest file 这是我的清单文件

<?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>

And this is the service that gets/registers my registration id; 这是获取/注册我的注册ID的服务;

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

MyGcmListenerService; 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"));
    }
}

The server uses PushJack and sends to the registration id's. 服务器使用PushJack并发送到注册ID。 I can see that the server can send the messages, I can get my registration id and register it with our server, but the onMessageReceived never gets called... I also made the google-services.json using the link on the website, so that should also be okay. 我可以看到服务器可以发送消息,我可以获取我的注册ID并将其注册到我们的服务器,但onMessageReceived永远不会被调用...我也使用网站上的链接制作了google-services.json,所以那也应该没问题。

Any or all ideas are welcome. 欢迎任何或所有想法。

You're missing 你错过了

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

in your permissions. 在你的权限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM