简体   繁体   English

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

[英]Bring cordova application to foreground when push arrives

I have app up and running. 我有应用程序和运行。 Push notifications are working ok. 推送通知正常。 I need that when push arrives, bring app to foreground, on Android. 我需要在推送到达时将应用程序带到前台,在Android上。 So, what I found is this piece of code: 所以,我发现这段代码:

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

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

Taken from this question: Bring application to front after user clicks on home button 取自这个问题: 用户点击主页按钮后将应用程序带到前面

I am trying to put this code in GCMIntentService.java from cordova push plugin. 我试图将此代码放在来自cordova push插件的GCMIntentService.java中。 No matter where I put it, on compile i always get this error: 无论我把它放在哪里,在编译时我总是得到这个错误:

/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

Any ideas how to access this "MainActivity.class" from cordova plugin .java file? 有关如何从cordova插件.java文件访问此“MainActivity.class”的任何想法?

The java compiler is telling you that it doesn't know what MainActivity.class is while compiling GCMIntentService.java . java编译器告诉你它在编译GCMIntentService.java不知道MainActivity.class是什么。 You must import MainActivity class from the package where it is defined eg if the package is called cordovaExample then at the top of GCMIntentService.java put 您必须从定义它的包中导入MainActivity类,例如,如果包名为cordovaExample则在GCMIntentService.java的顶部放置

import cordovaExample.MainActivity;

and the class must be declared public 并且必须将该类声明为公开

package cordova;

public class MainActivity {

Here is what I did, Changes on GMCIntentService.java file which worked great for me. 以下是我所做的,对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);
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM