Send GetSocial Notifications from iOS SDK¶
Prerequisite¶
- Finished Getting Started with GetSocial iOS SDK guide.
Send Notifications¶
You can send GetSocial Notification to any user in you application. To send notification you have to:
- Create a list of receivers. The list should contain GetSocial user IDs.
- Create a notification content.
```objc tab=”Objective-C”
// The list of GetSocial user IDs whom we will send notification to
NSArray *receivers = … ;
// The content of the notification
GetSocialMediaAttachment *attachment = … ;
GetSocialAction *action = …;
GetSocialNotificationContent *notificationContent = [GetSocialNotificationContent withText:@”Notification text”];
[notificationContent setTitle:@”Greetings!”];
[notificationContent setAction:action];
[notificationContent setMediaAttachment:attachment];
[GetSocialUser sendNotification:receivers withContent:notificationContent success:^(GetSocialNotificationsSummary *summary) {
NSLog(@”Successfully sent %d notifications!”, summary.successfullySentCount);
} failure:^(NSError *error) {
NSLog(@”Failed to send notifications, %@”, error);
}];
```swift tab="Swift"
// The list of GetSocial user IDs whom we will send notification to
let receivers : [String] = ...
// The content of the notification
let attachment : GetSocialMediaAttachment = ...
let action: GetSocialAction = ...
let notificationContent = GetSocialNotificationContent.withText("Notification text")
notificationContent.setTitle("Greetings!")
notificationContent.setAction(action)
notificationContent.setMediaAttachment(attachment)
GetSocialUser.sendNotification(receivers, with: notificationContent, success: { (summary : GetSocialNotificationsSummary) in
print("Successfully sent \(summary.successfullySentCount) notifications!")
}) { (error : Error) in
print("Failed to send notifications, error: \(error.localizedDescription)")
}
Receivers¶
- Receivers list can not be empty.
-
It may contain up to 25 unique user IDs and one or many placeholders from
GetSocial_NotificationPlaceholder_Receivers_
fromGetSocialConstants.h
.```objc tab=”Objective-C”
NSArray *receivers = @[
GetSocial_NotificationPlaceholder_Receivers_Referrer,
GetSocial_NotificationPlaceholder_Receivers_ReferredUsers,
@”[put user 1 id here]”,
@”[put user 2 id here]”];
```swift tab="Swift" let receivers = [GetSocial_NotificationPlaceholder_Receivers_Referrer, GetSocial_NotificationPlaceholder_Receivers_ReferredUsers, "put user 1 id here", "put user 2 id here"]
-
If you will send more that 25 user IDs - method will fail and notification will not be sent.
- If you mentioned one user twice or user is in two or more placeholder groups - the notification will be sent only once.
- If our service can not send the notification to one or many users, it will be delivered to all other users. You can check the number of successfully sent notifications in the response.
Notification Content¶
There are two ways to create a notification content:
-
Create all the content on the client side. You can customize notification text (mandatory), title, configure notification click action and attach media.
```objc tab=”Objective-C”
GetSocialNotificationContent *notificationContent = [GetSocialNotificationContent
withText:@”Notification text”];
```swift tab="Swift" let notificationContent = GetSocialNotificationContent.withText("Notification text")
-
Use templates provided by GetSocial or create your custom template on the GetSocial Dashboard. You can override any content in the template dynamically on the client side.
```objc tab=”Objective-C”
GetSocialNotificationContent *notificationContent = [GetSocialNotificationContent
withTemplateName:@”template-name”];
```swift tab="Swift" let notificationContent = GetSocialNotificationContent.withTemplateName("template-name")
Action¶
To set an action to the notification click, use notification.withAction(Action action)
.
```objc tab=”Objective-C”
GetSocialAction *action = [[[GetSocialActionBuilder alloc] initWithType:GetSocialActionOpenInvites] build];
GetSocialNotificationContent *notificationContent = [GetSocialNotificationContent
withText:@”Invite Friends”];
[notificationContent setAction:action];
```swift tab="Swift"
let action = GetSocialActionBuilder.init(type: GetSocialActionType.openInvites).build()
let notificationContent = GetSocialNotificationContent.withText("Invite Friends")
notificationContent.setAction(action)
Read how to create actions.
Action Buttons¶
To add an action button use [notification addActionButton:(GetSocialActionButton *)button]
or [notification addActionButtons:(NSArray<GetSocialActionButton *> *)actionButtons]
to add multiple buttons at once.
```objc tab=”Objective-C”
GetSocialAction *action = [[[GetSocialActionBuilder alloc] initWithType:@”add_friend”] build];
GetSocialNotificationContent *notificationContent = [GetSocialNotificationContent withText:[NSString stringWithFormat:@”Add %@ to friends”, GetSocial_NotificationPlaceholder_CustomText_SenderDisplayName]];
[notificationContent setAction:action];
[notificationContent addActionButtons:@[
[GetSocialActionButton createWithTitle:@”Accept” andActionId:GetSocialActionIdConsume],
[GetSocialActionButton createWithTitle:@”Decline” andActionId:GetSocialActionIdIgnore]
]];
```swift tab="Swift"
let action = GetSocialActionBuilder(type: GetSocialActionType(rawValue: "add_friend")).build()
let notificationContent = GetSocialNotificationContent.withText("Add \(GetSocial_NotificationPlaceholder_CustomText_SenderDisplayName) to friends")
notificationContent.setAction(action)
notificationContent.add([
GetSocialActionButton.create(withTitle: "Accept", andActionId: GetSocialActionId.consume),
GetSocialActionButton.create(withTitle: "Decline", andActionId: GetSocialActionId.ignore)
])
Media Attachment¶
It’s possible to send image and video content in notification. To set image or video use [content setMediaAttachment:attachment]
:
```objc tab=”Objective-C”
GetSocialMediaAttachment *attachment = … ;
GetSocialNotificationContent *content = [GetSocialNotificationContent withText:@”Check this cool image”];
[content setMediaAttachment:attachment];
```swift tab="Swift"
let mediaAttachment: GetSocialMediaAttachment = ...
let content = GetSocialNotificationContent.withText("Check this cool image")
content.setMediaAttachment(mediaAttachment)
Check full Media Attachment guide to learn about supported image, GIF and video formats and limitations.
Notification Templates¶
To create a template for notifications:
-
Login to the GetSocial Dashboard.
-
Go to the Notifications section → Templates tab.
-
Press New Template button.
-
Create a new template by giving a unique name and meaningful description.
-
Setup the notification content. You can add translations, emojis, default placeholders (Sender/Receiver display name) or custom placeholders that can be replaced on the SDK. Also you can set the fallback value for each placeholder which will be used if it wasn’t sent from the SDK side.
-
To check the list of your custom templates switch to Custom tab using radio button.
Now create and setup notification using GetSocial Android SDK:
```objc tab=”Objective-C”
// Text for the template “new_level_achieved” on the Dashboard:
// “Your friend [SENDER_DISPLAY_NAME] just reached [USER_LEVEL] lvl! Try to beat his score!”
GetSocialNotificationContent *notificationContent = [GetSocialNotificationContent withTemplateName:@”new_level_achieved”];
… // set up your notification
[notificationContent addTemplatePlaceholderValue:@”7” forKey:@”USER_LEVEL”]; // add replacement for your placeholders without brackets
[GetSocialUser sendNotification:receivers withContent:notificationContent success:onSuccess failure:onError];
// Your recipients will receive text:
// “Your friend John Doe just reached 7 lvl! Try to beat his score!”
```swift tab="Swift"
// Text for the template "new_level_achieved" on the Dashboard:
// "Your friend [SENDER_DISPLAY_NAME] just reached [USER_LEVEL] lvl! Try to beat his score!"
let notificationContent = GetSocialNotificationContent.withTemplateName("new_level_achieved")
... // set up your notification
notificationContent.addTemplatePlaceholderValue("7", forKey: "USER_LEVEL") // add replacement for your placeholders without brackets
GetSocialUser.sendNotification(receivers, with: notificationContent, success: onSuccess, failure: onFailure)
// Your recipients will receive text:
// "Your friend John Doe just reached 7 lvl! Try to beat his score!"
[SENDER_DISPLAY_NAME]
is automatically replaced with sender display name. You can check all possible placeholders in GetSocialConstants.h
prefixed with GetSocial_NotificationPlaceholder_CustomText_
.
Notification Sounds¶
Sound customization is supported via Templates only. But first you have to add sound file to your application:
- Prepare your audio file. It should be in
aiff
,wav
, orcaf
formats and not longer than30
seconds. You can read more about this on the official Apple documentation in Preparing Custom Alert Sounds section. - Add your file to your application target in the Xcode. Make sure it’s added by checking if the file is listed in Copy Bundle Resources section of your target.
- After file is added you should create a template on the Dashboard and specify this file name with extension in iOS Sound configuration.
- Build the application and run it on a device.
- Send notification to the user authenticated on this device. You can send notification via Smart Targeting or SDK. When the notification arrives to the device you should hear custom sound instead of the default one. Make sure your device is not muted and volume is on.
Badges¶
Badges are displayed only on iOS, on Android devices it will be ignored.
- Make sure you’ve completed setup guide for badges.
-
Create badge:
- To send a specific value use
[GetSocialNotificationBadge setTo:3]
- this will set badge on the recepient side to3
. - To increase/decrease badge value use
[GetSocialNotificationBadge increaseBy:1]
. For example if badge was5
, it will be set to6
after this push notification arrives.
- To send a specific value use
-
Add badge to notification content:
[notificationContent setBadge:badge]
.
Example - send notification and increase badge by 3
:
```objc tab=”Objective-C”
GetSocialNotificationContent *notificationContent = [GetSocialNotificationContent notificationWithText:@”You have 3 new messages!”];
[notificationContent setBadge:[GetSocialNotificationBadge increaseBy:3]];
[GetSocialUser sendNotification:receivers withContent:notificationContent success:onSuccess failure:onError];
```swift tab="Swift"
let notificationContent = GetSocialNotificationContent.withText("You have 3 new messages!")
notificationContent.setBadge(GetSocialNotificationBadge.increaseBy(3))
GetSocialUser.sendNotification(receivers, with: notificationContent, success: onSuccess, failure: onFailure)
Badge is cleared automatically when application is opened. If you want to clear a badge along with push notification, use setTo
with value 0
.