send notification for user

Copper Contributor

I am using SendActivityNotification as below:

 

     var requestBody = new Microsoft.Graph.Users.Item.Teamwork.SendActivityNotification.SendActivityNotificationPostRequestBody
                    {
                        Topic = new TeamworkActivityTopic
                        {
                            Source = TeamworkActivityTopicSource.EntityUrl,
                            Value = $"https://graph.microsoft.com/v1.0/users/{user.Id}/teamwork/installedApps/{installationId}",
                        },
                        ActivityType = "taskCreated",
                        PreviewText = new ItemBody
                        {
                            Content = "New Task Created",
                        },
                        TemplateParameters = new List<KeyValuePair>
                        {
                            new KeyValuePair
                            {
                                Name = "taskId",
                                Value = "Task 12322",
                            },
                        },
                            };
                    await graphClient.Users[user.Id].Teamwork.SendActivityNotification.PostAsync(requestBody);

 

Where graphClient was created using client accessToken "on behalf of" OAuth flow

I got an error that "The recipient of the notification cannot be the same as the sender of the notification."
I do want to be able to send notification to current Teams logged user about events happened in my application.
What I am doing wrong?

 

19 Replies

@vovanb -Thanks for reporting your issue.
We will check this at our end and will get back to you.

@vovanb -To ensure that the recipient of the notification is not the same as the sender.
Could you please try with the code like below, as here mentioned the recipient -

// Code snippets are only available for the latest version. Current version is 5.x

var graphClient = new GraphServiceClient(requestAdapter);

var requestBody = new Microsoft.Graph.Chats.Item.SendActivityNotification.SendActivityNotificationPostRequestBody
{
	Topic = new TeamworkActivityTopic
	{
		Source = TeamworkActivityTopicSource.EntityUrl,
		Value = "https://graph.microsoft.com/v1.0/chats/{chatId}",
	},
	ActivityType = "taskCreated",
	PreviewText = new ItemBody
	{
		Content = "New Task Created",
	},
	Recipient = new AadUserNotificationRecipient
	{
		OdataType = "microsoft.graph.aadUserNotificationRecipient",
		UserId = "569363e2-4e49-4661-87f2-16f245c5d66a",
	},
	TemplateParameters = new List<KeyValuePair>
	{
		new KeyValuePair
		{
			Name = "taskId",
			Value = "12322",
		},
	},
};
await graphClient.Chats["{chat-id}"].SendActivityNotification.PostAsync(requestBody);


Reference Document-Send activity feed notifications to users in Microsoft Teams - Microsoft Graph | Microsoft Learn

@vovanb -Did you get chance to check above solution?
Hi
I do not see difference between example I sent and you provided. I need to notify my user (which I logged in ) about new task created. so - notification should be sent to me. How to do it?
This is EXACTLY what I used - you can even see my code from this example. BUT i got error "The recipient of the notification cannot be the same as the sender of the notification."

@Sayali-MSFT please note I was able to successfully to construct Graph API request and I got 202 response

 

https://developer.microsoft.com/en-us/graph/graph-explorer?request=teamwork%2FsendActivityNotificati...
but nothing happen in Teams

@vovanb -In teams are you getting the same error?
We will setup the sample from our end and let you know the update.
Sample link-https://github.com/OfficeDev/Microsoft-Teams-Samples/blob/03dae3d9e7fe34ca968abdc530ebef95132f490b/samples/graph-activity-feed/csharp/ActivityFeedNotification/Controllers/HomeController.cs#L72

@Sayali-MSFT 

This is where I am struggling to convert Graph API to C# version. Could you provide example in C# for the below post call https://graph.microsoft.com/v1.0/teamwork/sendActivityNotificationToRecipients

In examples you provided I did found such implemntation.

@Sayali-MSFT 

Below what I did :

1. Using example Sample link-https://github.com/OfficeDev/Microsoft-Teams-Samples/blob/03dae3d9e7fe34ca968abdc530ebef95132f490b/samples/graph-activity-feed/csharp/ActivityFeedNotification/Controllers/HomeController.cs#L72 I got:
Cannot read the value '1' as a quoted JSON string value
This is because in example Source = TeamworkActivityTopicSource.Text, converted to 1
So this example does not work
2. I create below code to simulate Graph API example I provided - similar to Graph no activities appear in Teams:

 

                var requestBody = new Microsoft.Graph.Teamwork.SendActivityNotificationToRecipients.SendActivityNotificationToRecipientsPostRequestBody
                {
                    Topic = new TeamworkActivityTopic
                    {
                        Source = TeamworkActivityTopicSource.EntityUrl,
                        Value = "https://graph.microsoft.com/v1.0/appCatalogs/teamsApps/12438DD2-E2C5-4CCC-B480-79D0672F86C7",
                    },
                    ActivityType = "approvalRequired",
                    PreviewText = new ItemBody
                    {
                        Content = "Internal spending team has a pending finance approval requests",
                    },
                    Recipients = new List<TeamworkNotificationRecipient>
    {
        new AadUserNotificationRecipient
        {
            OdataType = "microsoft.graph.aadUserNotificationRecipient",
            UserId = userId,
        }
    },
                    TemplateParameters = new List<KeyValuePair>
    {
        new KeyValuePair
        {
            Name = "approvalTaskId",
            Value = "5",
        },
    },
};
await graphClient.Teamwork.SendActivityNotificationToRecipients.PostAsync(requestBody);

 

 SO all examples I used does not send notification or failed.
Can you guys create simple sample which can send notification to logged user?

 

@Sayali-MSFT 

any chance to help ? I not able to send any notification to myself from TAB?

Thanks

@vovanb - We have checked with the internal team, but currently, the only available sample is the one mentioned above. There are no other simple samples available for reference.
Here is Graph Api samples available- https://github.com/OfficeDev/Microsoft-Teams-Samples/tree/03dae3d9e7fe34ca968abdc530ebef95132f490b#g...

@Sayali-MSFT thanks for links, BUT this examples do not working. I can not send notification to logged user.
Is it working for you? Did you try?

@vovanb -Currently, we are not able to send the notification to be logged on user.

Could you please help by suggesting this feature on - Microsoft Teams · Community

@Sayali-MSFT 

sorry, did not understand your response - so it is not possible to use that API at all? Then it is a bug?

@vovanb -No, currently it is not possible. This is a kind of feature request, so could you please raise the feature request on Microsoft Teams · Community 

@Sayali-MSFT 

I am completely lost here. What is purpose of SendActivityNotification API? To whom I can send notification? When it should work? Nothing from your code example did not work for me?

Why this API created?

@vovanb - The purpose of the SendActivityNotification API is to send activity feed notifications in Microsoft Teams. This API allows you to send proactive messages to users, notifying them about updates or events related to your app.

You can send notifications to individual users or to a group of users. The recipients can be targeted based on their user ID, user principal name, or email address. You can also send notifications to a specific team or channel by specifying the team ID and channel ID.

The SendActivityNotification API should be used when you want to send notifications to users in real-time or based on specific events. It is commonly used to provide updates, alerts, or important information to users within the Teams app.

To use the SendActivityNotification API, you need to have the necessary permissions and authentication.