0
我已經正確設置Google Cloud Messaging for Android並接收來自我的服務器的通知。但是,如果應用程序處於打開狀態,但我想避免發出通知,但要獲取數據並在應用程序中通知用戶。請查看下面我GcmIntentService,該通知被處理:GCM Android通知當應用程序打開
@Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
String messageType = gcm.getMessageType(intent);
if (!extras.isEmpty()) {
if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
if(MainActivity.app_is_open){
// I would like to call a function from MainActivity to fetch data here
} else
// Sends notification here
}
}
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
我無法從MainActivity引用功能,因爲他們都是非靜態的。什麼是常用方法?我很難找到與這種情況有關的任何問題。
我設置了一個布爾值來知道應用程序是否正在運行。這不是我正在問的,但另一方面, – GameDevGuru
,你可以創建一個靜態方法,無論你想要的,我不知道你想如何注意用戶,如果它是一個吐司/對話或任何一種對於需要上下文的視圖,您可以在意圖中使用getApplicationContext()來獲取它。如果您使用的是數據庫,您也可以在活動中調用靜態方法並處理該事件,只需考慮更新UI中的更改所需的參數(在MainActivity中)。 –