本文介绍了无法接收gcm讯息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循此最新教程



但我的接收器没有收到gcm信息。



我非常肯定gcm发送成功,因为服务器端的响应是

  {
multicast_id:8234052701190658472,
success:1,
failure:0,
canonical_ids:0,
results:
[
{
message_id:0:1448357690225369%9aaa71e7f9fd7ecd
}
]
}

这是我的清单



 < uses-permission 
android:name =com.my.app.permission.C2D_MESSAGE/>

< receiver
android:name =com.google.android.gms.gcm.GcmReceiver
android:exported =true
android:permission =com。 google.android.c2dm.permission.SEND>
< intent-filter>
< category android:name =com.my.app/>
< / intent-filter>
< / receiver>
<! - [END gcm_receiver] - >

< service
android:name =com.my.app.MyGcmListenerService
android:exported =false
android:process =:remote>
< intent-filter>
< / intent-filter>
< / service>
<! - [END gcm_listener] - >
< service
android:name =com.my.app.MyInstanceIDListenerService
android:exported =false
android:process =:remote>
< intent-filter>
< / intent-filter>
< / service>
<! - [END instanceId_listener] - >

这是MyGcmListenerService.java。然而,它永远不会被调用。

  public class MyGcmListenerService extends GcmListenerService {

/ **
*收到消息时调用。
*
* @param from SenderID of sender。
* @param data包含消息数据作为键/值对的数据包。
*对于密钥集使用data.keySet()。
* /
// [START receive_message]
@Override
public void onMessageReceived(String from,Bundle data){
String message = data.getString(data );
Log.d(TAG,From:+ from);
Log.d(TAG,Message:+ message);
}
}

这是我的gcm依赖关系。

  compile'c​​om.android.support:support-annotations:23.0.1'
compile'c​​om.google.android.gms:play -services-gcm:8.3.0'
compile'c​​om.android.support:appcompat-v7:23.0.0'

为什么我的android应用程序没有收到gcm?

谢谢!

解决方案

以下代码适用于我

 <?xml version =1.0encoding = UTF-8 >?; 
< manifest xmlns:android =http://schemas.android.com/apk/res/android
package =com.push.gcm>

<使用权限android:name =android.permission.INTERNET/>
<使用权限android:name =android.permission.WAKE_LOCK/>
< uses-permission android:name =com.google.android.c2dm.permission.RECEIVE/>

< permission
android:name =com.push.gcm.permission.C2D_MESSAGE
android:protectionLevel =signature/>

<使用权限android:name =com.push.gcm.permission.C2D_MESSAGE/>

< application
android:allowBackup =true
android:icon =@ mipmap / ic_launcher
android:label =@ string / app_name
android:supportsRtl =true
android:theme =@ style / AppTheme>
< activity android:name =。activity.MainActivity>
< intent-filter>

< category android:name =android.intent.category.LAUNCHER/>
< / intent-filter>
< / activity>

< receiver
android:name =com.google.android.gms.gcm.GcmReceiver
android:exported =true
android:permission = com.google.android.c2dm.permission.SEND >
< intent-filter>
< category android:name =com.push.gcm/>
< / intent-filter>
< intent-filter>
< category android:name =com.push.gcm/>
< / intent-filter>
< / receiver>

< service
android:name =。services.GCMInstanceIDListenerService
android:exported =false>
< intent-filter>
< / intent-filter>
< / service>
< service
android:name =。services.GCMListenerService
android:exported =false>
< intent-filter>
< / intent-filter>
< / service>
< / application>

< / manifest>


I follow this most recent tutorial https://github.com/codepath/android_guides/wiki/Google-Cloud-Messaging

But my receiver does not receive the gcm message.

I am pretty sure that gcm is sent successfully, since the response from server side is

{
 "multicast_id":8234052701190658472,
 "success":1,
 "failure":0,
 "canonical_ids":0,
 "results":
     [
      {
       "message_id":"0:1448357690225369%9aaa71e7f9fd7ecd"
      }
     ]
 }

Here is my manifest

<uses-permission
    android:name="com.my.app.permission.C2D_MESSAGE" />

    <!-- [START gcm_receiver] -->
    <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.my.app" />
        </intent-filter>
    </receiver>
    <!-- [END gcm_receiver] -->

    <!-- [START gcm_listener] -->
    <service
        android:name="com.my.app.MyGcmListenerService"
        android:exported="false"
        android:process=":remote" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        </intent-filter>
    </service>
    <!-- [END gcm_listener] -->
    <!-- [START instanceId_listener] -->
    <service
        android:name="com.my.app.MyInstanceIDListenerService"
        android:exported="false"
        android:process=":remote" >
        <intent-filter>
            <action android:name="com.google.android.gms.iid.InstanceID"/>
        </intent-filter>
    </service>
    <!-- [END instanceId_listener] -->

Here is MyGcmListenerService.java. However, it never be called.

public class MyGcmListenerService extends GcmListenerService {

    /**
     * Called when message is received.
     *
     * @param from SenderID of the sender.
     * @param data Data bundle containing message data as key/value pairs.
     *             For Set of keys use data.keySet().
     */
     // [START receive_message]
     @Override
     public void onMessageReceived(String from, Bundle data) {
         String message = data.getString("data");
         Log.d(TAG, "From: " + from);
         Log.d(TAG, "Message: " + message);
     }
}

Here is my gcm dependencies.

compile 'com.android.support:support-annotations:23.0.1'
compile 'com.google.android.gms:play-services-gcm:8.3.0'
compile 'com.android.support:appcompat-v7:23.0.0'

Why my android app does not receive gcm?

Thank you!

解决方案

Below code works for me

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

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

    <permission
        android:name="com.push.gcm.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.push.gcm.permission.C2D_MESSAGE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".activity.MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <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" />
                <category android:name="com.push.gcm" />
            </intent-filter>
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
                <category android:name="com.push.gcm" />
            </intent-filter>
        </receiver>

        <service
            android:name=".services.GCMInstanceIDListenerService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.android.gms.iid.InstanceID" />
            </intent-filter>
        </service>
        <service
            android:name=".services.GCMListenerService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            </intent-filter>
        </service>
    </application>

</manifest>

这篇关于无法接收gcm讯息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 23:42