GCM(Google云消息传递)如何运作?

时间:2013-01-17 08:28:32

标签: android

我是否需要使用我的应用程序的服务器才能使用GCM(Google云消息传递)在另一台Android手机上发送通知?如果没有,那么我该如何向另一个Android手机发送通知?有人可以解释一下GCM究竟是如何运作的!

2 个答案:

答案 0 :(得分:1)

对于GCM请参阅本教程,它可能会对您有所帮助,

http://androidv5.wordpress.com/2012/08/15/how-to-implement-google-cloud-messaging/

&安培; http://avilyne.com/?p=267

它适用于我。

答案 1 :(得分: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" />

在应用此代码之前,请点击here