Android 即使gcm服务器成功发送消息,也无法从服务器接收gcm通知

Android 即使gcm服务器成功发送消息,也无法从服务器接收gcm通知,android,Android,它有一个onReceive方法,可以识别接收到的消息 I have developed my application server that sends the gcm message to the gcm server using http post. When i run my server it gives a response code of 200 that means that the message has been sent successfully by gcm server,

它有一个onReceive方法,可以识别接收到的消息

I have developed my application server that sends the gcm message to the gcm server using http post. When i run my server it gives a response code of 200 that means that the message has been sent successfully by gcm server, but the notification is not displayed on the android device.

I get the registration id of the device successfully and also it is stored in mysql database using httppost correctly.


Here is my C2DMMessageReceiver:
package com.example.cloud2devicemessageing;
导入android.app.Activity;
导入android.app.Notification;
导入android.app.NotificationManager;
导入android.app.pendingent;
导入android.content.BroadcastReceiver;
导入android.content.ComponentName;
导入android.content.Context;
导入android.content.Intent;
导入android.support.v4.content.WakefulBroadcastReceiver;
导入android.util.Log;
公共类C2DMMessageReceiver扩展了WakefulBroadcastReceiver{
@凌驾
公共void onReceive(上下文、意图){
//TODO自动生成的方法存根
String action=intent.getAction();
Log.w(“C2DM”,“呼叫的消息接收器”);
if(“com.google.android.c2dm.intent.RECEIVE.equals(action)){
Log.w(“C2DM”,“收到的消息”);
ComponentName comp=新的ComponentName(context.getPackageName(),
gcminentservice.class.getName());
//启动服务,在设备启动时保持设备处于唤醒状态。
startWakefulService(上下文,(intent.setComponent(comp));
setResultCode(Activity.RESULT\u OK);
}
}
}
处理接收到的消息的gcminentservice.java:
包com.example.cloud2devicemessageing;
导入android.app.Notification;
导入android.app.NotificationManager;
导入android.app.pendingent;
导入android.content.Context;
导入android.content.Intent;
导入android.util.Log;
导入com.google.android.gcm.GCMBaseIntentService;
公共类GCMinentService扩展了GCMBaseintService{
受保护的GCMinentService(字符串senderId){
超级(“1087507874278”);
//TODO自动生成的构造函数存根
}
@凌驾
受保护的void onError(上下文arg0,字符串arg1){
//TODO自动生成的方法存根
Log.d(“错误”,arg1);
}
@凌驾
受保护的void onMessage(上下文、意图){
//TODO自动生成的方法存根
int icon=R.drawable.eplogo;
长时间=System.currentTimeMillis();
字符串消息=intent.getExtras().toString();
NotificationManager NotificationManager=(NotificationManager)
getSystemService(context.NOTIFICATION\u服务);
通知通知=新通知(图标、消息、时间);
字符串title=“电子通行证申请”;
Intent notificationIntent=新意图(上下文,resetpass.class);
//设置意图,使其不会启动新活动
notificationIntent.setFlags(Intent.FLAG\u活动\u清除\u顶部|
意图。标记活动(单个顶部);
悬而未决=
getActivity(context,(int)(Math.random()*100),notificationIntent,0);
通知。SetLateStevenInfo(上下文、标题、消息,in);
notification.flags |=notification.FLAG_AUTO_CANCEL;
notification.defaults |=notification.DEFAULT_振动;
notificationManager.notify(0,通知);
}
@凌驾
已注册受保护的void(上下文arg0,字符串arg1){
//TODO自动生成的方法存根
Log.i(“移动设备注册”,“注册成功”);
}
@凌驾
未注册受保护的void(上下文arg0,字符串arg1){
//TODO自动生成的方法存根
Log.i(“注销”、“移动设备注销”);
}
}
这是使用httppost发送消息的我的应用程序服务器的代码:
包com.example.gcmserver;
导入java.io.BufferedReader;
导入java.io.IOException;
导入java.io.InputStreamReader;
导入java.util.ArrayList;
导入java.util.List;
导入org.apache.http.HttpException;
导入org.apache.http.HttpRequest;
导入org.apache.http.HttpRequestInterceptor;
导入org.apache.http.HttpResponse;
导入org.apache.http.NameValuePair;
导入org.apache.http.auth.AuthScope;
导入org.apache.http.auth.AuthState;
导入org.apache.http.auth.Credentials;
导入org.apache.http.auth.UsernamePasswordCredentials;
导入org.apache.http.client.CredentialsProvider;
导入org.apache.http.client.entity.UrlEncodedFormEntity;
导入org.apache.http.client.methods.HttpPost;
导入org.apache.http.client.protocol.ClientContext;
导入org.apache.http.impl.auth.BasicScheme;
导入org.apache.http.impl.client.DefaultHttpClient;
导入org.apache.http.message.BasicNameValuePair;
导入org.apache.http.protocol.HttpContext;
公共类服务器{
公共静态void main(字符串[]args)引发IOException{
字符串url=”https://android.googleapis.com/gcm/send";
DefaultHttpClient=新的DefaultHttpClient();
client.getCredentialsProvider().setCredentials(AuthScope.ANY,新用户名密码凭据(“用户名”、“密码”));
client.addRequestInterceptor(新的HttpRequestInterceptor(){
@凌驾
公共无效进程(HttpRequest arg0,HttpContext上下文)
抛出HttpException,IOException{
//TODO自动生成的方法存根
AuthState=(AuthState)context.getAttribute(ClientContext.TARGET\u AUTH\u state);
if(state.getAuthScheme()==null){
基本方案=新的基本方案();
CredentialsProvider=(CredentialsProvider)context.getAttribute(ClientContext.CREDS\u PROVIDER);
Credentials Credentials=credentialsProvider.getCredentials(AuthScope.ANY);
如果(凭据==null){
抛出新的HttpException();
}
state.setAuthScope(AuthScope.ANY);
state.setAuthScheme(scheme);
state.setCredentials(凭证);
}}
}, 0); 
HttpPost HttpPost=新的HttpPost(“https://android.googleapis.com/gcm/send");
package com.example.cloud2devicemessaging;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.WakefulBroadcastReceiver;
import android.util.Log;

public class C2DMMessageReceiver extends WakefulBroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        String action = intent.getAction();
         Log.w("C2DM", "Message Receiver called");
            if ("com.google.android.c2dm.intent.RECEIVE".equals(action)) {
              Log.w("C2DM", "Received message");

              ComponentName comp = new ComponentName(context.getPackageName(),
                        GCMIntentService.class.getName());

                // Start the service, keeping the device awake while it is launching.
                startWakefulService(context, (intent.setComponent(comp)));
                setResultCode(Activity.RESULT_OK);
    }

}

    }


GCMIntentService.java that handles the message received:

package com.example.cloud2devicemessaging;


import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.google.android.gcm.GCMBaseIntentService;

public class GCMIntentService extends GCMBaseIntentService {

    protected GCMIntentService(String senderId) {
        super("1087507874278");
        // TODO Auto-generated constructor stub
    }

    @Override
    protected void onError(Context arg0, String arg1) {
        // TODO Auto-generated method stub
        Log.d("Error", arg1);

    }

    @Override
    protected void onMessage(Context context, Intent intent) {

        // TODO Auto-generated method stub
        int icon = R.drawable.eplogo;
        long when = System.currentTimeMillis();
        String message=intent.getExtras().toString(); 

        NotificationManager notificationManager = (NotificationManager)
                context.getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = new Notification(icon, message, when);

        String title = "E-PASS APPLICATION";

        Intent notificationIntent = new Intent(context, resetpass.class);
        // set intent so it does not start a new activity
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
                Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent in =
                PendingIntent.getActivity(context,(int) (Math.random() * 100), notificationIntent, 0);
        notification.setLatestEventInfo(context, title, message, in);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;


        notification.defaults |= Notification.DEFAULT_VIBRATE;
        notificationManager.notify(0, notification);     

    }

    @Override
    protected void onRegistered(Context arg0, String arg1) {
        // TODO Auto-generated method stub
     Log.i("Mobile device registered","Registration successful");
    }

    @Override
    protected void onUnregistered(Context arg0, String arg1) {
        // TODO Auto-generated method stub
 Log.i("Unregister","Mobile  device unregistered");
    }

}


This is the code of my application server that sends message using httppost:

package com.example.gcmserver;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpException;
import org.apache.http.HttpRequest;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.AuthState;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HttpContext;

public class Server {
    public static void main(String[] args) throws IOException {

        String url = "https://android.googleapis.com/gcm/send";

        DefaultHttpClient client = new DefaultHttpClient();
        client.getCredentialsProvider().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("username", "password"));
        client.addRequestInterceptor(new HttpRequestInterceptor() {


    @Override
    public void process(HttpRequest arg0, HttpContext context)
            throws HttpException, IOException {
        // TODO Auto-generated method stub
          AuthState state = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
             if (state.getAuthScheme() == null) {
                 BasicScheme scheme = new BasicScheme();
                 CredentialsProvider credentialsProvider = (CredentialsProvider) context.getAttribute(ClientContext.CREDS_PROVIDER);
                 Credentials credentials = credentialsProvider.getCredentials(AuthScope.ANY);
                 if (credentials == null) {
                     throw new HttpException();
                 }
                 state.setAuthScope(AuthScope.ANY);
                 state.setAuthScheme(scheme);
                 state.setCredentials(credentials);
    }}
 }, 0); 








          HttpPost httppost = new HttpPost("https://android.googleapis.com/gcm/send");

          List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
          urlParameters.add(new BasicNameValuePair("data.registration id","APA91bGChJVdx2L1AUIJEVIBBVEkxEAk6zixj5V-L--fDGXudvrtl5gSQDgAP4iUJoMRq57sLSpGtsSQ5gnprX4McOPq7WxglKeQyTmaysVb0r-adOZ4tTjWG2MYwVEayFKy9Rwf1mloVt2jb-_izUdN-SfKvlpUpEYXNNT1987tYASKCcam16g"));




          urlParameters.add(new BasicNameValuePair("data.title", "e-pass application"));
          urlParameters.add(new BasicNameValuePair("data.message", "Your code is 13133"));


          httppost.setHeader("Authorization",
                  "key=AIzaSyDANlJHGPmzo1so_yyqkYZO7LSrQpnR7mk");
          httppost.setHeader("Content-Type",
                  "application/x-www-form-urlencoded;charset=UTF-8");

          httppost.setEntity(new UrlEncodedFormEntity(urlParameters, "UTF-8"));

          HttpResponse response = client.execute(httppost);
          System.out.println("Response Code : " 
                      + response.getStatusLine().getStatusCode());

          BufferedReader rd = new BufferedReader(
                  new InputStreamReader(response.getEntity().getContent()));

          StringBuffer result = new StringBuffer();
          String line = "";
          while ((line = rd.readLine()) != null) {
              result.append(line);
          }
}
}


And finally this is my manifest file in which i have added all the necessary permissions:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.cloud2devicemessaging"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />
<permission 
    android:name="com.example.cloud2devicemessaging.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="com.google.android.c2dm.permission.SEND" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" 
        android:protectionLevel="signature"/>
    <uses-permission android:name="android.permission.VIBRATE"/>
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.WAKE_LOCK"/>
    <uses-permission android:name="com.example.cloud2devicemessaging.permission.C2D_MESSAGE"/>

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

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

        <receiver android:name=".C2DMRegistrationReceiver" 
            android:permission="com.google.android.c2dm.permission.SEND" >
  <intent-filter>

    <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
    <category android:name="com.example.cloud2devicemessaging" />
  </intent-filter>
</receiver>
        <activity android:name="resetpass"></activity>
        <activity android:name="SendingMessage"></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>

                <category android:name="om.example.cloud2devicemessaging" />
            </intent-filter>
        </receiver>
        <service android:name=".GCMIntentService" />


    </application>

</manifest>


Thanks in advance for the help
public class GcmIntentService extends IntentService {
    public static final int NOTIFICATION_ID = 1;
    private NotificationManager mNotificationManager;
    NotificationCompat.Builder builder;

    public GcmIntentService() {
        super("GcmIntentService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Bundle extras = intent.getExtras();
        GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
        // The getMessageType() intent parameter must be the intent you received
        // in your BroadcastReceiver.
        String messageType = gcm.getMessageType(intent);

        if (!extras.isEmpty()) {  // has effect of unparcelling Bundle
            /*
             * Filter messages based on message type. Since it is likely that GCM
             * will be extended in the future with new message types, just ignore
             * any message types you're not interested in, or that you don't
             * recognize.
             */
            if (GoogleCloudMessaging.
                    MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
                sendNotification("Send error: " + extras.toString());
            } else if (GoogleCloudMessaging.
                    MESSAGE_TYPE_DELETED.equals(messageType)) {
                sendNotification("Deleted messages on server: " +
                        extras.toString());
            // If it's a regular GCM message, do some work.
            } else if (GoogleCloudMessaging.
                    MESSAGE_TYPE_MESSAGE.equals(messageType)) {
                // This loop represents the service doing some work.
                for (int i=0; i<5; i++) {
                    Log.i(TAG, "Working... " + (i+1)
                            + "/5 @ " + SystemClock.elapsedRealtime());
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                    }
                }
                Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime());
                // Post notification of received message.
                sendNotification("Received: " + extras.toString());
                Log.i(TAG, "Received: " + extras.toString());
            }
        }
        // Release the wake lock provided by the WakefulBroadcastReceiver.
        GcmBroadcastReceiver.completeWakefulIntent(intent);
    }

    // Put the message into a notification and post it.
    // This is just one simple example of what you might choose to do with
    // a GCM message.
    private void sendNotification(String msg) {
        mNotificationManager = (NotificationManager)
                this.getSystemService(Context.NOTIFICATION_SERVICE);

        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                new Intent(this, DemoActivity.class), 0);

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.ic_stat_gcm)
        .setContentTitle("GCM Notification")
        .setStyle(new NotificationCompat.BigTextStyle()
        .bigText(msg))
        .setContentText(msg);

        mBuilder.setContentIntent(contentIntent);
        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
    }
}