2012-03-21 21 views
0

私はc2dmのコードを試しました&登録IDを取得できましたが、c2dmからメッセージが届かない人がいます。c2dmのメッセージを取得中にエラーが発生しました

私のメインクラスは

マイレシーバクラスは

public class C2DMReceiver extends C2DMBaseReceiver { 
    public C2DMReceiver() { 
     // Email address currently not used by the C2DM Messaging framework 
     super("[email protected]"); 
    } 

    public void onRegistered(Context context, String registrationId) 
      throws java.io.IOException { 
     Log.e("C2DM", "Registration ID received"); 
     Log.e("C2DM", registrationId); 
     Intent intent = new Intent(context, ResultActivity.class); 
     intent.putExtra("message", "Registration ID received"); 
     intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     context.startActivity(intent); 
    }; 
    protected void onMessage(Context context, Intent intent) { 
      Log.e("C2DM", "Neue Message."); 
     Intent resultIntent = new Intent(context, ResultActivity.class); 
     resultIntent.putExtra("message", "Message received"); 
     resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     context.startActivity(intent); 
    } 

    public void onError(Context context, String errorId) { 
     Log.e("C2DM", "Error occured!!!"); 
     Log.e("C2DM", errorId); 
    } 

} 

public class ResultActivity extends Activity { 
    TextView view; 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.result); 
     Bundle extras = getIntent().getExtras(); 
     if (extras != null) { 
      String string = extras.getString("message"); 
      view = (TextView) findViewById(R.id.c2dm); 
      view.setText(string); 
     } 
    } 
} 

で、ここで私のマニフェストファイルである私の登録クラスがされ

public class RegisterActivity extends Activity { 
    EditText text; 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
    } 

    public void register(View view) { 
     Log.e("Super", "Starting registration"); 
     Toast.makeText(this, "Starting", Toast.LENGTH_LONG).show(); 
     text = (EditText) findViewById(R.id.editText1); 
     C2DMessaging.register(this, text.getText().toString()); 
    } 
} 

ある

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

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

    <uses-permission android:name="com.sample.permission.C2D_MESSAGE" /> 
    <!-- Permissions --> 
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.WAKE_LOCK" /> 

    <application 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" > 
     <activity 
      android:name="RegisterActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

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

     <service android:name=".C2DMReceiver" /> 

     <!-- 
       Only C2DM servers can send messages for the app. If permission is 
       not set - any other app can generate it 
     --> 
     <receiver 
      android:name="com.google.android.c2dm.C2DMBroadcastReceiver" 
      android:permission="com.google.android.c2dm.permission.SEND" > 

      <!-- Receive the actual message --> 
      <intent-filter> 
       <action android:name="com.google.android.c2dm.intent.RECEIVE" /> 

       <category android:name="com.sample" /> 
      </intent-filter> 
      <!-- Receive the registration id --> 
      <intent-filter> 
       <action android:name="com.google.android.c2dm.intent.REGISTRATION" /> 

       <category android:name="com.sample" /> 
      </intent-filter> 
     </receiver> 
     <activity android:name="ResultActivity" > 
     </activity> 
    </application> 

</manifest> 

答えて

0

RegisterActivityクラスに空のコンストラクタを追加してみてください、それは次のようになります。

public class RegisterActivity extends Activity { 
    EditText text; 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
    } 

public RegisterActivity() 
{ 

} 

を...

レジスタのサービスを開始するために必要とされます。

+0

私は自分のコードにこれを追加しましたが、それでもActivityコンテキストの外側からstartActivity()を呼び出すと例外がFLAG_ACTIVITY_NEW_TASKフラグを必要と示す働いていません。 – user1196969

0
package Utils; 

import java.io.IOException; 
import java.io.OutputStream; 
import java.net.URL; 
import java.net.URLEncoder; 
import javax.net.ssl.HostnameVerifier; 
import javax.net.ssl.HttpsURLConnection; 
import javax.net.ssl.SSLSession; 
import android.util.Log; 

public class MessageUtil 
{ 
public final static String AUTH = "authentication"; 

public static final String UPDATE_CLIENT_AUTH = "Update-Client-Auth"; 

public static final String PARAM_REGISTRATION_ID = "registration_id"; 

public static final String PARAM_DELAY_WHILE_IDLE = "delay_while_idle"; 

public static final String PARAM_COLLAPSE_KEY = "collapse_key"; 

private static final String UTF8 = "UTF-8"; 

public static int sendMessage(String auth_token, String registrationId,String message) throws IOException 
{ 

    StringBuilder postDataBuilder = new StringBuilder(); 

    postDataBuilder.append(PARAM_REGISTRATION_ID).append("=").append(registrationId); 
    postDataBuilder.append("&").append(PARAM_COLLAPSE_KEY).append("=").append("0"); 
    postDataBuilder.append("&").append("data.payload").append("=").append(URLEncoder.encode(message, UTF8)); 

    byte[] postData = postDataBuilder.toString().getBytes(UTF8); 

    // Hit the dm URL. 

    URL url = new URL("https://android.clients.google.com/c2dm/send"); 
    HttpsURLConnection.setDefaultHostnameVerifier(new CustomizedHostnameVerifier()); 
    HttpsURLConnection conn = (HttpsURLConnection) url.openConnection(); 
    conn.setDoOutput(true); 
    conn.setUseCaches(false); 
    conn.setRequestMethod("POST"); 
    conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded;charset=UTF-8"); 
    conn.setRequestProperty("Content-Length",Integer.toString(postData.length)); 
    conn.setRequestProperty("Authorization", "GoogleLogin auth="+ auth_token); 

    OutputStream out = conn.getOutputStream(); 
    out.write(postData); 
    out.close(); 

    int responseCode = conn.getResponseCode(); 
    if (responseCode == 200) 
    { 
     Log.d("error",conn.toString()); 
     Log.d("response",conn.getResponseMessage()); 
    } 
    return responseCode; 
} 

private static class CustomizedHostnameVerifier implements HostnameVerifier { 
    public boolean verify(String hostname, SSLSession session) { 
     return true; 
    } 
} 
} 
    //call the method from other activity 
    int responseCode = MessageUtil.sendMessage(token,registration_id, "your message for device"); 
     System.out.println(responseCode); 
関連する問題