Skip to content

[Bug]: Having issue with external ids for specific user targeted notification #82

@sadhan46

Description

@sadhan46

What happened?

I'm using node Js to trigger notifications using external Ids even though the notifications are getting triggered for all users.
I'm facing issues when triggering device specific notification

  1. external_id is showing up as "null".
  2. the error statement even after using "notification.include_aliases = { "external_id": externalIds }"
CreateNotificationSuccessResponse {
  id: 'ae3093eb-3bda-41a3-b67d-b0281991a08b',
  external_id: null,
  errors: { invalid_aliases: { external_id: [Array] } }
}

Steps to reproduce?

Node version : v21.4.0
One signal version : "@onesignal/node-onesignal": "^5.0.0-alpha-01",

1. Created node js function to trigger notification


const OneSignal = require('@onesignal/node-onesignal');
const { ONE_SIGNAL_CONFIG } = require('../config/app.config');

const configuration = OneSignal.createConfiguration({
    userAuthKey: ONE_SIGNAL_CONFIG.AUTH_KEY,
    restApiKey: ONE_SIGNAL_CONFIG.API_KEY,
});

const client = new OneSignal.DefaultApi(configuration);

// Function to send mobile notifications
async function sendNotification(externalIds, message) {
    try {
 
        //createNotification
        const notification = new OneSignal.Notification();
        notification.app_id = ONE_SIGNAL_CONFIG.APP_ID;
        notification.name = "external_id";
        notification.contents = {
            en: message
        }

        // required for Huawei
        notification.headings = {
            en: "External Id"
        }
        // Note: Tried passing static external id for testing
        notification.external_id = '66378fb25e9d1a7f26c5252b'
        
        //Note: passed array of externalIds. For e.g (['66378fb25e9d1a7f26c5252b']
        notification.include_aliases = { "external_id": externalIds }
        notification.target_channel = "push"
        const notificationResponse = await client.createNotification(notification);

        console.log('Notification sent:', notificationResponse);
        return notificationResponse;
    } catch (error) {
        console.error('Error sending notification:', error);
        throw error;
    }
}

module.exports = { sendNotification };



2. Called the function in one of my API

// Send notification
sendNotification([profile._id], 'PROFILE NOTIFICATION')
  .then(() => console.log('Notification sent successfully'))
  .catch(error => console.error('Error sending notification:', error));

3. Called following functions in initState() of my flutter project


  Future<void> initPlatform() async {
    OneSignal.Debug.setLogLevel(OSLogLevel.verbose);

    OneSignal.Debug.setAlertLevel(OSLogLevel.none);
    OneSignal.initialize(AppKeys.oneSignalAppId);
    OneSignal.Notifications.requestPermission(true);

    print(
        'ONE_SIGNAL IDs... ${await OneSignal.User.getOnesignalId()}'); 
    print(
        'ONE_SIGNAL IDs.... External... ${await OneSignal.User.getExternalId()}'); 
  }


  loginOneSignal() async {
    final externalId = await OneSignal.User.getExternalId();
    if (externalId == null) {
      final userId = ref.watch(accountPageProvider).profile?.sId;
      if (userId != null) {
        await OneSignal.logout();
        await OneSignal.login(userId);
      }
    }
  }

What did you expect to happen?

I expected that the notifications will be shown only in the target devices which have logged into oneSignal with custom Id.
For e.g. OneSignal.login('custom_id')

Relevant log output

CreateNotificationSuccessResponse {
  id: 'ae3093eb-3bda-41a3-b67d-b0281991a08b',
  external_id: null,
  errors: { invalid_aliases: { external_id: [Array] } }
}

Code of Conduct

  • I agree to follow this project's Code of Conduct

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions