推送到达时将cordova应用程序置于前台 [英] Bring cordova application to foreground when push arrives
问题描述
我已启动应用程序并正在运行.推送通知正常运行.我需要在推送到达时在Android上将应用程序置于前台.所以,我发现的是这段代码:
I have app up and running. Push notifications are working ok. I need that when push arrives, bring app to foreground, on 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
我正在尝试将这些代码从cordova push插件放入GCMIntentService.java中.无论我放在哪里,在编译时我总是会收到此错误:
I am trying to put this code in GCMIntentService.java from cordova push plugin. 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
有什么想法如何从cordova插件.java文件访问"MainActivity.class"吗?
Any ideas how to access this "MainActivity.class" from cordova plugin .java file?
推荐答案
java编译器告诉您在编译GCMIntentService.java
时它不知道MainActivity.class
是什么.您必须从定义了它的包中导入MainActivity
类,例如如果该包名为cordovaExample
,则在GCMIntentService.java
顶部放
The java compiler is telling you that it doesn't know what MainActivity.class
is while compiling GCMIntentService.java
. You must import MainActivity
class from the package where it is defined e.g. if the package is called cordovaExample
then at the top of GCMIntentService.java
put
import cordovaExample.MainActivity;
并且必须将该类声明为公共
and the class must be declared public
package cordova;
public class MainActivity {
这篇关于推送到达时将cordova应用程序置于前台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!