can't receive messages on device using GCM

浪子不回头ぞ 提交于 2020-01-14 19:18:28

问题


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>

回答1:


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.




回答2:


Well your message is surely getting sent to your device since gcm sends a success status of 1. Try to check the GCMReceiver code of your app.




回答3:


Change the following code in your onMessage method:

Log.i("Registration", "Got a message!");
             Log.i("Registration", context.toString() + " " + intent.toString());

to this:

String message=intent.getStringExtra("message"); 
Log.w("The message received is", message);

hope this help



来源:https://stackoverflow.com/questions/16121573/cant-receive-messages-on-device-using-gcm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!