Firebase - Send Notifications (Android) - c#

I've an android app and need to implement push notification on it, send and receive simple notifications.
My scenario; When userY submit form about userX will send userX notification that userY sent you message...
My Question: Do I need to create server side layer (C#, PHP or Node.js) to handle this part, so from android send data to C# and C# send to Firebase ? or there simple way to do it direct from Android application ?
Thank you.

You do not need a server to send the message itself. You can even send from the bash shell.
curl -H "Content-Type: application/json " -H "Authorization: key=Your_AUTHKEY" -X POST -d '
{ "data" : { "mykey" : "myvalue"},"notification": {"title": "My test 1","body": "bla bla"}
, "to" : "Your_looong_TOKEN"}'
A problem may arise when passing a token. There can be a useful durability value (write to the base).
Just send a post request with the appropriate headers.

As per my comment, you should have a look at Cloud Functions for Firebase, specifically, Realtime Database Triggers.
Let's say you're using Firebase Realtime Database. For your flow where "userY sends a form to userX", it could go like there'd be a forms node for a users, where you'll add forms from other users, and every time a form is added, you send a notification with Firebase Cloud Messaging. Similar on how you'd send when using Firebase Admin.

Related

How to make a C# REST request using a Google Cloud JSON Service account file?

I'd like to leverage the new Google TTS using a simple rest request. To that end, I've created a Service Account which downloaded a JSON file containing a private key id, a private key, client email, client id, client_x509_cert_url, etc.
I've also set the environment variable for the system:
Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", jsonFile);
I then found this sample CURL request to use the WaveNet TTS engine provided by Google:
curl -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
-H "Content-Type: application/json; charset=utf-8" \
--data "{
'input':{
'text':'Android is a mobile operating system developed by Google,
based on the Linux kernel and designed primarily for
touchscreen mobile devices such as smartphones and tablets.'
},
'voice':{
'languageCode':'en-gb',
'name':'en-GB-Standard-A',
'ssmlGender':'FEMALE'
},
'audioConfig':{
'audioEncoding':'MP3'
}
}" "https://texttospeech.googleapis.com/v1beta1/text:synthesize" > writtenfile.txt
I want to create a simple C# webrequest from the above CURL request, but the line "gcloud auth application-default print-access-token" is a problem for me. I cannot install a CLI gcloud instance on this machine. Isn't there some way to setup the webrequest authorization access token from the JSON service account file? Surely I don't HAVE to have gcloud installed.
Looking for code sample demonstrating how to convert CURL code to a C# rest request using the service account json file without gcloud.
In case you don't want to install the gcloud packages, you can execute REST calls by using API Keys. These keys can be created directly in the GCP console and they can be passed as a parameter through the request header; in this way, you can authenticate to the service without the need of gcloud. Additionally, I suggest you to take a look on this guide that contains the steps required to make REST requests with C#.
You can use this Restlet Client tool to test the following example:
REST content Example
Header
https://texttospeech.googleapis.com/v1beta1/text:synthesize?key=<YOUR_API_KEY>
Body
{
'input':{
'text':'Android is a mobile operating system developed by Google,
based on the Linux kernel and designed primarily for
touchscreen mobile devices such as smartphones and tablets.'
},
'voice':{
'languageCode':'en-gb',
'name':'en-GB-Standard-A',
'ssmlGender':'FEMALE'
},
'audioConfig':{
'audioEncoding':'MP3'
}
}
Finally, in case you want to consume a Service Account JSON file, you can use the Client libraries to create and send your TTS requests to then authenticate them directly from your code; however, this method require to install the TextToSpeech packages.

How to get new emails automatically using Microsoft Graph in a UWP app

I have successfully connected to Microsoft Graph using OAuth. I can receive and send emails from my Office365 account.
But I am completely stuck on how to receive emails automatically, similar to the IMAP IDLE routine.
I am referencing
using Microsoft.Graph;
using Microsoft.Toolkit.Services.MicrosoftGraph;
I have tried Subscription but have no clue what to do next, or even if this is correct.
Subscription sub = new Subscription {
ChangeType = "created",
NotificationUrl = "urn:ietf:wg:oauth:2.0:oob",
Resource = "/users/me/messages",
ExpirationDateTime = DateTimeOffset.Now.AddMinutes(20),
ClientState = "????" // if applicable, what is this
};
To make a subscription you need to expose a notification URL with https (see graph documentation).
POST https://graph.microsoft.com/v1.0/subscriptions
Content-Type: application/json
{
"changeType": "created,updated",
"notificationUrl": > "https://webhook.azurewebsites.net/notificationClient",
"resource": "/me/mailfolders('inbox')/messages",
"expirationDateTime": "2016-03-20T11:00:00.0000000Z",
"clientState": "SecretClientState"
}
If you want examples the graph documentation references a Node.js and asp.net example. Both use ngrok to expose an https URL (just for testing purposes though). The program tunnels HTTP requests through to your localhost (like a reverse proxy). If you have that setup, you have to validate your request. When you send your subscription request the first post message your notification URL will receive is a message with a validation token (see doc). You have to send this validation token back. Now you should receive notifications on your specified notification URL.
You can read about the Subscription resource type and its properties in the graph documentation.
For example, the client state is described as:
Specifies the value of the clientState property sent by the service in each notification. The maximum length is 255 characters. The client can check that the notification came from the service by comparing the value of the clientState property sent with the subscription with the value of the clientState property received with each notification.
Subscriptions are not in possible at this moment using Microsoft Graph for UWP, for notifications the Outlook 365 API should be used. The Microsoft graph api can be used for Auth and other tasks though.
Can Microsoft consider including streaming notifications in Microsoft Graph?

