0

请帮助我我自己无法解决这个问题,我没有收到任何广播信息 C2DM 应该通过广播我的注册 ID 发送给我,但我没有收到任何信息

这里是java文件

NotifyMeActivity.java

package com.notifyme;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;


public class NotifyMeActivity extends Activity {

    public static final String PUSH_ENABLED_PREF_KEY = "pushEnabled";
    private SharedPreferences prefs;
    Button button_enable_push;//initialisation bouttons enable et disable push
    Button button_disable_push;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        button_enable_push = (Button)findViewById(R.id.button_enable_push);
        button_disable_push = (Button)findViewById(R.id.button_disable_push);

      //On attribut un écouteur d'évènement à tout les boutons
        button_enable_push.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                registerForC2dm();
            }
        });
        button_disable_push.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                unregisterFromC2dm();
            }
        });

    }
    private void registerForC2dm() {
        Log.i("C2DM","register");
        Intent registrationIntent = new Intent("com.google.android.c2dm.intent.REGISTER");
        registrationIntent.putExtra("app", PendingIntent.getBroadcast(this, 0, new Intent(), 0));
        registrationIntent.putExtra("sender", "notifme@gmail.com");
        startService(registrationIntent);


    }

    private void unregisterFromC2dm() {
        Log.i("C2DM","unregister");
        Intent unregistrationIntent = new Intent("com.google.android.c2dm.intent.UNREGISTER");
        unregistrationIntent.putExtra("app", PendingIntent.getBroadcast(this, 0, new Intent(), 0));
        super.startService(unregistrationIntent);

    }

}

C2DMReceiver.java

package com.notifyme;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

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


public class C2DMReceiver extends BroadcastReceiver {
    private static String KEY = "c2dmPref";
    private static String REGISTRATION_KEY = "registrationKey";

    private Context context;
    @Override
    public void onReceive(Context context, Intent intent) {
        this.context = context;
        if (intent.getAction().equals("com.google.android.c2dm.intent.REGISTRATION")) {
            Log.d("c2dm", "handleRegistration");
            handleRegistration(context, intent);
        } else if (intent.getAction().equals("com.google.android.c2dm.intent.RECEIVE")) {
            Log.d("c2dm", "handleMessage");
            handleMessage(context, intent);
        }
     }

    private void handleRegistration(Context context, Intent intent) {
        String registration = intent.getStringExtra("registration_id");
        if (intent.getStringExtra("error") != null) {
            // Registration failed, should try again later.
            Log.d("c2dm", "registration failed");
            String error = intent.getStringExtra("error");
            if(error == "SERVICE_NOT_AVAILABLE"){
                Log.d("c2dm", "SERVICE_NOT_AVAILABLE");
            }else if(error == "ACCOUNT_MISSING"){
                Log.d("c2dm", "ACCOUNT_MISSING");
            }else if(error == "AUTHENTICATION_FAILED"){
                Log.d("c2dm", "AUTHENTICATION_FAILED");
            }else if(error == "TOO_MANY_REGISTRATIONS"){
                Log.d("c2dm", "TOO_MANY_REGISTRATIONS");
            }else if(error == "INVALID_SENDER"){
                Log.d("c2dm", "INVALID_SENDER");
            }else if(error == "PHONE_REGISTRATION_ERROR"){
                Log.d("c2dm", "PHONE_REGISTRATION_ERROR");
            }
        } else if (intent.getStringExtra("unregistered") != null) {
            // unregistration done, new messages from the authorized sender will be rejected
            Log.d("c2dm", "unregistered");

        } else if (registration != null) {
            Log.d("c2dm", registration);
            Editor editor =
                context.getSharedPreferences(KEY, Context.MODE_PRIVATE).edit();
            editor.putString(REGISTRATION_KEY, registration);
            editor.commit();
           // Send the registration ID to the 3rd party site that is sending the messages.
           // This should be done in a separate thread.
           // When done, remember that all registration is done.

            postData(registration,"polo");
        }
    }

    private void handleMessage(Context context, Intent intent)
    {
        //String app_name = (String)context.getText(R.string.app_name);
        String app_name = "Notify Me";
        String message =  intent.getStringExtra("message");

        // Use the Notification manager to send notification
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        // Create a notification using android stat_notify_chat icon. 
        Notification notification = new Notification(android.R.drawable.stat_notify_chat, app_name + ": " + message, 0);

        // Create a pending intent to call the HomeActivity when the notification is clicked
        PendingIntent pendingIntent = PendingIntent.getActivity(context, -1, new Intent(context, NotifyMeActivity.class), PendingIntent.FLAG_UPDATE_CURRENT); // 
        notification.when = System.currentTimeMillis();  
        notification.flags |= Notification.FLAG_AUTO_CANCEL; 
        // Set the notification and register the pending intent to it
        notification.setLatestEventInfo(context, app_name, message, pendingIntent); //

        // Trigger the notification
        notificationManager.notify(0, notification);
    }
    public void postData(String id,String error) {
        Log.i("TAG", "ENVOI Du post dATA");
        // On créé un client http
        HttpClient httpclient = new DefaultHttpClient();
        // On créé notre entête
        HttpPost httppost = new HttpPost("http://*******");

        try {
            // On ajoute nos données dans une liste
            List nameValuePairs = new ArrayList(2);

            // On ajoute nos valeurs ici un identifiant et un message
            nameValuePairs.add(new BasicNameValuePair("pseudo", error));
            nameValuePairs.add(new BasicNameValuePair("id", id));

            // Ajoute la liste à notre entête
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            // On exécute la requête tout en récupérant la réponse
            HttpResponse response = httpclient.execute(httppost);

            // On peut maintenant afficher la réponse
            Log.e("http réponse",response.toString());

        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
        } catch (IOException e) {
            // TODO Auto-generated catch block
        }
    }

}

还有我的清单

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

    <uses-sdk android:minSdkVersion="8" />

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

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

     <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".NotifyMeActivity"
            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=".NotifyMeActivity"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter >
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" >
                </action>

                <category android:name="com.notifyme" />
            </intent-filter>
        </receiver>
        <receiver
            android:name=".C2DMReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter >
                <action android:name="com.google.android.c2dm.intent.RECEIVE" >
                </action>

                <category android:name="com.notifyme" />
            </intent-filter>
        </receiver>

    </application>

</manifest>

非常感谢你帮助我

4

1 回答 1

1

从清单中删除这个块......

    <receiver
        android:name=".NotifyMeActivity"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter >
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" >
            </action>

            <category android:name="com.notifyme" />
        </intent-filter>
    </receiver>

然后将<intent-filter>上面的内容添加到您的.C2DMReceiver条目中......

    <receiver
        android:name=".C2DMReceiver"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter >
            <action android:name="com.google.android.c2dm.intent.RECEIVE" >
            </action>
            <category android:name="com.notifyme" />
        </intent-filter>
        <intent-filter >
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" >
            </action>
            <category android:name="com.notifyme" />
        </intent-filter>
    </receiver>
于 2012-04-08T22:45:54.560 回答