notifo
notifo copied to clipboard
Dotnet integration
Step 1 Initialize the client
First you have to download the official SDK:
https://www.nuget.org/packages/Notifo.SDK
You can initialize it like this:
INotifoClient client =
NotifoClientBuilder.Create()
.SetApiKey("YOUR_API_KEY")
// .SetApiUrl(ServerUrl)
// .SetTimeout(Timeout())
.Build();
The api key can be found at the home page of your app.
Step 2: Create a user
The next step is to create a user. Because notifications are only sent to users (either directly or indirectly):
// STEP 0: Create user.
var userId1 = Guid.NewGuid().ToString();
var userId2 = Guid.NewGuid().ToString();
var userRequest = new UpsertUsersDto
{
Requests =
[
new UpsertUserDto
{
Id = userId1,
FullName = "name1_0"
},
new UpsertUserDto
{
Id = userId2,
FullName = "name2_0"
},
]
};
var users = await _.GetClient(mode).Users.PostUsersAsync(_.AppId, userRequest);
There are a few things that make this API special:
- It is an upsert. You can use the same API endpoint to also make updates to your users.
- As you can see, you can create multiple users in a single request. This is useful if you want to synchronize all your users to notifo. But if you have one million users it is recommended to call this endpoint in batches of 1000 users or so.
- You can define the ID yourself. This is optional, but I recommend to use the same ID that you use in your database, to make updates easier.
STEP 2: Send notifications
Next we send notifications to the user. Again we use the bulk endpoint for that.
var publishRequest = new PublishManyDto
{
Requests =
[
new PublishDto
{
Topic = $"users/{user.Id}",
Preformatted = new NotificationFormattingDto
{
Subject = new LocalizedText
{
["en"] = "HELLO User"
}
}
},
]
};
await _.Client.Events.PostEventsAsync("YOUR_APP_ID", publishRequest);
In this case we do not use any external channel like email or so. The notification is just added to the database and can be consumed by another API.
You can also integrate the widget if you have a web application: https://github.com/notifo-io/notifo/wiki/Web-Plugin
The API Key for that can be retrieved from the response when you create the user in step 1. This is the very basic setup.
Hello Sebastian,
I’ve applied your instruction to my code in order to integrate notifo to my application. Here is my code:
public void Send(string emailAddress, string subject, string content)
{
INotifoClient client = NotifoClientBuilder
.Create()
.SetApiKey(_apiKey)
// .SetApiUrl(ServerUrl)
// .SetTimeout(Timeout())
.Build();
// STEP 0: Create user.
var users = client. //OK
.Users
.PostUsersAsync(_appId, CreateUserObject("John Doe", emailAddress))
.GetAwaiter()
.GetResult();
client. //OK
.Events
.PostEventsAsync(_appId, CreateBody(_topic, subject, content))
.GetAwaiter()
.GetResult();
var subs = client //subscriptions collection return 0
.Users
.GetSubscriptionsAsync(_appId, _topic)
.GetAwaiter()
.GetResult();
}
private UpsertUsersDto CreateUserObject(string fullName, string email)
{
var users = new List<UpsertUserDto>
{
new()
{
Id = "user1",
FullName = fullName,
EmailAddress = email,
PhoneNumber = "90555544443322",
PreferredLanguage = "en"
}
};
return new UpsertUsersDto { Requests = users };
}
private PublishManyDto CreateBody(string topic, string subject, string content) => new()
{
Requests = new List<PublishDto>
{
new()
{
Topic = topic,
Preformatted = new NotificationFormattingDto
{
Subject = new LocalizedText
{
["en"] = content
}
}
}
}
};
After this business, when I see the logs =>
In order to solve this, I’ve insert a snipped code out of your instruction,
client
.Users
.PostSubscriptionsAsync(_appId, _topic, CreateSubscription(_topic))
.GetAwaiter()
.GetResult();
private SubscribeManyDto CreateSubscription(string topicPrefix) =>
new()
{
Subscribe = new List<SubscribeDto>
{
new()
{
TopicPrefix = topicPrefix,
}
}
};
But I’ve got an error again. How should I go further? Thanks for your assists.
Notifo has a hierarchical topic system. For Example
products/
products/shoes
products/shoes/nike
So you subscribe the user to whatever he is interested, lets say he wants to be notified about all shoes, then you subscribe him to
products/shoes
Then you publish an event with a very specific topic, e.g. products/shoes/nike/model-1/red
And all users which either subscribe to this topic or one of the parent topics (like products/shoes
) get notified. There are also "implicit" topics like users/USER_ID
So you have to subscribe to these events first.
It should look like this:
await actualClient.Users.PostSubscriptionsAsync(APP_ID, USER_ID,
new SubscribeManyDto
{
Subscribe = [
new SubscribeDto
{
TopicPrefix = "product/shoes"
}
]
});