Application_to_user notifications problem

Dec 4, 2008 at 6:28 PM
Hi, i'm trying to send an Application_to_user notification using:

facebook.

API api = new facebook.API();
api.ApplicationKey =
"xxx";
api.Secret =
"xxx";
String text = api.notifications.send(string.Empty, "notification text");

 

buti'm getting this error:
user_to_user notifications require a session

Anybody can help me?
thanks.
Jan 9, 2009 at 2:20 AM
I was struggling with the same issue.

This is not the authoritative answer, so take it with a grain of salt:
unfortunately, I found that the .NET API does not support application-to-user notifications unless two painful criteria are met:

The user's (session owner's) key is set before notifications.send is called.
The session owner is online your application (I heard that's an application page request within the last hour) or the session owner has extended permission "offline access" on.

Now bear in mind that with session key set, passing a user id list in the first arg will prepend the notification with the session key owner's name embedded in a link to their profile. (thus, user-to-user[s] notification). IE, Rob notifies Villagra, "This won't work!"

Looking at your code, that user id list is emptyset. So, the notification shall only go to the session owner.

This can work. But it's a weird implementation, eh?

So you're on the right track. The work around I'm using is to persist the session owner's key to this code so it's set before we call notifications.send(). Now this is clearly a pain. Most of the time we want to send application-to-user notifications, we don't expect the target to be online.

That's why we have to wheedle the user into setting that extended permission "offline access" on.

This makes the session key you get infinite, so you can store it somewhere and pull it out when you want to. Sweet.

I believe authorizing offline_access can be done at the initial application authorization (help here?) but in my case, I didn't want to do that. I just wanted to show a link that would allow users who didn't grant the permission to do so:

http://www.facebook.com/authorize.php?api_key=<YOUR API KEY>&ext_perm=offline_access&v=1.0

You may notice in testing authorize.php that once a user authorizes the extended permission, the page doesn't redirect the user back to the application. So add the querystring values:

&next=<LINK TO YOUR APPLICATION>
&next_cancel=<LINK TO YOUR APPLICATION>

Bear in mind that a user can always go to Application Settings and disable this at any time.

So one more thing: to check if your user has offline_access granted to your application, try FacebookService.users.hasAppPermission(facebook.Types.Enums.Extended_Permissions.offline_access)

There may be other solutions. I think you can modify the Facebook .NET source for notifications.send to pass the new argument Type. I'm using v2.0 and the summary stamp reads "FBML for the notifications page" which is consistent with the old PHP implementation. If that tickles your fancy then check out this link:

http://wiki.developers.facebook.com/index.php/Notifications.send

Hope this helps.
 
Mar 26, 2009 at 7:16 PM
You can do app to user notifications using api.sendRequest (which will work only for users of your app, not random facebook users). Here is some sample c# code:

facebook.API api = new facebook.API();
api.ApplicationKey = "your app key here";
api.Secret = "you app secret here";
api.IsDesktopApplication = true;
Dictionary<string,string> parameters = new Dictionary<string,string>();
parameters.Add("method", "Notifications.send");
parameters.Add("to_ids", "comma seperated list of ids ");
parameters.Add("type", "app_to_user");
parameters.Add("notification",  "the text of your notification");
api.SendRequest(parameters,false);