2013-01-17 48 views
-2

我是否需要我的應用程序的服務器使用GCM(Google Cloud Messaging)在另一個Android手機上發送通知?如果不是,那麼我怎麼能發送通知到另一個Android手機?有人能解釋我GCM究竟是如何運作的!GCM(Google雲消息傳遞)如何工作?

回答

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

相關問題