[英]send device to device gcm notification
有人可以指向一個鏈接,我可以在其中輕松地了解如何將GCM用於設備到設備的通知。 使設備成為服務器和接收器需要什么。
讓我解釋一下我的要求,你們可能會更好地解決我的問題
我想在一個按鈕單擊時向一個或一組人發送通知(預定義消息),其他用戶也可以這樣做。
謝謝
讓我嘗試說明您需要的基本工作-一台服務器和兩部手機
步驟1您有兩部電話都連接到的服務器。
步驟2前往此鏈接的 Google Developers頁面
步驟3在此處創建一個新項目,然后將GCM API添加到您的項目中。 保存項目的sender_id 。 還要檢查API的憑據,並添加新的瀏覽器密鑰並保存該密鑰。
步驟4將GCM添加到您的項目。 為此,轉到安裝Eclipse的根目錄。 然后導航到extras/google/google-play-services/libsproject/
文件夾,然后將那里的文件夾復制到另一個位置。 然后將該文件夾導入Eclipse中的工作區,並更改其屬性以使其成為庫。 然后將此添加到您的應用程序項目。
步驟5您需要在GCM中注冊您的手機。 為此,您可以使用這樣的示例代碼
// to start process
public void registerDevice() {
// TODO Auto-generated method stub
try {
if (checkPlayServices()) {
regid = getRegistrationId();
if (regid.isEmpty()) { // creating a new reg id
registerInBackground();
} else
saveid();
} else{
}
} catch (Exception e) {
}
}
// to create a new id
private void registerInBackground() {
// TODO Auto-generated method stub
try {
AsyncRegister logMeIn = new AsyncRegister("<you project id here>", context);
logMeIn.doneWith = this;
logMeIn.execute();
System.out.println("execute complete");
} catch (Exception e) {
}
}
// to fetch stored registration id
private String getRegistrationId() {
// TODO Auto-generated method stub
try {
int i=myPrefs.getInt("cuid", 0);
if(id!=i){
myPrefs.edit().putInt("cuid", id).commit();
int registeredVersion = myPrefs.getInt("appversion",
Integer.MIN_VALUE); // to check if app is updated
int currentVersion = getAppVersion();
if (registeredVersion != currentVersion) {
myPrefs.edit().putInt("appversion",currentVersion).commit();
}
return "";
}
String registrationId = myPrefs.getString("regid", "");
if (registrationId.isEmpty()) {
return "";
}
int registeredVersion = myPrefs.getInt("appversion",
Integer.MIN_VALUE); // to check if app is updated
int currentVersion = getAppVersion();
if (registeredVersion != currentVersion) {
myPrefs.edit().putInt("appversion", currentVersion).commit();
return "";
}
// when id is stored previously then this is called
return registrationId;
} catch (Exception e) {
}
}
//to fetch the current app version
private int getAppVersion() throws NameNotFoundException {
// TODO Auto-generated method stub
PackageInfo packageInfo = context.getPackageManager().getPackageInfo(
context.getPackageName(), 0);
return packageInfo.versionCode;
}
// to check if play services are there on the device
private boolean checkPlayServices() {
// TODO Auto-generated method stub
try {
int resultCode = GooglePlayServicesUtil
.isGooglePlayServicesAvailable(context);
if (resultCode != ConnectionResult.SUCCESS) {
if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
toSend = new Bundle();
//tell user google play needs to be updated
} else {
//tell user their system does not support GCM
}
myPrefs.edit().putBoolean("support", false).commit();
return false;
}
myPrefs.edit().putBoolean("support", true).commit();
return true;
} catch (Exception e) {
}
}
步驟6保存注冊ID,並將其傳遞到服務器以進行保存。
步驟7當用戶單擊發送按鈕時,將消息傳遞到您的服務器。 服務器將拉出與數據庫一起存儲的ID列表,然后將消息和這些ID一起推送到GCM。
步驟8現在,GCM會將該消息發送到為其提供ID的所有電話。
步驟9在您的應用程序中添加一個接收器,該接收器從GCM接收文本,然后將其顯示給用戶。
這是一個簡短的解釋。 如果您想詳細了解這一點,請閱讀在線教程。 另外,您也可以在我的論壇上給我發短信(您可以在我的個人資料描述中找到鏈接),我很樂意為您提供幫助。
String postData = "{ \"registration_ids\": [ \"" + OtherDeviceToken+ "\" ], " +
"\"delay_while_idle\": true, " +
"\"data\": {\"tickerText\":\"Your title\", "+
"\"contentTitle\":\"AppName\", " +
"\"message\": \" " + edtYourMessage.getText().toString() + "\"}}";
MediaType JSON= MediaType.parse("application/json; charset=utf-8");
RequestBody body = RequestBody.create(JSON, postData);
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("https://android.googleapis.com/gcm/send")
.addHeader("Content-Type", "application/json")
.addHeader("Authorization", "key=" + SERVER_API_KEY)
.post(body)
.build();
執行okkhttp請求。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.