Skip to content

Use case: send friend requests, challenge friends, request gifts

Image with pn and a few buttons

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:

  1. Sending notification with the information about the resource.
  2. 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

  1. Integrate GetSocial SDK. Check the detailed guide on Android, iOS or Unity.

  2. Setup push notifications. Check the detailed setup guide on Android, iOS or Unity.

1. Send notification

  1. 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:

    1
    2
    3
    4
    5
    6
    7
    8
    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();
    
    1
    2
    3
    4
    5
    NSString *clanId = ... ; // Get clan id that user is requesting access to
    
    GetSocialActionBuilder *builder = [[GetSocialActionBuilder alloc] initWithType:@"request-clan-access"];
    [builder addActionData:@{ @"clan-id": clanId, @"sender-user-id": [GetSocialUser userId] }];
    GetSocialAction *action = [builder build];
    
    1
    2
    3
    4
    5
    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()
    
    1
    2
    3
    4
    5
    6
    7
    8
    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();
    
  2. 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”.

    1
    2
    3
    4
    5
    6
    7
    8
    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));
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    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]
    ]];
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    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)
    ])
    
    1
    2
    3
    4
    5
    6
    7
    8
    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.

  3. 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:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    String clanAdminId = ... ; // Get GetSocial User Id for the clan admin
    List<String> notificationReceivers = Arrays.asList(clanAdminId);
    
    GetSocial.User.sendNotification(
            notificationReceivers,
            notificationContent,
            new Callback<NotificationsSummary>() {
                @Override
                public void onSuccess(NotificationsSummary sendSummary) {
                    Log.i("GetSocial", "Successfully sent notifications count: " + sendSummary.getSuccessfullySentCount());
                }
    
                @Override
                public void onFailure(GetSocialException exception) {
                    Log.e("GetSocial", "Failed to send notification, error: " + exception.getMessage());
                }
            });
    
    1
    2
    3
    4
    5
    6
    7
    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);
    }];
    
    1
    2
    3
    4
    5
    6
    7
    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)")
    }
    
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    var clanAdminId = ... ; // Get GetSocial User Id for the clan admin
    
    GetSocial.User.SendNotification(
        new List<string>() { clanAdminId },
        notificationContent,
        sendSummary =>
        {
            Debug.Log("Successfully sent notifications count: " + sendSummary.SuccessfullySentCount);
        },
        error =>
        {
            Debug.LogError("Failed to send notification, error: " + error.Message);
        });
    

2. Receive notification

  1. Handle the click on the push notification. If receiving user has push notifications enabled, GetSocial will send a push notification.

    Access to the clan 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:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    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));
                        }
                    });
    
                    // By returning true, we tell GetSocial SDK that we've already handled the notification
                    return true;
            }
            return false;
        }
    });
    
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    [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]];
            }];
    
            // By returning YES, we tell GetSocial SDK that we've already handled the notification
            return YES;
        }
        return NO;
    }];
    
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    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
        }
    }
    
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    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]);
                });
    
                // By returning true, we tell GetSocial SDK that we've already handled the notification
                return true;
        }
        return false;
    });
    
  2. 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.

    See the detailed implementation guide.

  3. Implement a notification action handler. The last step will be to handle the action on notification click:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    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");
    
        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);
        }
    }
    
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    - (void)handleNotificationClick:(GetSocialNotification *)notification withButton:(GetSocialActionButton *)button {
        NSString *clanId = notification.notificationAction.data[@"clan-id"];
        NSString *senderUserId = notification.notificationAction.data[@"sender-user-id"];
    
        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];
        }
    }
    
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    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)
        }
    }
    
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    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"];
    
        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.

Try in the app

Next steps

Give us your feedback! Was this article helpful?

😀 🙁