未调用GcmListenerService.onMessageReceived

时间:2015-08-29 11:35:18

标签: android service google-cloud-messaging

我正试图从服务器获取一个msg到我的应用程序。 我有这个服务器URL,它向所有用户发送消息: https://taub-prayer-ramym.c9.io/send?message=123

我的应用程序的令牌已在服务器中注册...但我遇到的问题是我的GcmListenerService实现中的onMessageReceived()方法未被调用。

我的清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.atheel.prayer" >

    <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="21" />

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WAKE_LOCK"/>
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
    <uses-permission android:name="com.example.atheel.prayer.permission.C2D_MESSAGE"/>
    <uses-permission android:name="com.google.android.c2dm.permission.SEND"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

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

        <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.RECEIVE" />
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
                <category android:name="com.example.atheel.prayer" />
            </intent-filter>
        </receiver>
        <service
            android:name="com.example.atheel.prayer.MyGcmListenerService"
            android:exported="false" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            </intent-filter>
        </service>
        <service
            android:name="com.example.atheel.prayer.MyInstanceIDListenerService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.android.gms.iid.InstanceID"/>
            </intent-filter>
        </service>
        <service android:exported="false" android:name="com.example.atheel.prayer.RegistrationIntentService"/>

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

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".EmptyStatus"
            android:label="@string/title_activity_empty_status" >
        </activity>
        <activity
            android:name=".UnEmpty"
            android:label="@string/title_activity_un_empty" >
        </activity>
    </application>

</manifest>

我的myGCMListener:

public class MyGcmListenerService extends GcmListenerService {
    private static final String TAG = "MyGcmListenerService";
    File notificationsFile;
    private String sessionForAppClosed;

    // [START receive_message]
    @Override
    public void onMessageReceived(String from, Bundle data) {
        Log.i(TAG,"Message Received!!!!!!!!!");
        System.out.print("***************Message Received!!!!!!!!!");
    }
}

任何人都可以认为它为什么不起作用?

1 个答案:

答案 0 :(得分:2)

我认为您已经阅读了official documentation以获取GCM支持。我还建议你阅读this文章。

与我的GCM代码相比,您错过了

<action android:name="com.google.android.c2dm.intent.GCM_RECEIVED_ACTION"/> 
来自GcmReceiver类的

。我也没有看到关于gcm版本的元标记:

<meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version"/>

我做的第三件事是权限说明:

<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
    <permission
    android:name="your.package.permission.C2D_MESSAGE"
    android:protectionLevel="signature"/>

我还记得在某些网络上gcm根本不起作用。这很奇怪,但你可以阅读更多关于here的信息。

编辑:由于问题仍未解决,我在下面添加了用于接收GCM的工作代码。请注意,我认为该应用已在Google服务中成功注册,防火墙未被阻止且应用服务器成功将消息发送到Google服务器: GcmBroadcastReceiver - 用于接收CGM消息

public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    ComponentName comp = new ComponentName(context.getPackageName(),GcmIntentService.class.getName());
    startWakefulService(context, (intent.setComponent(comp)));
    setResultCode(Activity.RESULT_OK);
}

}

此组件调用以下服务

public class GcmIntentService extends IntentService {

public GcmIntentService() {
    super(App.getStr(R.string.gcm_sender_id)); // this integer is provided when registering the application 
}

@Override
protected void onHandleIntent(Intent intent) {
    Bundle extras = intent.getExtras();
    GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
    String messageType = gcm.getMessageType(intent);
    if (!extras.isEmpty()) {
        if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
            sendNotification(extras);
        } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
            sendNotification(extras);
        } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
            // here you can do your work
            }
        }
    }
    GcmBroadcastReceiver.completeWakefulIntent(intent);
}

}

PS:请注意,我的解决方案受到互联网上其他解决方案的启发,例如this one