简体   繁体   English

GcmListenerService.onMessageReceived未调用

[英]GcmListenerService.onMessageReceived not called

I am trying to get a msg to my app from a server. 我正在尝试从服务器获取味精到我的应用程序。 I have this server url wich sends msg to all users: https://taub-prayer-ramym.c9.io/send?message=123 我有将此服务器URL发送至所有用户的消息: https ://taub-prayer-ramym.c9.io/send?message =123

my app's token is registered in the server... but I have a problem that the onMessageReceived() method from my GcmListenerService implementation isn't called. 我的应用程序的令牌已在服务器中注册...但是我有一个问题,就是我的GcmListenerService实现中的onMessageReceived()方法没有被调用。

my Manifest: 我的清单:

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

my myGCMListener : 我的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!!!!!!!!!");
    }
}

anyone can think why its not working? 任何人都可以想到为什么它不起作用?

I suppose that you already read the official documentation for GCM support. 我想您已经阅读了有关GCM支持的官方文档 I also suggest you to read this article. 我也建议您阅读这篇文章。

Compared to my GCM code, you are missing the line 与我的GCM代码相比,您错过了这一行

<action android:name="com.google.android.c2dm.intent.GCM_RECEIVED_ACTION"/> 

from class GcmReceiver. 来自GcmReceiver类。 I also don't see meta tag about the gcm version: 我也没有看到有关gcm版本的meta标签:

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

Third thing that I do differently is the specification of permission: 我做不同的第三件事是权限的规范:

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

I also remember that on some networks the gcm simply do not work. 我还记得在某些网络上gcm根本无法正常工作。 It's a weird thing, but you can read more about it here . 这是一件很奇怪的事情,但是您可以在这里阅读更多有关它的信息

Edit: since the issue is still not resolved, I add my working code for receiving GCM below. 编辑:由于问题仍未解决,因此我在下面添加了用于接收GCM的工作代码。 Note that I suppose that the app is successfully registered in google services, firewall is not blocked and the app server successfully send the message to google servers: GcmBroadcastReceiver - for receiving the CGM messages 请注意,我想该应用程序已成功在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);
}

} }

This component calls following service 该组件调用以下服务

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: note that my solution is heavily inspired by other solutions on the internet, for example this one PS:请注意,我的解决方案在很大程度上由互联网上其他解决方案的启发,比如这一个


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

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