2014-07-08 40 views
0

我正在嘗試執行推送通知應用程序。我一直跟着拉維玉田的博客這個http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/我不明白的一個類的流..試圖瞭解程序的流程

類如下..

public class InitialActivity extends Activity { 
// label to display gcm messages 
TextView lblMessage; 

// Asyntask 
AsyncTask<Void, Void, Void> mRegisterTask; 

// Alert dialog manager 
AlertDialogManager alert = new AlertDialogManager(); 

// Connection detector 
ConnectionDetector cd; 

public static String name; 
public static String email; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main_pn); 

    cd = new ConnectionDetector(getApplicationContext()); 

    // Check if Internet present 
    if (!cd.isConnectingToInternet()) { 
     // Internet Connection is not present 
     alert.showAlertDialog(InitialActivity.this, 
       "Internet Connection Error", 
       "Please connect to working Internet connection", false); 
     // stop executing code by return 
     return; 
    } 

    // Getting name, email from intent 
    Intent i = getIntent(); 

    name = i.getStringExtra("name"); 
    email = i.getStringExtra("email");  

    // Make sure the device has the proper dependencies. 
    GCMRegistrar.checkDevice(this); 

    // Make sure the manifest was properly set - comment out this line 
    // while developing the app, then uncomment it when it's ready. 
    GCMRegistrar.checkManifest(this); 

    lblMessage = (TextView) findViewById(R.id.lblMessage); 

    registerReceiver(mHandleMessageReceiver, new IntentFilter(
      DISPLAY_MESSAGE_ACTION)); 

    // Get GCM registration id 
    final String regId = GCMRegistrar.getRegistrationId(this); 

    // Check if regid already presents 
    if (regId.equals("")) { 
     // Registration is not present, register now with GCM 
     lblMessage.append("inside first if condition where regid = null" + "\n\n"); 
     GCMRegistrar.register(this, SENDER_ID); 
    } else { 
     // Device is already registered on GCM 
     if (GCMRegistrar.isRegisteredOnServer(this)) { 
      // Skips registration.    
      Toast.makeText(getApplicationContext(), "Already registered with GCM", Toast.LENGTH_LONG).show(); 
     } else { 
      // Try to register again, but not in the UI thread. 
      // It's also necessary to cancel the thread onDestroy(), 
      // hence the use of AsyncTask instead of a raw thread. 
      lblMessage.append("inside 2nd if condition where regid != null and GCMRegistrar.isRegisteredOnServer(this) = false " + "\n"); 
      final Context context = this; 
      mRegisterTask = new AsyncTask<Void, Void, Void>() { 

       @Override 
       protected Void doInBackground(Void... params) { 
        // Register on our server 
        // On server creates a new user 
        lblMessage.append("inside doinbackground" + "\n\n"); 
        ServerUtilities.register(context, name, email, regId); 
        return null; 
       } 

       @Override 
       protected void onPostExecute(Void result) { 
        mRegisterTask = null; 
       } 

      }; 
      mRegisterTask.execute(null, null, null); 
     } 
    } 
}  

/** 
* Receiving push messages 
* */ 
private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     lblMessage.append("inside BroadcastReceiver mHandleMessageReceiver " + "\n\n"); 
     String newMessage = intent.getExtras().getString(EXTRA_MESSAGE); 
     // Waking up mobile if it is sleeping 
     WakeLocker.acquire(getApplicationContext()); 

     /** 
     * Take appropriate action on this message 
     * depending upon your app requirement 
     * For now i am just displaying it on the screen 
     * */ 

     // Showing received message 
     lblMessage.append(newMessage + "\n\n");   
     Toast.makeText(getApplicationContext(), "New Message: " + newMessage, Toast.LENGTH_LONG).show(); 
     // From Demo Server: successfully added device! 
     // Releasing wake lock 
     WakeLocker.release(); 
    } 
}; 

@Override 
protected void onDestroy() { 
    if (mRegisterTask != null) { 
     mRegisterTask.cancel(true); 
    } 
    try { 
     lblMessage.append("onDestroy " + "\n"); 
     unregisterReceiver(mHandleMessageReceiver); 
     GCMRegistrar.onDestroy(this); 
    } catch (Exception e) { 
     Log.e("UnRegister Receiver Error", "> " + e.getMessage()); 
    } 
    super.onDestroy(); 
} 

}

現在它的輸出如下:

該程序進入廣播接收機3次..這是怎麼發生的?我將相同的代碼應用到另一個應用程序,但代碼只輸入一次廣播接收器..所以發生了什麼?

+0

你可以問問Ravi Tamada,不管他是誰 –

+0

@亞歷克斯我做..沒有反應..我有點需要這個迫切.. – user3214173

+0

我會建議,而不是使用這個博客(這不是最近),你使用當前的官方演示For GCM。你可以在這裏找到它(https://code.google.com/p/gcm/source/browse/gcm-client/GcmClient/src/main/)。 – Eran

回答

0

評論說這一切。在開始看代碼時,您會檢查互聯網連接。第二次檢查後,您將註冊接收器以接收GCM消息。首先,您將嘗試從GCM服務器獲取設備的註冊ID。如果未註冊(爲此您將獲得「」),然後嘗試在GCM服務器中註冊設備,這對獲取GCM消息非常重要。一旦註冊了該應用程序項目(爲此您將無法獲得任何ID),請嘗試在您的服務器上註冊,因爲您的服務器需要具有唯一註冊的移動設備的註冊ID來發送GCM消息。 Receiver將接收來自GCM的消息。這是整個代碼所說的。對於一步一步的代碼,抱歉。您需要至少爲此付出一些努力。

+0

好吧,我同意..但我不明白的是接收器總是在聽..因此,即使代碼在不同的應用程序中提供,它應該工作? RYT? – user3214173

+0

如果接收方必須響應所有的廣播,那麼所有的應用程序都會響應所有的廣播(如Facebook的朋友請求的WhatsApp警報:P。只是開玩笑)。我想你必須仔細閱讀GCM的文檔。 – user1728071

+0

<! - 創建一個自定義權限,所以只有這個應用程序可以收到它的消息。 - > 。此代碼將使您的應用程序響應您的GCM發送的特定消息。這是你的應用程序。 – user1728071