Skip to content
This repository has been archived by the owner on Apr 5, 2022. It is now read-only.

adding the app notification send of the graph api #107

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,19 @@ public interface OpenGraphOperations {
String publishAction(String action, String objectType, String objectUrl);

String publishAction(String action, MultiValueMap<String, Object> parameters, boolean builtInAction);

/**
* The way a application sends notification to users
*
* @param userId the facebook user id
* @param href The relative path/GET params of the canvas APP
* @param template The customized text of the notification. See
* https://developers.facebook.com/docs/games/notifications/#templating for
* customized messages
*
* @return true if the user received the notification false otherwise
*
* @see https://developers.facebook.com/docs/games/notifications/
*/
void sendAppNotification(String userId, String href, String template);
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.springframework.social.facebook.api.FeedOperations;
import org.springframework.social.facebook.api.FqlOperations;
import org.springframework.social.facebook.api.FriendOperations;
import org.springframework.social.facebook.api.GraphApi;
import org.springframework.social.facebook.api.GroupOperations;
import org.springframework.social.facebook.api.ImageType;
import org.springframework.social.facebook.api.LikeOperations;
Expand Down Expand Up @@ -103,6 +104,10 @@ public class FacebookTemplate extends AbstractOAuth2ApiBinding implements Facebo

private String applicationNamespace;

private String appId;

private String appSecret;

/**
* Create a new instance of FacebookTemplate.
* This constructor creates a new FacebookTemplate able to perform unauthenticated operations against Facebook's Graph API.
Expand All @@ -121,12 +126,14 @@ public FacebookTemplate() {
* @param accessToken An access token given by Facebook after a successful OAuth 2 authentication (or through Facebook's JS library).
*/
public FacebookTemplate(String accessToken) {
this(accessToken, null);
this(accessToken, null, null, null);
}

public FacebookTemplate(String accessToken, String applicationNamespace) {
public FacebookTemplate(String accessToken, String appId, String appSecret, String appNamespace) {
super(accessToken);
this.applicationNamespace = applicationNamespace;
this.appId = appId;
this.appSecret = appSecret;
this.applicationNamespace = appNamespace;
initialize();
}

Expand Down Expand Up @@ -314,7 +321,7 @@ private void initialize() {
}

private void initSubApis() {
openGraphOperations = new OpenGraphTemplate(this, isAuthorized());
openGraphOperations = new OpenGraphTemplate(this, appId, appSecret, isAuthorized());
userOperations = new UserTemplate(this, getRestTemplate(), isAuthorized());
placesOperations = new PlacesTemplate(this, isAuthorized());
friendOperations = new FriendTemplate(this, getRestTemplate(), isAuthorized());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ class OpenGraphTemplate extends AbstractFacebookOperations implements OpenGraphO

private FitnessActions fitnessActions;

private String appId;

private String appSecret;

public OpenGraphTemplate(GraphApi graphApi, boolean isAuthorizedForUser) {
super(isAuthorizedForUser);
this.graphApi = graphApi;
Expand All @@ -53,6 +57,13 @@ public OpenGraphTemplate(GraphApi graphApi, boolean isAuthorizedForUser) {
this.fitnessActions = new FitnessActionsTemplate(this);
}

public OpenGraphTemplate(FacebookTemplate facebookTemplate, String appId,
String appSecret, boolean authorized) {
this(facebookTemplate, authorized);
this.appId = appId;
this.appSecret = appSecret;
}

public GeneralActions generalActions() {
return generalActions;
}
Expand Down Expand Up @@ -90,6 +101,17 @@ public String publishAction(String action, MultiValueMap<String, Object> paramet
}
return graphApi.publish("me", action, parameters);
}

public void sendAppNotification(String userId, String href, String template) {
requireAuthorization();
MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();

map.set("access_token", appId + "|" + appSecret);
map.set("href", href);
map.set("template", template);

graphApi.post(userId, "notifications", map);
}

private void requireApplicationNamespace() {
String applicationNamespace = graphApi.getApplicationNamespace();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
public class FacebookServiceProvider extends AbstractOAuth2ServiceProvider<Facebook> {

private String appNamespace;
private String appId;
private String appSecret;

/**
* Creates a FacebookServiceProvider for the given application ID, secret, and namespace.
Expand All @@ -36,11 +38,13 @@ public class FacebookServiceProvider extends AbstractOAuth2ServiceProvider<Faceb
*/
public FacebookServiceProvider(String appId, String appSecret, String appNamespace) {
super(new FacebookOAuth2Template(appId, appSecret));
this.appId = appId;
this.appSecret = appSecret;
this.appNamespace = appNamespace;
}

public Facebook getApi(String accessToken) {
return new FacebookTemplate(accessToken, appNamespace);
return new FacebookTemplate(accessToken, appId, appSecret, appNamespace);
}

}