OneSignal / onesignal-go-api

The official OneSignal API client for Go
MIT License
11 stars 7 forks source link

Create notification with include_aliases failed #38

Open kienvtqhi opened 7 months ago

kienvtqhi commented 7 months ago

How can we help?

Hello,

I am using create notification API with included_aliases to send the notification to the specified user by external_id.

aliasExternalIds := &onesignal.PlayerNotificationTargetIncludeAliases{
        AdditionalProperties: map[string]interface{}{
            "external_id": []string{receiverId},
        },
    }

notification.IncludeAliases = *onesignal.NewNullablePlayerNotificationTargetIncludeAliases(aliasExternalIds)

notification.IncludeExternalUserIds = []string{receiverId}

    targeChannel := "push"
    notification.TargetChannel = &targeChannel
    // notification.IncludedSegments = []string{"Total Subscriptions"}

    var iosBadgeCount *int32 = new(int32)
    *iosBadgeCount = 1
    notification.IosBadgeCount = *onesignal.NewNullableInt32(iosBadgeCount)

    iosBadgeType := "Increase"
    notification.IosBadgeType = *onesignal.NewNullableString(&iosBadgeType)

    appAuth := context.WithValue(context.Background(), onesignal.AppAuth, nd.oneSignalConfig.RestApiKey)

    successResponse, _, err := nd.oneSignalApiClient.DefaultApi.CreateNotification(appAuth).Notification(notification).Execute()

But it is failed. It return the invalid_aliases.

{
    "id": "b15ba6e5-2223-49d6-9eda-684b9051f5a8",
    "external_id": null,
    "errors": {
        "invalid_aliases": {
            "external_id": [
                "bnudasdfRUt4yUJh0912E63tzIXPsxq1"
            ]
        }
    }
}

What's wrong? Please help me to fix it.

Thank you so much!

Code of Conduct

jasperkuperus commented 5 months ago

Same issue here. I'm trying to migrate from device-centric to the user-centric model. From the app everything works. Now I'm writing a migration script that loops over all subscription IDs of a user. For the first subscription, it gets the onesignal_id using GET https://onesignal.com/api/v1/apps/${onesignalAppId}/subscriptions/${subscriptionId}/user/identity.

Then it creates an alias by doing PATCH https://onesignal.com/api/v1/apps/${onesignalAppId}/users/by/onesignal_id/${oneSignalId}/identity.

For the rest of the subscriptions connected to a user, I transfer the subscriptions using PATCH https://onesignal.com/api/v1/apps/${onesignalAppId}/subscriptions/${subscriptionId}/owner.

I've tested this approach manually and it seems to work. I have now ran my script on 1 user and when I get that user by external_id, it properly shows me all subscriptions connected to that user.

However, when I now try to send a notification, I get this:

{"id":"","errors":{"invalid_aliases":{"external_id":["3cc7a2ba-91ea-4b18-93fc-c2123e458c6e","3cc7a2ba-91ea-4b18-93fc-c2123e458c6e","3cc7a2ba-91ea-4b18-93fc-c2123e458c6e","3cc7a2ba-91ea-4b18-93fc-c2123e458c6e","3cc7a2ba-91ea-4b18-93fc-c2123e458c6e"]}}}

Thoughts, @emawby?

malikmajai commented 2 days ago

It happens when notifications permission is not enabled by user on device.

jasperkuperus commented 1 day ago

@malikmajai I could see the logic there, but sending a notification to that external_id then fails as a whole. Even the linked subscriptions that do have permissions enabled don't get the notification. If you're sending a notification to 3 devices, where 1 device is not properly configured, the other 2 should still get the notification. Otherwise the whole user-centric model doesn't have any value.