2011-06-12 313 views
1

一切似乎正常工作,直到我嘗試推出通知;應用程序獲取註冊ID,並且服務器獲得Auth代碼,但顯然註冊ID無效。Android:C2DM無效註冊錯誤

當我從應用程序獲得註冊後運行PHP代碼,它返回Error=InvalidRegistration

我要發表我的所有代碼都在C2DM實現和服務器端捲曲的請求。

裏面的OnCreate:

Intent registrationIntent = new Intent("com.google.android.c2dm.intent.REGISTER"); 
registrationIntent.putExtra("app", PendingIntent.getBroadcast(this, 0, new Intent(), 0)); 
registrationIntent.putExtra("sender", "MYEMAIL"); 
startService(registrationIntent); 

C2DM類:

import java.net.URL; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.util.Log; 
import android.widget.Toast; 

public class C2DMReceiver extends BroadcastReceiver 
{ 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     String receiver = context.getPackageName() + ".C2DMReceiver"; 
     intent.setClassName(context, receiver); 
     context.startService(intent); 

     if (intent.getAction().equals("com.google.android.c2dm.intent.REGISTRATION")) { 
      handleRegistration(context, intent); 
     } else if (intent.getAction().equals("com.google.android.c2dm.intent.RECEIVE")) { 
      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. 
     } else if (intent.getStringExtra("unregistered") != null) { 
      // unregistration done, new messages from the authorized sender will be rejected 
     } else if (registration != null) { 
      // 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. 
      sendReg(registration, context); 
      Toast.makeText(context, registration, Toast.LENGTH_SHORT).show(); 
     } 
    } 

    private void handleMessage(Context context, Intent intent) 
    { 
    } 

    public void sendReg(String key, Context context) 
    { 
     //Send key to server, this works fine 
    } 
} 

PHP代碼:

<?php 
// get Auth from Google ClientLogin 
$new = shell_exec("curl -d accountType=HOSTED_OR_GOOGLE -d Email=MYEMAIL -d Passwd=MYPASSWORD -d service=ac2dm -d source=MYSOURCE -k https://www.google.com/accounts/ClientLogin | grep Auth="); 
$auth = substr($new, 5); // "DQAAAKY..." 

$fp = fopen('/home/me/Desktop/test.txt','w'); 
fputs($fp, $new."\nAuth: ".$auth."\n"); 

// ID is from the app 
$id = "APA91b..."; 
fputs($fp, "ID: ".$id."\n\n"); 

$msg = "hello"; 

// if I don't include ContentLength, Google tells me I need it 
$len = strlen("registration_id=".$id."&data.message=".$msg."&collapse_key=something"); 
fputs($fp, "Len: ".$len."\n\n"); 

$info = shell_exec("curl --header \"Content-Length: ".$len."\" --header \"Authorization: GoogleLogin auth=".$auth."\" -d registration_id=".$id." -d data.message=".$msg." -d collapse_key=something -k https://android.apis.google.com/c2dm/send"); 

fputs($fp, $info); // "Error=InvalidRegistration" 
fclose($fp); 
?> 

回答

0

你肯定整個數據包被髮送正確?嘗試在兩行中使用fputs,在其中使用shell_exec確保格式正確。我沒有看到除了你做錯之外的任何事情......

+0

哇,第二個shell_exec在它的中間有一個換行符,從我grep的授權碼。它現在工作:) – 2011-06-15 01:57:29