c2dmの登録IDと認証トークンを持っています。そして、これらの値をdbにパスストアします。phpを使用して、1つのメッセージをc2dmサーバーに送信できます。しかし、私の問題は、アプリケーションでメッセージを受信する方法がわからないことです。メッセージの取得方法が正しいかどうかわかりません。とにかく私はそれを以下に与えます。
登録インテントを使用してc2dmに登録するアクティビティが1つあります。reg_idと通知メッセージを受信する1つの受信者。c2dmに登録しており、メッセージを受信していません。
マニフェスト
<intent-filter>
<action android:name="com.google.android.c2dm.intent.REGISTRATION"></action>
<category android:name="my.android.c2dm"></category>
</intent-filter>
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE"></action>
<category android:name="my.android.c2dm"></category>
</intent-filter>
</receiver>
</application>
C2dmRegistration.class(アクティビティ)
Intent objRegIntnet=new Intent("com.google.android.c2dm.intent.REGISTER");
objRegIntnet.putExtra("app",PendingIntent.getBroadcast(this,0,new Intent(),0));
objRegIntnet.putExtra("sender","mymail@gmail.com");
startService(objRegIntnet);
c2dmReceiver
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"))
{
handleRegistration(context, intent);
}
else if (intent.getAction().equals("com.google.android.c2dm.intent.RECEIVE"))
{
handleMessage(context, intent);
}
}
private void handleRegistration(Context context, Intent intent)
{
//handles registeration
}
private void handleMessage(Context context, Intent intent)
{
String title= intent.getStringExtra("title");
String message= intent.getStringExtra("msg");
Toast.makeText(context,"title : "+title+"\n message : "+message,1).show();
//Do whatever you want with the message
}
私が犯した間違いは何ですか...
アップデート
みなさん、こんにちは。今日、同じコードが私を悩ませています。私が犯した間違いはphpコードです。値をPOSTとして渡すのではなく、GETとして送信しました。POSTに変更すると、トーストメッセージが表示されます。しかし、まだいくつかの問題があります。
ここでは、titleとmsgの値はnullです。私のphpコードは:
function sendMessageToPhone($authCode, $deviceRegistrationId, $msgType, $messageText)
{
//$messageText="have a nice day";
//$msgtype="important";
$headers = array('Authorization: GoogleLogin auth=' . $authCode);
$data = array(
'registration_id' => $deviceRegistrationId,
'collapse_key' => $msgType,
'data.message' => $messageText
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://android.apis.google.com/c2dm/send");
if ($headers)
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
実際、collapse_key変数とdata.message変数にどのタイプの値を使用すべきかわかりません。
助けてください...ありがとう...