android - Cordova - 通知后台运行服务

标签 android android-service cordova-plugins android-notifications

我正在使用 cordova 构建我的 android 应用程序。由于 android 终止服务,我将服务与通知绑定(bind)以避免服务终止。

这是我如何将服务与通知绑定(bind)的方法

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

这是输出

enter image description here

通知已生成,但通知标题与我设置的不同。此外,当我单击此通知时,它会转到应用信息 Activity 。但我想转到我的主要 Activity 。

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

最佳答案

当我点击此通知时,它会转到应用信息 Activity 。但我想转到我的主要 Activity 来实现这一目标 改变这一行

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

到这一行

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

希望对你有帮助

关于android - Cordova - 通知后台运行服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42618202/

相关文章:

android - Cordova:StatusBar 插件适用于模拟器但不适用于设备

android - 在没有 ArCore 的情况下与 SceneView 内的 3D 模型交互

android - flex android ActionScript native 扩展

android - 如何在带有 Gradle 的 Android Studio 中使用 roboeletric?

android - 下载管理器下载文件

ios - 将我的 cordova 项目转移到 Xcode

java - 用于在 android NDK 中录制音频的 OpenSL ES

ios - 相当于 iOS 上的 Android 服务

Android Activity 生命周期 - 通知服务

cordova - 屏幕锁定时 navigator.geolocation.getCurrentPosition()