Use case: send friend requests, challenge friends, request gifts¶
Overview¶
GetSocial Notifications provide a generic API to implement sending in-app resources or requesting access to them from other users. More specifically you can enable your users to:
- Send friend requests
- Send invitations to join a group/clan/challenge
- Send in-app gifts
- Request access to join group/clan/challenge
- Request lives or energy packs from friends
- Invite friends to join multiplayer session
- Notify friend of their turn to play
Implementation guide¶
Sending/requesting resources uses ability to send user to user notifications directly from the GetSocial mobile SDKs.
Any of the use cases described above consists of two parts:
- Sending notification with the information about the resource.
- Receiving notification to get the information about the resource and accepting/granting access to it.
Below we’ll show an example how a user can request access to the clan from the clan admin.
0. Prerequisite¶
-
Integrate GetSocial SDK. Check the detailed guide on Android, iOS or Unity.
-
Setup push notifications. Check the detailed setup guide on Android, iOS or Unity.
1. Send notification¶
-
Describe an action and collect information about the resource.
GetSocial Notifications allow you to attach an action using key-value pairs. Action can be predefined by GetSocial, e.g.,
OPEN_URL
,OPEN_ACTIVITY
; or you can define your custom action.For instance to request access to the clan:
```java tab=”Android(Java)”
String clanId = … ; // Get clan id that user is requesting access to// Create an action with custom request-clan-access type
Action notificationAction = Action.builder(“request-clan-access”)
// Attach information about the clan and user that requests the access
.addActionData(“clan-id”, clanId)
.addActionData(“sender-user-id”, GetSocial.User.getId())
.build();
```kotlin tab="Android(Kotlin)" val clanId : String = ... // Get clan id that user is requesting access to // Create an action with custom request-clan-access type val notificationAction = Action.builder("request-clan-access") // Attach information about the clan and user that requests the access .addActionData("clan-id", clanId) .addActionData("sender-user-id", GetSocial.User.getId()) .build()
```objc tab=”iOS(Objective-C)”
NSString *clanId = … ; // Get clan id that user is requesting access toGetSocialActionBuilder *builder = [[GetSocialActionBuilder alloc] initWithType:@”request-clan-access”];
[builder addActionData:@{ @”clan-id”: clanId, @”sender-user-id”: [GetSocialUser userId] }];
GetSocialAction *action = [builder build];
```swift tab="iOS(Swift)" let clanId: String = ... // Get clan id that user is requesting access to let builder = GetSocialActionBuilder.init(type: GetSocialActionType.init("request-clan-access")) builder.addActionData([ "clan-id": clanId, "sender-user-id": GetSocialUser.userId() ]) let action = builder.build()
```c# tab=”Unity”
var clanId = … ; // Get clan id that user is requesting access to// Create an action with custom request-clan-access type
var notificationAction = GetSocialAction.CreateBuilder(“request-clan-access”)
// Attach information about the clan and user that requests the access
.AddActionData(“clan-id”, clanId)
.AddActionData(“sender-user-id”, GetSocial.User.Id)
.Build();
``` -
Create notification. Next step will be to create a notification and describe possible actions for the resource, e.g., request to get access to clan can be “Granted” or “Declined”.
```java tab=”Android(Java)”
String clanName = … ; // Get clan name
String text = String.format(“Your friend %s requested access to %s clan”, SendNotificationPlaceholders.CustomText.SENDER_DISPLAY_NAME, clanName);NotificationContent notificationContent = NotificationContent.notificationWithText(text)
.withTitle(“Clan request”)
.withAction(notificationAction)
.addActionButton(ActionButton.create(“Approve”, ActionButton.CONSUME_ACTION))
.addActionButton(ActionButton.create(“Reject”, ActionButton.IGNORE_ACTION));
```kotlin tab="Android(Kotlin)" val clanName : String // Get clan name val text = "Your friend ${SendNotificationPlaceholders.CustomText.SENDER_DISPLAY_NAME} requested access to ${clanName} clan" val notificationContent = NotificationContent.notificationWithText(text) .withTitle("Clan request") .withAction(notificationAction) .addActionButton(ActionButton.create("Approve", ActionButton.CONSUME_ACTION)) .addActionButton(ActionButton.create("Reject", ActionButton.IGNORE_ACTION))
```objc tab=”iOS(Objective-C)”
NSString *clanName = … ; // Get clan name
NSString *text = [NSString stringWithFormat:@”Your friend %s requested access to %s clan”, GetSocial_NotificationPlaceholder_CustomText_SenderDisplayName, clanName];GetSocialNotificationContent *notificationContent = [GetSocialNotificationContent withText:text];
[notificationContent setAction:action];
[notificationContent addActionButtons:@[
[GetSocialActionButton createWithTitle:@”Approve” andActionId:GetSocialActionIdConsume],
[GetSocialActionButton createWithTitle:@”Reject” andActionId:GetSocialActionIdIgnore]
]];
```swift tab="iOS(Swift)" let clanName = ... // Get clan name let text = "Your friend \(GetSocial_NotificationPlaceholder_CustomText_SenderDisplayName) requested access to \(clanName) clan" let notificationContent = GetSocialNotificationContent.withText(text) notificationContent.setAction(action) notificationContent.add([ GetSocialActionButton.create(withTitle: "Approve", andActionId: GetSocialActionId.consume), GetSocialActionButton.create(withTitle: "Reject", andActionId: GetSocialActionId.ignore) ])
```c# tab=”Unity”
var clanName = … ; // Get clan name
var text = String.Format(“Your friend {0} requested access to {1} clan”, SendNotificationPlaceholders.CustomText.SenderDisplayName, clanName);var notificationContent = NotificationContent.NotificationWithText(text)
.WithTitle(“Clan request”)
.WithAction(notificationAction)
.AddActionButton(ActionButton.Create(“Approve”, ActionButton.ConsumeAction))
.AddActionButton(ActionButton.Create(“Reject”, ActionButton.IgnoreAction));
```For each notification sent via GetSocial we will generate corresponding push notification on the receiver side.
-
Send notification to one or more users. The final step is to send the notification to the user(s) who can activate the notification.
In our example we have to send a notification to the clan admin:
```java tab=”Android(Java)”
String clanAdminId = … ; // Get GetSocial User Id for the clan admin
ListnotificationReceivers = Arrays.asList(clanAdminId); GetSocial.User.sendNotification(
notificationReceivers,
notificationContent,
new Callback() {
@Override
public void onSuccess(NotificationsSummary sendSummary) {
Log.i(“GetSocial”, “Successfully sent notifications count: ” + sendSummary.getSuccessfullySentCount());
}1 2 3 4 5
@Override public void onFailure(GetSocialException exception) { Log.e("GetSocial", "Failed to send notification, error: " + exception.getMessage()); } });
kotlin tab=”Android(Kotlin)”
val clanAdminId : String // Get GetSocial User Id for the clan admin
val notificationReceivers = Arrays.asList(clanAdminId)
GetSocial.User.sendNotification(
notificationReceivers,
notificationContent,
object : Callback{
override fun onSuccess(sendSummary: NotificationsSummary) {
println(“Successfully sent notifications count: ${sendSummary.getSuccessfullySentCount()}”)
}
override fun onFailure(exception: GetSocialException) {
println(“Failed to send notification, error: ${exception.message}”)
}
})
``````objc tab=”iOS(Objective-C)”
NSString *clanAdminId = … ; // Get GetSocial User Id for the clan admin[GetSocialUser sendNotification:@[ clanAdminId ] withContent:notificationContent success:^(GetSocialNotificationsSummary *summary) {
NSLog(@”Successfully sent notifications count: %d”, summary.successfullySentCount);
} failure:^(NSError *error) {
NSLog(@”Failed to send notifications, error: %@”, error);
}];
```swift tab="iOS(Swift)" let clanAdminId: String = ... // Get GetSocial User Id for the clan admin GetSocialUser.sendNotification([ clanAdminId ], with: notificationContent, success: { (summary : GetSocialNotificationsSummary) in print("Successfully sent notifications count: \(summary.successfullySentCount)") }) { (error : Error) in print("Failed to send notifications, error: \(error.localizedDescription)") }
```c# tab=”Unity”
var clanAdminId = … ; // Get GetSocial User Id for the clan adminGetSocial.User.SendNotification(
new List() { clanAdminId },
notificationContent,
sendSummary =>
{
Debug.Log(“Successfully sent notifications count: ” + sendSummary.SuccessfullySentCount);
},
error =>
{
Debug.LogError(“Failed to send notification, error: ” + error.Message);
});
```
2. Receive notification¶
-
Handle the click on the push notification. If receiving user has push notifications enabled, GetSocial will send a push notification.
GetSocial SDK can handle the predefined actions, but in our example, we’ve defined a custom one. To handle it we have to implement a notification listener:
```java tab=”Android(Java)”
GetSocial.setNotificationListener(new NotificationListener() {
@Override
public boolean onNotificationReceived(Notification notification, boolean wasClicked) {
if (!wasClicked) {
// if if wasn’t clicked we won’t act to notification, though we may show dialog or in-app notification
return false;
}
switch (notification.getAction().getType()) {
case “request-clan-access”:
// show the dialog with incoming notification, you have to implement this method
showDialog(notification.getTitle(), notification.getText(), notification.getActionButtons(), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
handleNotificationClick(notification, notification.getActionButtons().get(i));
}
});1 2 3 4 5
// By returning true, we tell GetSocial SDK that we've already handled the notification return true; } return false; }
});
kotlin tab=”Android(Kotlin)”
GetSocial.setNotificationListener { notification, wasClicked ->
if (!wasClicked) {
// if if wasn’t clicked we won’t act to notification, though we may show dialog or in-app notification
return@setNotificationListener false
}
when(notification.action.type) {
“request-clan-access” -> {
// show the dialog with incoming notification, you have to implement this method
showDialog(notification.title, notification.text,
notification.actionButtons, { dialog, which ->
handleNotificationClick(notification, notification.actionButtons.get(which))
})
// By returning true, we tell GetSocial SDK that we’ve already handled the notification
return@setNotificationListener true
}
}
return@setNotificationListener false
}
``````objc tab=”iOS(Objective-C)”
[GetSocial setNotificationHandler:^BOOL(GetSocialNotification *notification, BOOL wasClicked) {
if (!wasClicked) {
// if if wasn’t clicked we won’t act to notification, though we may show dialog or in-app notification
return NO;
}
if ([notification.notificationAction.type isEqualToString:@”request-clan-access”]) {
// show the dialog with incoming notification, you have to implement this method
[self showDialogWithTitle:notification.title withText:notification.text withButtons:notification.actionButtons withClickHandler:^(int selectedButton) {
[self handleNotificationClick:notification withButton:notification.actionButtons[selectedButton]];
}];1 2 3 4
// By returning YES, we tell GetSocial SDK that we've already handled the notification return YES; } return NO;
}];
swift tab=”iOS(Swift)”
GetSocial.setNotificationHandler { (notification, wasClicked) -> Bool in
if !wasClicked {
// if if wasn’t clicked we won’t act to notification, though we may show dialog or in-app notification
return false
}
switch(notification.notificationAction.type.rawValue) {
case “request-clan-access”:
// show the dialog with incoming notification, you have to implement this method
showDialog(withTitle: notification.title, withText: notification.text, withButtons: notification.actionButtons, withClickHandler: { (selectedButton: Int) in
self.handleNotificationClick(notification, with: notification.actionButtons[selectedButton])
})
// By returning true, we tell GetSocial SDK that we’ve already handled the notification
return true
default:
return false
}
}
``````c# tab=”Unity”
GetSocial.SetNotificationListener((notification, clicked) =>
{
if (!wasClicked)
{
// if if wasn’t clicked we won’t act to notification, though we may show dialog or in-app notification
return false;
}
switch (notification.NotificationAction.Type)
{
case “request-clan-access”:
// show the dialog with incoming notification, you have to implement this method
ShowDialog(notification.Title, notification.Text, notification.ActionButtons, (selectedPosition) =>
{
HandleNotificationClick(notification, notification.ActionButtons[selectedPosition]);
});1 2 3 4
// By returning true, we tell GetSocial SDK that we've already handled the notification return true; } return false;
});
``` -
Implement in-app notification center.
According to Localytics data, only 53% of the users have push notifications enabled. Implementing an in-app notifications center will provide a way to deliver notifications to 100% of the users.
-
Implement a notification action handler. The last step will be to handle the action on notification click:
```java tab=”Android(Java)”
void handleNotificationClick(Notification notification, ActionButton clickedActionButton) {
// Get data we attached about the clan and user
String clanId = notification.getAction().getData().get(“clan-id”);
String senderUserId = notification.getAction().getData().get(“sender-user-id”);1 2 3 4 5 6 7 8 9 10 11
if (ActionButton.CONSUME_ACTION.equalsIgnoreCase(clickedActionButton.getId())) { // Call your API to grant clan access grantClanAccess(clanId, senderUserId); // Set notification status to Consumed GetSocial.User.setNotificationsStatus(Collections.singletoneList(notification.getId()), NotificationStatus.CONSUMED, completionCallback); } else if (ActionButton.IGNORE_ACTION.equalsIgnoreCase(clickedActionButton.getId())) { // Set notification status to Ignored GetSocial.User.setNotificationsStatus(Collections.singletoneList(notification.getId()), NotificationStatus.IGNORED, completionCallback); }
}
kotlin tab=”Android(Kotlin)”
fun handleNotificationClick(notification: Notification, clickedActionButton: ActionButton) {
// Get data we attached about the clan and user
val clanId = notification.action.data[“clan-id”]
val senderUserId = notification.action.data[“sender-user-id”]
if (ActionButton.CONSUME_ACTION.equals(clickedActionButton.id)) {
// Call your API to grant clan access
grantClanAccess(clanId, senderUserId)
// Set notification status to Consumed
GetSocial.User.setNotificationsStatus(Collections.singletonList(notification.id),
NotificationStatus.CONSUMED, callback)
} else if (ActionButton.IGNORE_ACTION.equals((clickedActionButton.id))) {
// Set notification status to Ignored
GetSocial.User.setNotificationsStatus(Collections.singletonList(notification.id),
NotificationStatus.IGNORED, callback)
}
}
``````objc tab=”iOS(Objective-C)”
- (void)handleNotificationClick:(GetSocialNotification *)notification withButton:(GetSocialActionButton *)button {
NSString *clanId = notification.notificationAction.data[@”clan-id”];
NSString *senderUserId = notification.notificationAction.data[@”sender-user-id”];1 2 3 4 5 6 7 8 9 10
if ([GetSocialActionIdConsume isEqualToString:button.actionId]) { // Call your API to grant clan access [self grantAccessToClan:clanId forUser:senderUserId]; // Set notification status to Consumed [GetSocialUser setNotificationsStatus:@[ notification.notificationId ] status:GetSocialNotificationStatusConsumed success:onSuccess failure:onFailure]; } else if ([GetSocialActionIdIgnore isEqualToString:button.actionId]) { // Set notification status to Consumed [GetSocialUser setNotificationsStatus:@[ notification.notificationId ] status:GetSocialNotificationStatusIgnored success:onSuccess failure:onFailure]; }
}
swift tab=”iOS(Swift)”
func handleNotificationClick(_ notification: GetSocialNotification, with button: GetSocialActionButton) {
let clanId = notification.notificationAction.data[GetSocialActionDataKey.init(“clan-id”)]
let senderUserId = notification.notificationAction.data[GetSocialActionDataKey.init(“sender-user-id”)]
if (GetSocialActionId.consume == button.actionId) {
// Call your API to grant clan access
self.grantAccess(toClan: clanId, forUser: senderUserId)
// Set notification status to Consumed
GetSocialUser.setNotificationsStatus([ notification.notificationId ], status: GetSocialNotificationStatus.consumed, success: onSuccess, failure: onFailure)
} else if (GetSocialActionId.ignore == button.actionId) {
// Set notification status to Consumed
GetSocialUser.setNotificationsStatus([notification.notificationId], status: GetSocialNotificationStatus.ignored, success: onSuccess, failure: onFailure)
}
}
``````c# tab=”Unity”
void HandleNotificationClick(Notification notification, ActionButton clickedActionButton)
{
// Get data we attached about the clan and user
var clanId = notification.NotificationAction.Data[“clan-id”];
var senderUserId = notification.NotificationAction.Data[“sender-user-id”];1 2 3 4 5 6 7 8 9 10 11 12
if (ActionButton.ConsumeAction.Equals(clickedActionButton.Id) { // Call your API to grant clan access GrantClanAccess(clanId, senderUserId); // Set notification status to Consumed GetSocial.User.setNotificationsStatus(new List<string> { notification.Id }, NotificationStatus.Consumed, OnSuccess, OnFailure); } else if (ActionButton.IgnoreAction.Equals(clickedActionButton.Id)) { // Set notification status to Ignored GetSocial.User.setNotificationsStatus(new List<string> { notification.Id }, NotificationStatus.Ignored, OnSuccess, OnFailure); }
}
```
Try it out¶
Download DevFest Ukraine conference application to check sending of friend request based in action.