나는 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>
RegisterActivity
클래스에 빈 생성자를 추가
는 FLAG_ACTIVITY_NEW_TASK 플래그가 필요합니다. – user1196969