在Eclipse中進行Android應用的推送通知設置,需要使用Google Cloud Messaging (GCM) 服務。以下是設置步驟:
1. 首先,需要在Google Cloud Console中創建一個項目并啟用GCM服務。然后,獲取項目的API密鑰(Server Key)。
2. 在Eclipse中,打開你的Android項目。
3. 添加GCM依賴項到項目的build.gradle
文件中:
dependencies {
implementation 'com.google.android.gms:play-services-gcm:17.0.0'
}
4. 在AndroidManifest.xml文件中,添加必要的權限和GCM服務聲明:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application ...>
...
<service android:name=".MyGcmListenerService" android:exported="false">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
<receiver android:name=".MyGcmBroadcastReceiver">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</receiver>
</application>
5. 創建一個新的類MyGcmListenerService
,繼承自GcmListenerService
,并實現onMessageReceived
方法來處理接收到的消息:
import com.google.android.gms.gcm.GcmListenerService;
import android.os.Bundle;
public class MyGcmListenerService extends GcmListenerService {
@Override
public void onMessageReceived(String from, Bundle data) {
// 處理接收到的消息
}
}
6. 創建一個新的類MyGcmBroadcastReceiver
,繼承自BroadcastReceiver
,并實現onReceive
方法來處理注冊和消息接收事件:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class MyGcmBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = "MyGcmBroadcastReceiver";
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals("com.google.android.c2dm.intent.REGISTRATION")) {
// 處理設備注冊事件
} else if (action.equals("com.google.android.c2dm.intent.RECEIVE")) {
// 處理接收到的消息事件
}
}
}
7. 在MainActivity
或其他適當的地方,初始化GCM客戶端并請求注冊ID:
import com.google.android.gms.gcm.GoogleCloudMessaging;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private GoogleCloudMessaging gcm;
private String regid;
private static final String PROJECT_NUMBER = "YOUR_PROJECT_NUMBER"; // 替換為你的項目編號
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button registerButton = findViewById(R.id.register_button);
registerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
registerInBackground();
}
});
}
private void registerInBackground() {
new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... params) {
String msg = "";
try {
if (gcm == null) {
gcm = GoogleCloudMessaging.getInstance(getApplicationContext());
}
regid = gcm.register(PROJECT_NUMBER);
msg = "Device registered, registration ID=" + regid;
// You should send the registration ID to your server over HTTP, so it can use GCM/HTTP or CCS to send messages to your app.
sendRegistrationIdToBackend();
// Persist the registration ID - no need to register again.
storeRegistrationId(context, regid);
} catch (IOException ex) {
msg = "Error :" + ex.getMessage();
}
return msg;
}
@Override
protected void onPostExecute(String msg) {
Toast.makeText(MainActivity.this, msg, Toast.LENGTH_LONG).show();
}
}.execute(null, null, null);
}
}
8. 最后,確保你的服務器能夠處理來自GCM的推送通知請求,并將它們發送到相應的設備。