Send notifications to authenticated app users using Facebook API
Monday, September 2, 2013

If you have a Facebook application and want to send notification to the app users through Facebook API, here is how you can achieve it.

App notifications is one of the most effective ways to re-engage app users by sending them a free-form message to take an action, notify about an event or to invite friends. Please note that app notifications can only be sent to users who have authorized the app. Please take additional steps to make sure that the user is authorized if necessary.

Notification can contain text which contain up to 180 characters and once delivered, it will be displayed in the notification area of the user. Simple plain text or templates  can be sent as notifications. For example, You can specify any other authorized users UID in the notification, which when sent, the UID will be expanded to show people's actual names, but they won't be clickable. The click through URL of a notification can be set while sending the notification.

Sending notification using API


Apps can send a notification to a user by the issuing an HTTP POST request to the /USER_ID/notifications Graph API, with an app access token. The format for sending an API notification is as follows.


POST /{recipient_userid}/notifications?access_token=  &template=  &href= 

href - Facebook only supports canvas application to send notifications(But there is a work around for page tab applications by specifying the canvas URL and redirecting the window.top location to Page tab URL).href is the relative path of the canvas application (for example, "index.html?gift_id=123", or "?gift_id=123"). Then Facebook will construct proper target URL based on your app settings. The logic is that, on web, if Canvas setting exists, Facebook always show “Canvas URL + href”. If not, it show nothing. In the future (not in this version), we will also use existing URL re-writing logic to support mobile canvas and native mobile apps. Facebook also appends some special tracking params (fb_source, notif_id, notif_t) to the target URL for developers to track where the traffic is coming from.

One example of target URL displayed in the jewel is: 
https://apps.facebook.com/appnamespace/?fb_source=notification&notif_id=notif_514699839_145756436&ref=notif&notif_t=app_notification

template - The customized text for notification

ref - you can group notifications by using ref parameter and can view reports of these groups in Insights

Templates can be 
  1. Plain text (eg - POST /{recipient_userid}/notifications?access_token= … & href= … & template=Your notification text )
  2. Light weight template -You can refer user_ids in the notification text and the userids will be rendered as users' full name while displaying. The user_ids should be of valid Facebook users and users who have already authorized your app, else Facebook will throw an error. (Eg - POST /{recipient_userid}/notifications?access_token= … &href= … &template=@[596824621]  and @[657682378] started a game with you, play now!)
The response for a successful request will be as follows
{
  "success": true
}
and for an unsuccessfull request will be


  1. If the user you're trying to send to has not authenticated your app, you will get an error 200:"message": "(#200) Cannot send notifications to a user who has not installed the app"
  2. If a person you're trying to tag has not authenticated your app, you will get an error 200: "message": "(#200) Cannot tag users who have not installed the app"
  3. If your template text contains more than 180 characters, we throw an error 100: "message": "(#100) template parameter cannot be longer than 180 characters."


Obtaining App Access token

App access token can be obtained by issuing an HTTP GET request to the following GRAPH API

https://graph.facebook.com/oauth/access_token?client_id=YOUR_APP_ID&client_secret=YOUR_APP_SECRET&grant_type=client_credentials

and you will get the result which contains the app access token   as follows access_token=YOUR_APP_ACCESS_TOKEN. You can save this access token and can be reused until it is refreshed manually by the developer. (You can refresh the app access token from your app settings page in developers.facebook.com)

For detailed documentation, Please refer

 
posted by Abhijeet at 9:11 AM | Permalink | 0 comments
Sending mail using flash and asp.net
Tuesday, November 24, 2009
This article decribes how to send mail from a Flash UI using asp.net. Create a UI like this in flash.

Assign an instance name of the 'Send' movie clip as'myButton'. Also name the text box as 'email'. On the first frame action, write the action script code.

var loadvar=new LoadVars(); //to send variables
var receiveLoad = new LoadVars();//to receive the response


receiveLoad.onLoad=function(success)
{
if(success)
{
trace(unescape(receiveLoad.toString()));//if received succesfully
}
else
{
trace("error");
}
}

