Android 未调用onMessageReceived()

Android 未调用onMessageReceived(),android,google-cloud-messaging,Android,Google Cloud Messaging,和其他人一样,我似乎也无法让GCM发挥作用。特别是,我不明白为什么执行没有到达GcmListenerService.onMessageReceived() 我遵循了官方文件,并(我认为)成功地做到了以下几点: 使用InstanceID获取注册令牌 将此令牌发送回本地Rails服务器进行存储 随后,使用相同的令牌使用GCM发送消息(无通知) 相关的代码位如下所示。如果有什么不对劲,请告诉我 AndroidManifest.xml <!-- GCM specific --> <

和其他人一样,我似乎也无法让GCM发挥作用。特别是,我不明白为什么执行没有到达GcmListenerService.onMessageReceived()

我遵循了官方文件,并(我认为)成功地做到了以下几点:

  • 使用InstanceID获取注册令牌
  • 将此令牌发送回本地Rails服务器进行存储
  • 随后,使用相同的令牌使用GCM发送消息(无通知)
相关的代码位如下所示。如果有什么不对劲,请告诉我

AndroidManifest.xml

<!-- GCM specific -->
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission android:name="com.gradians.prepwell.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />
<uses-permission android:name="com.gradians.prepwell.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>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.gradians.prepwell" />
</intent-filter>
</receiver>

<service android:name=".services.MyGcmListenerService" android:exported="false">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>

<service android:name=".services.MyInstanceIDListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID" />
</intent-filter>
</service>

<service android:name=".services.RegisterService"
android:exported="false" />
Rails代码

def post_potd
api_key = "AIz......-_Y"
gcm = GCM.new(api_key)
reg_ids = Device.all.map(&:gcm_token)
payload = { 
  data: { message: "Hello Dude!" }
}   
response = gcm.send reg_ids, payload 
render json: response, status: :ok 
end 
对上述Rails代码的JSON响应似乎正常(status_code=200,Response='success'等)。所以,我猜GCM对发布的请求没有问题

但在GCM集成中是否还有其他必须注意的常见陷阱?你怎么能检查它们呢

我认为我做的每件事都是对的。但我显然错过了一些东西。因此,任何见解/提示/建议都将非常有用

谢谢
Abhinav.

只要确保您发送的令牌也是正确的,请尝试字符串文字。几点澄清:logcat中是否有消息到达设备的指示?您是否能够在正在使用的设备上接收其他通知?你在另一台设备上试用过Android应用程序吗?事实上,一切都正常。问题不在代码中。事实上,所有问题都得到了正确的解决。问题不在代码中。测试时,我正处于网络死区。因此,当通知被发送到GCM时,即使在10-15分钟后,它们也无法到达我的设备。但当我外出时,我会收到信息/通知。总之,我学会了接受GCM到设备连接的怪癖。任何事情都不必瞬间发生。只要确保您发送的令牌也是正确的,请尝试字符串文字。几点澄清:logcat中是否有消息到达设备的指示?您是否能够在正在使用的设备上接收其他通知?你在另一台设备上试用过Android应用程序吗?事实上,一切都正常。问题不在代码中。事实上,所有问题都得到了正确的解决。问题不在代码中。测试时,我正处于网络死区。因此,当通知被发送到GCM时,即使在10-15分钟后,它们也无法到达我的设备。但当我外出时,我会收到信息/通知。总之,我学会了接受GCM到设备连接的怪癖。每件事都不必瞬间发生。
def post_potd
api_key = "AIz......-_Y"
gcm = GCM.new(api_key)
reg_ids = Device.all.map(&:gcm_token)
payload = { 
  data: { message: "Hello Dude!" }
}   
response = gcm.send reg_ids, payload 
render json: response, status: :ok 
end