【问题标题】:GcmListenerService.onMessageReceived not calledGcmListenerService.onMessageReceived 未调用
【发布时间】:2015-08-29 11:35:18
【问题描述】:

我正在尝试从服务器获取消息到我的应用程序。 我有这个服务器 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!!!!!!!!!");
    }
}

谁能想到它为什么不工作?

【问题讨论】:

    标签: android service google-cloud-messaging


    【解决方案1】:

    我想您已经阅读了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


    【讨论】:

    • 谢谢。 1)丢失的线路应该在接收器或服务中? 2)你输入什么而不是签名?
    • 缺少的行在接收器中,并且“签名”不是占位符,它应该是:“签名”。在此处检查developer.android.com/intl/ru/guide/topics/manifest/… PS:给定代码中的唯一占位符是 your.package - 这应该是您的基本包,应该是 com.example.atheel.prayer
    • 我发布了一个编辑。如果问题仍然存在,请随时通过我的网站 martinvano.sk 与我联系。我相信我们可以以某种方式解决它。
    • @vanomart 我认为您仍在使用旧 API
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-01
    • 1970-01-01
    • 2018-01-09
    相关资源
    最近更新 更多