如何在Android上接收推送时无需用户操作即可自动打开应用程序

时间:2015-05-07 00:35:02

标签: android android-intent cordova-plugins launching-application

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

代码:

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);    
    }
}

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

我们想说我想启动应用程序,那就是它。我不想在AndroidManifest.xml中添加其他活动。

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

MyActivity.class究竟是什么?

3 个答案:

答案 0 :(得分:4)

PackageManager.getLaunchIntentForPackage可以帮助您找到参赛作品。

    Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
    launchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(launchIntent);

答案 1 :(得分:1)

它是实现您要启动的活动的类。 LoginActivity,MainActivity,无论你命名它。

答案 2 :(得分:-1)

public void getnotification(View view){

       NotificationManager notificationmgr =(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
       Intent intent = new Intent(this,resultpage.class);
       PendingIntent p = PendingIntent.getService(this,(int) System.currentTimeMillis(),intent,0);


       Notification n = null;
       if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
           n = new Builder(this)
                                .setSmallIcon(R.mipmap.ic_launcher)
                               .setContentTitle("Hello android user here")
                               .setContentText("welcome to notification serrvice")
                               .setContentIntent(p)
                               .build();

           notificationmgr.notify(0,n);

       }