GCM (Google Cloud Messaging) を使用して別の Android フォンで通知を送信するには、アプリケーションのサーバーが必要ですか? いいえの場合、別の Android スマートフォンに通知を送信するにはどうすればよいですか? GCMが実際にどのように機能するかを誰か説明してもらえますか!
質問する
1519 次
2 に答える
1
GCM については、このチュートリアルを参照すると役立つ場合があります。
http://androidv5.wordpress.com/2012/08/15/how-to-implement-google-cloud-messaging/
それは私にとってはうまくいきます。
于 2013-01-17T08:33:28.163 に答える
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" />
このコードを適用する前に、ここをクリックしてください
于 2013-01-17T10:16:38.137 に答える