Ionic 3 + One Signal handleNotificationReceived 不会被触发

Ionic 3 + One Signal handleNotificationReceived doesn't get fired

我正在关注 this Ionic SDK Setup link。我正在我的应用程序上设置推送通知。

有效,我可以收到通知。这是我的代码。

this.platform.ready().then(() => {
    let OneSignal = window["plugins"].OneSignal;

    OneSignal
      .startInit(AppSettings.ONESIGNAL_APP_ID)
      .inFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification)
      .handleNotificationOpened(function(jsonData) {
        console.log('OPENED');
      })
      .handleNotificationReceived(function(jsonData) {
        console.log('RECEIVED');
      })
      .endInit();

    OneSignal.getIds(notificationsCallBack.getIdReceived.bind(this));
});

但是,函数 handleNotificationReceived 仅在应用程序打开时调用。我需要执行一些代码(我需要应用内徽章),即使应用已关闭。我在我的 OneSignal API 上设置了 content_available to true,我也直接在 OneSignal 网站上进行了测试,但无法正常工作。函数 handleNotificationOpened 按预期工作。

当我的应用程序未打开时,如何处理收到通知事件?

我正在 iPhone 6+、iOS 11.0.3

上进行测试

我的环境:

Ionic 3.12.0 Cordova 7.0.1

我的package.json

{
    "name": "XXX",
    "author": "XXXX XX",
    "homepage": "XXX",
    "private": true,
    "scripts": {
        "ionic:build": "ionic-app-scripts build",
        "ionic:serve": "ionic-app-scripts serve"
    },
    "dependencies": {
        "@angular/common": "4.2.2",
        "@angular/compiler": "4.2.2",
        "@angular/compiler-cli": "4.2.2",
        "@angular/core": "4.2.2",
        "@angular/forms": "4.2.2",
        "@angular/http": "4.2.2",
        "@angular/platform-browser": "4.2.2",
        "@angular/platform-browser-dynamic": "4.2.2",
        "@angular/platform-server": "4.2.2",
        "@angular/router": "^4.2.2",
        "@ionic-native/camera": "^3.4.4",
        "@ionic-native/core": "^3.10.3",
        "@ionic-native/facebook": "^4.3.1",
        "@ionic-native/geolocation": "^3.2.2",
        "@ionic-native/google-maps": "^3.4.4",
        "@ionic-native/google-plus": "^3.4.4",
        "@ionic-native/image-picker": "^3.2.2",
        "@ionic-native/native-storage": "^3.4.4",
        "@ionic-native/splash-screen": "^3.12.1",
        "@ionic-native/status-bar": "^3.12.1",
        "@ionic-native/toast": "^3.10.3",
        "@ionic-native/transfer": "^3.2.2",
        "@ionic/storage": "^2.0.0",
        "bourbon": "^4.2.7",
        "cordova-ios": "^4.5.1",
        "cordova-plugin-camera": "^2.4.1",
        "cordova-plugin-compat": "^1.2.0",
        "cordova-plugin-console": "^1.0.7",
        "cordova-plugin-crop": "^0.3.1",
        "cordova-plugin-device": "^1.1.6",
        "cordova-plugin-facebook4": "^1.7.4",
        "cordova-plugin-googlemaps": "^2.0.11",
        "cordova-plugin-googlemaps-sdk": "git+https://github.com/mapsplugin/cordova-plugin-googlemaps-sdk.git",
        "cordova-plugin-googleplus": "^5.1.1",
        "cordova-plugin-nativestorage": "^2.2.2",
        "cordova-plugin-splashscreen": "^4.0.3",
        "cordova-plugin-statusbar": "^2.2.3",
        "cordova-plugin-whitelist": "^1.3.2",
        "cordova-plugin-x-toast": "^2.6.0",
        "cordova-sqlite-storage": "^2.0.4",
        "cropperjs": "^1.1.0",
        "font-awesome": "^4.7.0",
        "ionic-angular": "3.3.0",
        "ionic-native": "2.9.0",
        "ionic-plugin-keyboard": "^2.2.1",
        "ionic2-auto-complete": "^1.5.0-beta",
        "ionicons": "3.0.0",
        "ios-sim": "^6.1.2",
        "ng2-cookies": "^1.0.4",
        "onesignal-cordova-plugin": "^2.0.11",
        "rxjs": "5.4.0",
        "sw-toolbox": "3.6.0",
        "zone.js": "0.8.12"
    },
    "devDependencies": {
        "@ionic/app-scripts": "1.3.7",
        "typescript": "2.3.4"
    },
    "cordovaPlugins": [
        "cordova-plugin-device",
        "cordova-plugin-console",
        "cordova-plugin-whitelist",
        "cordova-plugin-splashscreen",
        "cordova-plugin-statusbar",
        "ionic-plugin-keyboard",
        "cordova-plugin-geolocation",
        "cordova-plugin-compat",
        "cordova-plugin-file-transfer",
        "cordova-plugin-file"
    ],
    "cordovaPlatforms": [
        {
            "platform": "ios",
            "version": "",
            "locator": "ios"
        },
        {
            "platform": "android",
            "version": "",
            "locator": "android"
        }
    ],
    "description": "ion2-FullApp: The Best Ionic 2 Starter app",
    "config": {
        "ionic_sass": "./config/sass.config.js",
        "ionic_copy": "./config/copy.config.js"
    },
    "cordova": {
        "platforms": [
            "ios"
        ],
        "plugins": {
            "cordova-plugin-camera": {
                "CAMERA_USAGE_DESCRIPTION": " ",
                "PHOTOLIBRARY_USAGE_DESCRIPTION": " "
            },
            "cordova-plugin-console": {},
            "cordova-plugin-crop": {},
            "cordova-plugin-device": {},
            "cordova-plugin-facebook4": {
                "APP_ID": "XX",
                "APP_NAME": "XXX"
            },
            "cordova-plugin-googleplus": {
                "REVERSED_CLIENT_ID": "XX"
            },
            "cordova-plugin-nativestorage": {},
            "cordova-plugin-splashscreen": {},
            "cordova-plugin-statusbar": {},
            "cordova-plugin-whitelist": {},
            "cordova-plugin-x-toast": {},
            "cordova-sqlite-storage": {},
            "ionic-plugin-keyboard": {},
            "onesignal-cordova-plugin": {},
            "cordova-plugin-googlemaps": {
                "API_KEY_FOR_ANDROID": "XX",
                "API_KEY_FOR_IOS": "XX",
                "LOCATION_WHEN_IN_USE_DESCRIPTION": "Show your location on the map",
                "LOCATION_ALWAYS_USAGE_DESCRIPTION": "Trace your location on the map",
                "key": "XX"
            }
        }
    }
}

通知打开事件已在 OneSignal Cordova SDK 2.2.0 中修复。这只是 iOS 11 台设备的问题。

您需要检查 this。请让我知道这对你有没有用。 您需要在 POST 请求的正文中将 content_available 和 mutable_content 设置为 true。