2014-07-08 115 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