Android - 收到推播後,告知現在運行的頁面

摘要:Android - 收到推播後,告知現在運行的頁面

有需要一個功能,是如果現在在運行的頁面中,

收到與他有關的推播,則更新資料

可參考以下網址去實作

http://stackoverflow.com/questions/22252065/refreshing-activity-on-receiving-gcm-push-notification

 

Acitivty 面要做以下事情


//register your activity onResume()
@Override
public void onResume() {
    super.onResume();
    context .registerReceiver(mMessageReceiver, new IntentFilter("unique_name"));
}

//Must unregister onPause()
@Override
protected void onPause() {
    super.onPause();
    context.unregisterReceiver(mMessageReceiver);
}


//This is the handler that will manager to process the broadcast intent
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {

        // Extract data included in the Intent
        String message = intent.getStringExtra("message");

        //do other stuff here
    }
};

 

收到推播的Service要送內部廣播


// This function will create an intent. This intent must take as parameter the "unique_name" that you registered your activity with
static void updateMyActivity(Context context, String message) {

    Intent intent = new Intent("unique_name");

    //put whatever data you want to send, if any
    intent.putExtra("message", message);

    //send broadcast
    context.sendBroadcast(intent);
}