TFS 2015 Web Hook to Slack Integration

I have got a requirement,
When a work item is created (and status -say In progress) in TFS 2015(not vsts), I want my web hook to fire a post method which ll create a Channel in Slack, and invite few folks.
2) Once the Bug is closed - read all the history of channel and push the history back to Bug.
I was able to achieve the same with TFS server side plugin and it's working, but requirement got changed to Web hooks.
Could someone help me on how to achieve custom web hook.which ll enable events from TFS WI to Slack please?
Thanks,
You can not create new incoming webhooks in slack programmatically, but you can use any existing incoming webhook for a new channel.
Just add the channel property in your message to the webhook.
Example from the Slack documentation:
curl -X POST \
--data-urlencode 'payload={"text": "This is posted to #general and comes from *monkey-bot*.", "channel": "#general", "link_names": 1, "username": "monkey-bot", "icon_emoji": ":monkey_face:"}' \
https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX
So to implement your requirement I suggest you create a new channel and invite users as needed through the Slack web API. And then use an existing incoming webhook to send messages to that channel.
TFS has default slack service hook which can post a message to a channel. The API looks like below:
POST http://tfsserver:8080/tfs/DefaultCollection/_apis/hooks/subscriptions?api-version=3.2
Content-Type: application/json
{
"consumerActionId":"postMessageToChannel",
"consumerId":"slack",
"consumerInputs":{
"url":"https://hooks.slack.com/services/xxxxxx"},
"eventType":"workitem.created",
"publisherId":"tfs",
"publisherInputs":{
"areaPath":"",
"workItemType":"",
"projectId":"77e3c775-dc30-4354-xxxx-xxxxxxxxxxxx"}
}
For your second requirement, you need to check Slack Api to see how to read all the history of channel, and use Update work items to update Bug work items.

Unable to send Windows Phone push notification using Azure.NotificationHubs SDK

I'm using Microsoft.Azure.NotificationHubs SDK v1.0.8 to integrate with the azure notification hub and send pushes. While iOS and Android work just fine, my Windows Phone pushes stop working some time ago. There the payload I use:
<?xml version="1.0" encoding="utf-8"?>
<wp:Notification xmlns:wp="WPNotification">
<wp:Toast>
<wp:Text1>Notification Title</wp:Text1>
<wp:Text2>Notification Text!</wp:Text2>
<wp:Param>?param1=value1&param2=value2</wp:Param>
</wp:Toast>
</wp:Notification>
And here is the error I'm getting:
System.ArgumentException: The payload is not in accepted XML format.
The first node should be Tile/Toast. If want to send raw notification,
please set "X-NotificationClass" to "3" in header. at
Microsoft.Azure.NotificationHubs.RegistrationSDKHelper.DetectMpnsTemplateRegistationType(String
body, String errorMsg)
I tried to send the push directly from the azure portal or visual studio - it works fine. So the issue is somewhere in the SDK I guess.
This guide shows how to send the same notification using regular web request and special headers are set to send toast
sendNotificationRequest.Headers.Add("X-WindowsPhone-Target", "toast");
sendNotificationRequest.Headers.Add("X-NotificationClass", "2");
With the Azure SDK I have no way to setup these headers and can use only the following method to send my payload which gives me the error above:
await Hub.SendMpnsNativeNotificationAsync(payload, userIdTag);
What could be wrong in my setup here?
The issues was in this line
<wp:Param>?param1=value1&param2=value2</wp:Param>
& value should be encoded so xml could be properly parsed by the Azure Notification Hub SDK, so I changed it to
<wp:Param>?param1=value1&param2=value2</wp:Param>

Facebook push notification

Im using Facebook C# SDK to post and fetch data. These simple tasks took forever to get running properly. Now I need to notify my webpage(ASP.NET C#) about changes on the specific facebook page so the webpage knows then it should fetch new data.
I have looked at this page : https://developers.facebook.com/docs/graph-api/real-time-updates/v2.2
But as usally with facebook documentations it misses to explain in detail how it works and how to get it working. Where exacly do I create the subscriptions? It says /{app-id}/subscriptions but I have tried this url with my app-id but no page is found?
I have tried to find examples on how to set this up but to no sucess.
Could someone please explain how this works? What do I need to do exacly to get this running?
Facebook subscription works by pinging a URL you own every time data has changed. You need to add a URL you own as a callback URL for Facebook subscriptions to work with
POST /v2.2/{app-id}/subscriptions HTTP/1.1
Host: graph.facebook.com
object=page
callback_url=http%3A%2F%2Fexample.com%2Fcallback%2F
fields=feed
verify_token=thisisaverifystring
In the above API request a POST request is made to add http://example.com/callback/ as the callback URL subscribing to the feed edges of the page object (a page object that the session user owns)
In your callback URL you must have it handle two actions
the initial callback (Handling Verification Requests via the verify token)
saving updated subscriptions (Receiving the Real Time Updates)
Here is an example of what it looks like in PHP
<?php
if ($_REQUEST['hub_verify_token'] === 'thisisaverifystring') {
echo $_REQUEST['hub_challenge'];
}
$file = 'sample.txt';
$inputJSON = json_decode(file_get_contents('php://input'));
file_put_contents($file, file_get_contents('php://input') );
?>

Categories