2012-04-08 42 views
0

请帮助我,我不能独自解决这个问题,我不接收任何广播信息 C2DM应该由广播我的注册ID发送给我,但我不接受任何C2DM不能有registration_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", "[email protected]"); 
     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> 

非常感谢你对我的帮助

+0

我不确定是否是原因e,但是你的清单文件中不应该有''这个实际上是'Activity'的条目你有一个用于'android:name =“。NotifyMeActivity”'的活动和一个接收者。 – Squonk 2012-04-08 22:42:26

回答

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> 
+0

非常感谢你!!!!!!! – poloh11 2012-04-09 08:29:50