我是否需要我的應用程序的服務器使用GCM(Google Cloud Messaging)在另一個Android手機上發送通知?如果不是,那麼我怎麼能發送通知到另一個Android手機?有人能解釋我GCM究竟是如何運作的!GCM(Google雲消息傳遞)如何工作?
-2
A
回答
1
對於GCM請參閱本教程,它可以幫助你,
http://androidv5.wordpress.com/2012/08/15/how-to-implement-google-cloud-messaging/
它工作正常,我。
1
使用下面的代碼GCMIntentService類在你的應用程序包
import java.util.Iterator;
import java.util.Set;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import com.google.android.gcm.GCMBaseIntentService;
public class GCMIntentService extends GCMBaseIntentService {
public ParseNotification parseNotification;
public String pushMessage = "", push_Count = "0", company_Name,
project_Name, config_Id, att_ID, push_Id, survey_Id, S_or_C,
systemName, company_Id;
int value;
public GCMIntentService() {
super(AppsData.SENDER_ID);
}
protected void onRegistered(Context context, String registrationId) {
AppsData.GCM_REGISTERED_ID = registrationId;
SharedPreferences settings = getSharedPreferences("GCM_ID",
MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putString("GCM_DEVICEID", registrationId);
editor.commit();
}
protected void onUnregistered(Context context, String registrationId) {
}
@Override
protected void onMessage(Context context, Intent intent) {
Log.e("Prashant", "Action : " + intent.getAction());
Set<String> set = intent.getExtras().keySet();
Iterator<String> iterator = set.iterator();
JSONObject object = new JSONObject();
while (iterator.hasNext()) {
String key = iterator.next();
Log.v("Prashant ==== ", key + " : " + intent.getStringExtra(key));
try {
object.put(key, intent.getStringExtra(key));
} catch (JSONException e) {
e.printStackTrace();
}
}
System.out.println(object);
parseNotification = new ParseNotification();
PushNotificationBean notification = ParseNotification
.parseNotification(object);
pushMessage = notification.getAlert();
push_Count = notification.getBadge();
company_Name = notification.getC_name();
project_Name = notification.getP_name();
config_Id = notification.getConf_id();
systemName = notification.getSystemName();
att_ID = notification.getAtt_id();
push_Id = notification.getPrid();
survey_Id = notification.getEid();
S_or_C = notification.getSc();
company_Id = notification.getCompany_Id();
AppsData.COMPANY_NAME = company_Name;
AppsData.PROJECT_NAME = project_Name;
AppsData.SYSTEM_NAME = systemName;
AppsData.ATTEND_ID = att_ID;
AppsData.CONFID_ID = config_Id;
AppsData.eventID = survey_Id;
AppsData.COMPANY_ID = company_Id;
value = Integer.parseInt(S_or_C);
SharedPreferences sharedPrefs = getSharedPreferences("IS_LOGGED_IN",
MODE_WORLD_READABLE);
} else {
if (AppsData.OFFLINE_DATA_FLAG == true) {
} else {
generateNotification(context, pushMessage);
}
}
@Override
protected void onDeletedMessages(Context context, int total) {
}
@Override
public void onError(Context context, String errorId) {
}
@Override
protected boolean onRecoverableError(Context context, String errorId) {
return super.onRecoverableError(context, errorId);
}
public void generateNotification(Context context, String message) {
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
int icon = R.drawable.ic_launcher;
CharSequence tickerText = message;
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
int pushCount = Integer.parseInt(push_Count);
notification.number = pushCount;
try {
} catch (Exception e) {
e.printStackTrace();
}
Intent intent = new Intent(context, AlertDialogActivity.class);
intent.putExtra("VALUE", value);
intent.putExtra("MESSAGE", pushMessage);
intent.putExtra("COMPANY_NAME", company_Name);
intent.putExtra("PROJECT_NAME", project_Name);
intent.putExtra("SYSTEM_NAME", systemName);
intent.putExtra("ATTEND_ID", att_ID);
intent.putExtra("CONFID_ID", config_Id);
intent.putExtra("EVENTID", survey_Id);
intent.putExtra("COMPANY_ID", company_Id);
intent.setAction("" + Math.random());
PendingIntent contentIntent = PendingIntent.getActivity(
getApplicationContext(), 0, intent,
PendingIntent.FLAG_CANCEL_CURRENT);
notification.flags = Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_SOUND;
notification.setLatestEventInfo(context, "EAG", message, contentIntent);
mNotificationManager.notify(0, notification);
}
}
中添加以下服務您的androidmanifest
<receiver
android:name="com.google.android.gcm.GCMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="Your app package" />
</intent-filter>
</receiver>
<receiver android:name="com.eag.utils.NetworkReciver" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
<service android:name=".GCMIntentService" />
和applay在此之前的代碼,請遵循點擊here
相關問題
- 1. Google雲消息傳遞(GCM)問題
- 2. 如何在Xamarin中使用Google雲消息傳遞(GCM)Android
- 3. Google雲消息傳遞
- 4. Android中的Google雲消息傳遞服務如何工作?
- 5. 谷歌雲消息傳遞(GCM)不能與火狐工作
- 6. Google雲消息傳遞消息隱私
- 7. GCM(Google雲消息傳遞)通過Linux批量發送
- 8. 什麼是Google雲消息傳遞(GCM)的AUTH-TOKEN?
- 9. Google雲消息傳遞(GCM)是否支持Kindle Fire?
- 10. 使用Android的Google雲消息傳遞(GCM)的基礎知識
- 11. Firebase(Google)雲消息傳遞(FCM,舊的GCM) - 檢查消息狀態
- 12. 如何通過Google雲消息傳遞(GCM)觸發推送通知?
- 13. Google雲消息傳遞:BroadcastReceiver還是Service?
- 14. Android Google雲消息傳遞概念
- 15. Android排球或Google雲消息傳遞
- 16. 帶Google雲消息傳遞的App Engine
- 17. Google雲消息傳遞的setRegisteredOnServer()函數。
- 18. Google雲消息傳遞業務設置
- 19. Google雲消息傳遞唯一密鑰
- 20. Google雲消息傳遞限制
- 21. Google雲消息傳遞,註冊握手
- 22. GCM消息傳遞確認
- 23. PushSharp GCM消息傳遞
- 24. Google雲消息傳遞 - 使用PHP向Android發送推消息
- 25. Android上使用Google雲消息傳遞的即時消息
- 26. 無法從服務器發佈消息:Google雲消息傳遞
- 27. 用於推送通知的GCM(Google雲消息傳遞)的令牌標識
- 28. GCM(Google雲消息傳遞)ID是Android應用程序的唯一ID嗎?
- 29. 設置Google雲消息傳遞(GCM)HTTP服務器和客戶端
- 30. Google雲消息不工作的ANDROID 2.3.6
但我需要我自己的服務器? – user1949861
本教程適用於瀏覽器實現。如何在android中實現它。 –