Cordova - 通知后台运行服务

Ija*_*d N 6 android android-service android-notifications cordova-plugins

我正在使用cordova来构建我的android应用程序.由于android杀死服务,我绑定服务与通知,以避免服务终止.

这是我如何通过通知绑定服务的方法

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    context = this.getApplicationContext();
    notifyService();

    return START_NOT_STICKY;
}

private void notifyService() {
    String package_name = this.getApplication().getPackageName();

    Intent notificationIntent = new Intent(this, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

    Bitmap icon = BitmapFactory.decodeResource(getResources(),
            this.getApplication().getResources().getIdentifier("icon", "drawable-hdpi", package_name));

    Notification notification = new NotificationCompat.Builder(this)
            .setContentTitle("Smart Home")
            .setContentText("Smart Home running in background")
.setSmallIcon(this.getApplication().getResources().getIdentifier("icon", "drawable-hdpi", package_name))
            .setContentIntent(pendingIntent)
            .setOngoing(true)
            .build();

    startForeground(notificationId, notification);
} 
Run Code Online (Sandbox Code Playgroud)

这是输出

在此输入图像描述

生成通知但通知标题不是我设置的.此外,当我点击此通知时,它正在转移到应用信息活动.但我想转到我的主要活动.

有没有人遇到同样的问题?或者我的代码需要对cordova进行任何更改?

Ija*_*d N 3

经过长时间的尝试才弄清楚。问题出在我待处理的意图活动名称上。下面的代码对我有用

String package_name = this.getApplication().getPackageName();
Intent notificationIntent = context.getPackageManager().getLaunchIntentForPackage(package_name);
Run Code Online (Sandbox Code Playgroud)