如何在没有用户操作的情况下自动打开应用程序在 Android 上接收推送

Din*_*ler 2 android launching-application android-intent cordova-plugins

我正在尝试在收到推送通知时打开应用程序。

代码:

public class GCMIntentService extends GCMBaseIntentService {

    public GCMIntentService() {
        super("GCMIntentService");
    }

    @Override
    protected void onMessage(Context context, Intent intent) {
        Intent dialogIntent = new Intent(this, MyActivity.class);
        dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(dialogIntent);    
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我到目前为止。但我无法弄清楚 MyActivity.class 是什么。

假设我想启动应用程序,就是这样。我不想向 AndroidManifest.xml 添加另一个活动。

我不能简单地将 MainActivity.class 用于 Cordova 吗?

MyActivity.class 到底是什么?

小智 5

PackageManager.getLaunchIntentForPackage 可以帮助您找到入口活动。

    Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
    launchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(launchIntent);
Run Code Online (Sandbox Code Playgroud)