无法使用GCM在设备上接收消息 [英] can't receive messages on device using GCM

查看:75
本文介绍了无法使用GCM在设备上接收消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循google关于启动gcm服务器和客户端的说明。
我的问题是,虽然我从服务器获得了成功的gcm:

lockquote
MulticastResult(multicast_id = 8287827393174436535,total = 1,success = 1,失败= 0,canonical_ids = 0,结果:
[[messageId = 0:1366468335181772%8e1522ac00000031]]

我无法看到在客户端接收消息的任何动作 - onMessage()未被调用。
客户端ID以及服务器ID是正确的,据我所知。



我会很感激任何帮助...:)

服务器端代码: p>

  try {

Sender sender = new Sender(API_KEY);
Message message = new Message.Builder()。collapseKey(1)
.timeToLive(3)
.delayWhileIdle(true)
.addData(message,
这个文本会在通知栏中看到!!)。build();

ArrayList< String> devices = new ArrayList< String>();

devices.add(DEVICE_ID1);
//devices.add(DEVICE_ID2);

MulticastResult result = sender.send(message,devices,2);
// Result result = sender.send(message,DEVICE_ID1,2);
System.out.println(result.toString());
if(result.getResults()!= null){
int canonicalRegId = result.getCanonicalIds();
if(canonicalRegId!= 0){
}
} else {
int error = result.getFailure();
System.out.println(error);

} catch(Exception e){
// TODO:处理异常
}

客户端代码
注册:

  GCMRegistrar.checkDevice(本); 
GCMRegistrar.checkManifest(this);
final String regId = GCMRegistrar.getRegistrationId(this);
if(regId.equals()){
GCMRegistrar.register(this,SENDER_ID); //注意:从配置中获取发件人ID。
String regIdString = GCMRegistrar.getRegistrationId(this);
Log.v(TAG,RegisterId,regId:+ regIdString);
} else {

Log.v(TAG,已经注册,regId:+ regId);

和gcmIntentServiceCreated(部分):

  public class GCMIntentService extends GCMBaseIntentService {
public GCMIntentService(){
super(SENDER_ID);

覆盖$ b $保护无效onMessage(上下文上下文,意图意图){
Log.i(Registration,Got a message!);
Log.i(Registration,context.toString()++ intent.toString());
}
...
}

客户端清单:

 <?xml version =1.0encoding =utf-8?> 
< manifest xmlns:android =http://schemas.android.com/apk/res/android
package =com.example.myapp
android:versionCode = 1
android:versionName =1.0>

< uses-sdk
android:minSdkVersion =8
android:targetSdkVersion =17/>
< permission android:protectionLevel =signatureandroid:name =com.example.myapp.permission.C2D_MESSAGE>< / permission>
< uses-permission android:name =com.example.myapp.permission.C2D_MESSAGE/>
<! - 应用程序接收GCM消息。 - >
< uses-permission android:name =com.google.android.c2dm.permission.RECEIVE/>
<! - GCM连接到Google服务。 - >
< uses-permission android:name =android.permission.INTERNET/>
<! - GCM需要Google帐户。 - >
<使用权限android:name =android.permission.GET_ACCOUNTS/>
<! - 保持处理器在收到消息时不会睡眠。 - >
<使用权限android:name =android.permission.WAKE_LOCK/>

< application
android:allowBackup =true
android:icon =@ drawable / ic_launcher
android:label =@ string / app_name
android:theme =@ style / AppTheme>
< activity
android:name =com.example.myapp.MainActivity
android:label =@ string / app_name>
< intent-filter>

< category android:name =android.intent.category.LAUNCHER/>
< / intent-filter>
< / activity>
< receiver android:name =com.google.android.gcm.GCMBroadcastReceiver
android:permission =com.google.android.c2dm.permission.SEND>
< intent-filter>
< category android:name =com.example.myapp/>
< / intent-filter>
< / receiver>
< service android:name =。GCMIntentService/>
< / application>

< / manifest>


解决方案

为了最大化接收邮件的机会,在3秒内不设置time_to_live。我会完全忽略此参数,以便它采用默认值4周。我还会将delay_while设置为false,以便电话即使在空闲时也能收到消息。


i've followed google instruction on initiating a gcm server and client. my problem is though i get a successful gcm from server :

MulticastResult(multicast_id=8287827393174436535,total=1,success=1,failure=0,canonical_ids=0,results: [[ messageId=0:1366468335181772%8e1522ac00000031 ]]

i'm not able to see any movement of receiving a message on the client - onMessage() isn't called. client id as well as server id are correct as far as i can tell.

i'd appreciate any help... :)

the server side code :

try {

Sender sender = new Sender(API_KEY);
Message message = new Message.Builder().collapseKey("1")
        .timeToLive(3)
        .delayWhileIdle(true)
        .addData("message",
                "this text will be seen in notification bar!!").build();

ArrayList<String> devices = new ArrayList<String>();

devices.add(DEVICE_ID1); 
//devices.add(DEVICE_ID2);

MulticastResult result = sender.send(message, devices, 2);
//Result result = sender.send(message, DEVICE_ID1, 2);
System.out.println(result.toString());
if (result.getResults() != null) {
    int canonicalRegId = result.getCanonicalIds();
    if (canonicalRegId != 0) {
    }
} else {
    int error = result.getFailure();
    System.out.println(error);
}
} catch (Exception e) {
    // TODO: handle exception
}

the client code : registering :

GCMRegistrar.checkDevice(this);
        GCMRegistrar.checkManifest(this);
        final String regId = GCMRegistrar.getRegistrationId(this);
        if (regId.equals("")) {
          GCMRegistrar.register(this, SENDER_ID); // Note: get the sender id from configuration.
          String regIdString = GCMRegistrar.getRegistrationId(this);
          Log.v(TAG, "RegisterId, regId: " + regIdString);
        } else {

          Log.v(TAG, "Already registered, regId: " + regId);
        }

and gcmIntentServiceCreated (partly) :

public class GCMIntentService extends GCMBaseIntentService {
public GCMIntentService(){
        super(SENDER_ID);       
    }
@Override
    protected void onMessage(Context context, Intent intent) {
         Log.i("Registration", "Got a message!");
             Log.i("Registration", context.toString() + " " + intent.toString());
    }
...
}

client's manifest :

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <permission android:protectionLevel="signature" android:name="com.example.myapp.permission.C2D_MESSAGE"></permission>
    <uses-permission android:name="com.example.myapp.permission.C2D_MESSAGE"/>
    <!-- App receives GCM messages. -->
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <!-- GCM connects to Google Services. -->
    <uses-permission android:name="android.permission.INTERNET" /> 
    <!-- GCM requires a Google account. -->
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <!-- Keeps the processor from sleeping when a message is received. -->
    <uses-permission android:name="android.permission.WAKE_LOCK" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.myapp.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>
        <receiver android:name="com.google.android.gcm.GCMBroadcastReceiver" 
            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.myapp" />
          </intent-filter>
        </receiver>
        <service android:name=".GCMIntentService" />
    </application>

</manifest>

解决方案

To maximise your chances of receiving the message, I would not set the time_to_live at a mere 3 seconds. I'd omit this parameter altogether so that it takes the default value of 4 weeks. I would also set delay_while to false, so that the phone gets the message even whilst idle.

这篇关于无法使用GCM在设备上接收消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