当推送到达时,将cordova应用程序带到前台

时间:2015-09-27 20:54:18

标签: java android cordova

我有应用并运行。推送通知正常。我需要在推送到达时将应用程序带到前台,在Android上。所以,我发现的是这段代码:

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

我试图将此代码放在来自cordova push插件的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

如何从cordova插件.java文件访问此“MainActivity.class”?

2 个答案:

答案 0 :(得分:3)

java编译器告诉你它在编译MainActivity.class时不知道GCMIntentService.java是什么。您必须从定义它的包中导入MainActivity类,例如如果包被称为cordovaExample,则在GCMIntentService.java put

的顶部
import cordovaExample.MainActivity;

并且必须将该类声明为公共

package cordova;

public class MainActivity {

答案 1 :(得分: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);
            }
        }
    }
}