You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Ionic 8+Capacitor7项目中适配OneSignal插件

Ionic 8 + Capacitor 7 集成OneSignal插件求助

问题概述

迁移Ionic4旧项目至Ionic8+Capacitor7环境,导入import { OneSignal } from '@awesome-cordova-plugins/onesignal/ngx';后,Android模拟器抛出错误:
*Native: tried calling OneSignal.xxxxxxx, but the OneSignal plugin is not installed. *

旧项目依赖ionic cordova plugin add onesignal-cordova-plugin安装插件,但该命令无法在Capacitor项目中执行。OneSignal官方的Ionic+Capacitor配置说明不够详细,无法复现旧项目中的核心功能,特此求助。

需要复现的旧项目OneSignal功能代码

// 1. DEBUGGING
// 0 = NONE, 1 = FATAL, 2 = ERROR, 3 = WARN, 4 = INFO, 5 = DEBUG, 6 = VERBOSE

this.oneSignal.setLogLevel({logLevel: 5, visualLevel: 0});

// 2. INIT PROCESS
// startInit('onesignal app id','google firebase project number')
this.oneSignal.startInit('xxxxxxxxx', 'yyyyyyy');

// 3. DISPLAYING
// to control when to show notifications while using smartphone
this.oneSignal.inFocusDisplaying(this.oneSignal.OSInFocusDisplayOption.Notification);

// 4. EVENTS 
// Received handler: Called when the app receives a notification. Do something when a notification is received.
this.oneSignal.handleNotificationReceived().subscribe(data => {
    alert('>>>>>> Notifica ricevuta: '+JSON.stringify(data, null, 3));
});
// Opened handler: Called when the user opens or taps an action on a notification. Do something when a notification is opened
this.oneSignal.handleNotificationOpened().subscribe(data => {
    console.log('>>>>Notification opened: '+JSON.stringify(data)); 
});

// Test to see if user has already granted consent: If user hasn't granted consent then
window.plugins.OneSignal.setRequiresUserPrivacyConsent(true);

// Display a modal window asking for GDPR consent: Capture the consent (or not) and update OneSignal
window.plugins.OneSignal.provideUserConsent(true);

//to get onesignalPlayerId when subscribed
this.oneSignal.addSubscriptionObserver().subscribe( state => {
    if (!state.from.subscribed && state.to.subscribed) {
        alert(">>>>Subscribed for OneSignal push notifications!");
        state.to.userId // get player id
        alert(">>>>Push Subscription state changed: "+JSON.stringify(state));

        let onesignalPlayerId = state.to.userId;
        alert(">>>>onesignalPlayerId: "+onesignalPlayerId);
        this.initDevice(uuid,onesignalPlayerId);
    }
});

this.oneSignal.getPermissionSubscriptionState().then(res =>{
    let status: any = res;
    alert(">>>>>>>>getPermissionSubscriptionState: "+JSON.stringify(status));
    status.permissionStatus.hasPrompted; // Bool
    status.permissionStatus.status; // iOS only: Integer: 0 = Not Determined, 1 = Denied, 2 = Authorized
    status.permissionStatus.state; //Android only: Integer: 1 = Authorized, 2 = Denied
    status.subscriptionStatus.subscribed; // Bool
    status.subscriptionStatus.userSubscriptionSetting; // Bool
    status.subscriptionStatus.userId; // String: OneSignal Player ID
    status.subscriptionStatus.pushToken; // String: Device Identifier from FCM/APNs
});


// 5. ENDINIT PROCESS
this.oneSignal.endInit();

额外需求

还需实现this.oneSignal.setExternalUserId(externalUserId)功能,但当前使用的插件中未找到对应方法。

限制条件

因现有大量基于OneSignal API的应用,无法切换到Firebase Cloud Messaging+Capacitor推送通知方案,必须让OneSignal正常工作。


内容的提问来源于stack exchange,提问作者Stefano Bertoli

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 23:50:08