this.myButton.onRelease=function(){ //myButton click event


if (email.text.indexOf(" ") != -1 || email.text.indexOf("@") == -1 || email.text.indexOf(".") == -1 ||email.text.length<6||email.text.lastIndexOf(".") {
res="validateion error";
return false;
} //email validation



if(email.text!=""){

loadvar.customp=escape(email.text);//send a variable customp

var url="http://YOUR SERVER NAME/SendMail.ashx?cb="+(Math.round(Math.random()*96584674));//cb-cache burst used to explicitly send new urls so that the request is not cached

loadvar.sendAndLoad(url,receiveLoad,"POST");
}
}

The LoadVars object is an alternative to the loadVariables action for transferring variables between a Flash movie and a server. It uses the methods load , send , and sendAndLoad to communicate with a server.


In Asp.NET create a generic handler with name 'SendMail.ashx' . Write the code to send email in the handler file.

<%@ WebHandler Language="C#" Class="SendMail" %>

using System;
using System.Web;
using System.Net.Mail;
using System.Text.RegularExpressions;

public class SendMail : IHttpHandler {

public void ProcessRequest (HttpContext context) {

string emailId = string.Empty;

if (context.Request["customp"] != null)
{
emailId = HttpUtility.UrlDecode(context.Request["customp"].ToString());
}

if ((emailId != "") && Regex.IsMatch(emailId, @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*")) //check for a valid emailid
{
SmtpClient smtpClient = new SmtpClient();
MailAddress mailFrom = new MailAddress("YOUR EMAIL ID");
MailAddress mailTo = new MailAddress(emailId);
MailMessage mailMessage = new MailMessage(mailFrom, mailTo);
mailMessage.Subject = "Test Mail";
mailMessage.Body = "This is a test mail sent using Flash 8";
mailMessage.DeliveryNotificationOptions = DeliveryNotificationOptions.Never;
smtpClient.Host = "YOUR MAIL SERVER HOST NAME";
smtpClient.Port = YOUR PORT NO;
smtpClient.Credentials = new System.Net.NetworkCredential("YOUR USERNAME", "YOUR PASSWORD");
try
{
smtpClient.Send(mailMessage);
context.Response.Write("&res=1");
}
catch (Exception exException)
{
context.Response.Write("&res="+exException.ToString());
}
}
else
{
context.Response.Write("&res=2");
}

}

public bool IsReusable {
get {
return false;
}
}

}


When executed the loadvar variable posts a request to the sendmal.ashx file with the emailid which inturn sends the mail.

Labels: , , , , , ,

 
posted by Abhijeet at 8:39 PM | Permalink | 0 comments
Custom Splash screen for Silverlight applications
Monday, August 24, 2009


Is the in-stock splash screen in Silverlight application boring you? Here is an easy way to create custom splash screens for your Silverlight application. Essentially the splash screen itself is an XAML file with the desired graphical content and the download progress of the XAP can be tracked using a little javascript code. As the C# managed enironment has not been loaded we can't use C# code to track the progress. Also the XAML file should not be the part of the XAP file as it has to be displayed while the XAP is been loaded.


To craete a custom splashscreen lets first start a Silverlight project with ASP.NET test website.Then, add a new XAML file to your ASP.NET website (not the Silverlight project). To do so, select the ASP.NET website in the Solution Explorer and choose Website > Add New Item. Choose the Silverlight JScript page template, enter a name(CustomSplashScreen.xaml here), and click Add. This XAML file will hold the markup for your splash screen. In our application we are creating XAML like this to build a custom splashscreen like the image below.


<Grid xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<StackPanel VerticalAlignment="Center" HorizontalAlignment="Center">
<Grid >
<Rectangle x:Name="progressBarBackground" Fill="White" Stroke="Black"
StrokeThickness="1" Height="30" Width="200" Grid.Row="1"></Rectangle>
<Rectangle x:Name="progressBar" Fill="Yellow" Height="28" Width="0"
Grid.Row="1" HorizontalAlignment="Left">
</Rectangle>
</Grid>
<TextBlock x:Name="progressText" HorizontalAlignment="Center"
Text="0% downloaded ..."></TextBlock>
</StackPanel>
</Grid>

Next we need to add a little javascript code to track the progress of the XAP file. the event looks like this


<script type="text/javascript">
function onSourceDownloadProgressChanged(sender, eventArgs)
{
sender.findName("progressText").Text = Math.round((eventArgs.progress * 100)) + "% downloaded ...";
sender.findName("progressBar").Width = eventArgs.progress * sender.findName("progressBarBackground").Width;
}
</script>



Also add the following parameters to the <object> tag


<param name = "splashscreensource" value = "CustomSplashScreen.xaml" />
<param name = "onsourcedownloadprogresschanged" value = "onSourceDownloadProgressChanged" />

The parameter 'splashscreensource' is used to identify the splash screen and 'onsourcedownloadprogresschanged' is used to hook up the javascript event.

If instead of <object> tag Silverlight web control is used , it also has two similiar properties named 'SplashScreenSource' and 'OnSourceDownloadProgressChanged'.

Example


<asp:Silverlight ID="silverlightControl" runat="server"
Source="~/ClientBin/MyApp.xap" Version="2.0" Width="800" Height="500"
SplashScreenSource="~/CustomSplashScreen.xaml"
OnSourceDownloadProgressChanged= "onSourceDownloadProgressChanged" />

To test the application add some large mp3 files and set the build action of each one to Resource so it’s added to the XAP file and will be downloaded slowly in the local system.

Labels: , , , , , ,

 
posted by Abhijeet at 4:31 AM | Permalink | 0 comments