2015-09-27 31 views
3

我的應用程序已啓動並正在運行。推送通知工作正常。當推送到達時,我需要將應用程序帶到Android上的前臺。所以,我發現這是一段代碼:推送到達時將cordova應用程序置於前臺

Intent toLaunch = new Intent(getApplicationContext(), MainActivity.class); 

toLaunch.setAction("android.intent.action.MAIN"); 
toLaunch.addCategory("android.intent.category.LAUNCHER"); 

來自這個問題摘自: Bring application to front after user clicks on home button

我試圖把這個代碼GCMIntentService.java從科爾多瓦推插件。無論身在何處,我把它,在編譯我總是得到這個錯誤:

/appdir/android/src/com/plugin/gcm/GCMIntentService.java:94: error: cannot find symbol 
Intent toLaunch = new Intent(getApplicationContext(), MainActivity.class); 
                ^
symbol: class MainActivity 
location: class GCMIntentService 

任何想法如何訪問從科爾多瓦插件java文件這個「MainActivity.class」?

+0

你有MainActivity是不是應用程序..如果它的存在導入此。否則,請提供您要在推送通知中打開的活動名稱 – Amar1989

+0

您可以分享您的最終解決方案嗎?它可能會幫助某些人? – karma

回答

3

java編譯器告訴你,在編譯GCMIntentService.java時,它不知道MainActivity.class是什麼。您必須從其定義的包中導入MainActivity類,例如如果包被稱爲cordovaExample然後在GCMIntentService.java上面放

import cordovaExample.MainActivity; 

和類必須聲明爲public

package cordova; 

public class MainActivity { 
+0

這就是它,正確的答案。非常感謝。 :) –

0

這是我做什麼,在GMCIntentService.java文件,其中工作過的巨大變化我。

import com.package.app.*; 


@Override 
     protected void onMessage(Context context, Intent intent) { 
      Log.d(TAG, "onMessage - context: " + context); 

    // Extract the payload from the message 
    Bundle extras = intent.getExtras(); 
    if (extras != null) 
    { 
     // if we are in the foreground, just surface the payload, else post it to the statusbar 
     if (PushPlugin.isInForeground()) { 
      extras.putBoolean("foreground", true); 
      PushPlugin.sendExtras(extras); 
     } 
     else { 
      extras.putBoolean("foreground", false); 

      Log.d(TAG, "force launch event"); 
      Intent wintent = new Intent(context, MainActivity.class); 
      wintent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED | Intent.FLAG_ACTIVITY_NEW_TASK); 
      startActivity(wintent); 

      // Send a notification if there is a message 
      if (extras.getString("message") != null && extras.getString("message").length() != 0) { 
       createNotification(context, extras); 
       PushPlugin.sendExtras(extras); 
      } 
     } 
    } 
} 
相關問題