Skip to content

GetSocial Notifications on iOS

Prerequisite

Setup iOS Push Notifications

Generate Push Notification Certificate

To enable push notifications in your iOS application, go to “Capabilities” tab in your target configuration in XCode and enable “Push Notification”.

On Apple Developer Portal:

  1. Open Apple Developer Portal;

  2. Log into your account;

  3. Open “Certificates, Identifiers & Profiles”;

  4. Find “App IDs” in “Identifiers” section and find your application;

  5. Click “Edit” and scroll to “Push Notifications” configurations;

  6. Click “Create Certificate…” for Development and Production.

  7. Follow the instructions how to create the certificate and download it.

In your .entitlements file you should enter “development” or “production” value for “APS Environment” key, for Development and Production builds respectively.

Export Server .p12 Certificate

Double click on downloaded .cer file (your certificate), it will add it to your Keychain Access and open. Click on “Certificates” section, find your certification, and export it with clicking “Export Apple Development/Production iOS Push Services”, create a password for your certificate and save it somewhere on the disk.

Do it for both Production and Development certificates.

Configure Push Notification on the Dashboard

  1. Login to the GetSocial Dashboard.
  2. Go to the “App settings” section → “Notifications” tab.
  3. Enable iOS notifications by clicking on the switch and upload .p12 certificates from the previous section.
  4. Select what kind of notifications users will receive:

    GetSocial Dashboard - Notifications Type

  5. Select “Sandbox” if your application using development PN, or “Production” for development.

  6. Press “Save” button in the bottom right corner.

  7. Check notifications texts and translations.

    GetSocial Dashboard - Check Notification Text

  8. Check setup sending a test notification to your test device.

    GetSocial Dashboard - Send Test Notification

Warning

Be careful with the configuration of your APS Environment, if you are using “development”, you need to sign your application with Development provisioning and choose “Sandbox” in “Enable Certificate” on GetSocial Dashboard notification configurations.

Enable/disable GetSocial Notifications on the Client Side

By default GetSocial SDK automatically register at push server, and the user will start receiving push notifications.

To prevent auto registration and register for push notifications manually:

  1. Add autoregister-push parameter to getsocial.sh in Build Phases section:

    1
    getsocial.sh --app-id=[GetSocial App Id] --autoregister-push=false
    
  2. To start receiving GetSocial push notifications call:

    1
    [GetSocial registerForPushNotifications];
    

To enable push notifications, just remove the autoregister-push parameter from getsocial.sh or set its value to true.

If you’re not using GetSocial iOS Installer Script check how to disable auto registration for push notifications in the Manual Integration Guide.

Handle Click on Push Notifications

GetSocial UI

If you are using GetSocial UI, our respective view will be opened on click on push notifications: if someone liked your activity or commented under your activity, this activity will be shown on application start.

Important

If GetSocial View is opened automatically by GetSocial UI, you can not set the custom title, UiActionHandler, action button handler, etc. In this case, we recommend you to override the default behavior and open GetSocial View by yourself.

Notification Handler

To handle notification click in your code, we provide GetSocialNotificationHandler, you can set it in application:didFinishLaunchingWithOptions: method of your AppDelegate:

1
2
3
4
5
6
7
[GetSocial setNotificationHandler:^BOOL(GetSocialNotification *notification, BOOL wasClicked) {
    if (!wasClicked) {
        return NO;
    }
    BOOL isHandled = [self processNotification:notification];
    return isHandled;
}];

wasClicked parameter is true when the application was opened by push notification click or false if the application was in the foreground when the notification was received.
You return a BOOL: YES, if notification is handled by your own, or NO if not, so it will be handled by our library.
For example, if you want to prevent of showing GetSocial Activity Feed UI on notification click:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
- (BOOL) processNotification: (GetSocialNotification *)notification {
    switch (notification.action) {
        case GetSocialNotificationActionOpenActivity: {
            // add custom logic here
            NSLog(@"Activity action received");
            return YES;
        }
        default:
            return NO;
    }
}

In this case, you’re showing something you would want to show about the activity, and GetSocial UI will not be shown.

Tip

If you are not using GetSocial UI, we recommend to handle notifications by yourself - better user experience would be to react to notification actions.

Show Notifications In Foreground

If you want to show GetSocial notifications while app is in foreground, follow next:

  1. In your Xcode project, go to: Build TargetBuild Phases.

  2. Find Run Script section with getsocial.sh configured.

  3. Add --foreground-notifications flag

    1
    getsocial.sh --app-id=[GetSocial App Id] --foreground-notifications=true
    

If --foreground-notifications is set to true:

  • Notifications will be shown in the system notification center when your application is in foreground.
  • NotificationHandler will be called after clicking on the notification; parameter wasClicked will be YES.

iOS 9 and lower

--foreground-notifications works on iOS 10+ only, on the older version of iOS the notification will be delegated to the NotificationHandler.

If you’re not using GetSocial iOS Installer Script check how to enable foreground notifications in the Manual Integration Guide.

Get Notifications

To be sure your users didn’t miss any important(or not) notifications, you can query GetSocial notifications and show it to your user inside the app. Or send it once again as local notification. Or handle it in any other convenience method.

Query

You have two types of queries: GetSocialNotificationQuery and GetSocialNotificationsCountQuery to get notifications list and notifications count respectively.

GetSocialNotification can differ by a read status - be read or unread. Also each notification has type, it is one of GetSocialNotificationType constants.

To query notifications by a read status, use one of static factory methods read, unread or readAndUnread.

By default notifications of all types are queries. Also you can explicitly call ofAllTypes to be sure that all types are queries. To specify a list of types you’re interested in call method ofTypes:(NSArray<NSNumber *> *)types and pass one or few types you want to query. You should wrap a constants with shorthand @(GetSocialNotificationTypeComment) or [NSNumber numberWithInteger:GetSocialNotificationTypeComment].

Notifications Count

To get the count of notifications use [GetSocialUser notificationsCountWithQuery:query success:success failure:failure] method. For example, to get a number of all unread notifications for current user:

1
2
3
4
5
6
GetSocialNotificationsCountQuery *query = [GetSocialNotificationsCountQuery unread];
[GetSocialUser notificationsCountWithQuery:query success:^(int count) {
    NSLog(@"Notifications count is %d", count);
} failure:^(NSError *error) {
    NSLog(@"Failed to get notifications count, %@", error);
}];

Notifications List

Similar to notifications count you can fetch a list of notifications using GetSocial.User.getNotifications(query, callback) mehtod:

1
2
3
4
5
6
GetSocialNotificationsQuery *query = [GetSocialNotificationsQuery unread];
[GetSocialUser notificationsWithQuery:query success:^(NSArray<GetSocialNotification *> *notifications) {
    [self showNotifications:notifications];
} failure:^(NSError *error) {
    NSLog(@"Failed to get notifications count, %@", error);
}];

Read And Unread Notifications

All the notifications that are sent to a user are unread. The only exception is notifications that was clicked by a user - such becomes read automatically.

If you want to set notification read or unread use [GetSocialUser setNotificationsRead:notificationIds read:isRead success:success failure:failure] method:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
NSArray<GetSocialNotification *> *notificationsToBeRead = ...;
NSMutableArray<NSString *> *notificationIds = [NSMutableArray new];
for (GetSocialNotification *notification in notificationsToBeRead) {
    [notificationIds addObject:notification.notificationId];
}
BOOL markAsRead = YES;
[GetSocialUser setNotificationsRead:notificationIds read:markAsRead success:^() {
    NSLog(@"Successfully changed notifications status");
} failure:^(NSError *error) {
    NSLog(@"Failed to change notifications status, %@", error);
}];

You can mark just one notification using @[notification.notificationId].

Disable Push Notifications For User

If you don’t want to send notifications to a user, use [GetSocialUser setPushNotificationsEnabled:NO success:success failure:failure]. Notifications will still appear in the notification center. To enable it back use [GetSocialUser setPushNotificationsEnabled:YES success:success failure:failure].

To check current setting value use [GetSocialUser isPushNotificationsEnabledWithSuccess:success failure:failure].

Send Notification

You can also send notifications directly from the SDK to other users just in few lines of code:

1
2
3
4
5
6
7
8
9
NSString *text = [NSString stringWithFormat:@"Hello, %@! Greetings from %@.", GetSocial_NotificationPlaceholder_CustomText_ReceiverDisplayName, GetSocial_NotificationPlaceholder_CustomText_SenderDisplayName];
GetSocialNotificationContent *notification = [GetSocialNotificationContent withText:text];
[notification setTitle:@"Greetings!"];
NSArray *receivers = @[ GetSocial_NotificationPlaceholder_Receivers_Referrer, GetSocial_NotificationPlaceholder_Receivers_ReferredUsers ];
[GetSocialUser sendNotification:receivers withContent:notification success:^(GetSocialNotificationsSummary *summary) {
    NSLog(@"Successfully sent %d notifications!", summary.successfullySentCount);
} failure:^(NSError *error) {
    NSLog(@"Failed to send notifications, %@", error);
}];

This example shows how to send message with dynamic text and title to all the users, whom you invited and the one who invited you.

Receivers

  • Receivers list can not be empty.
  • It may contain up to 25 different user IDs and one or many placeholders prefixed with GetSocial_NotificationPlaceholder_Receivers_ from GetSocialConstants.h.
  • If you will send more that 25 user IDs - method will fail and no notifications will 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 three ways to create a notification content:

  • Dynamically create all the content on the SDK.
  • Use one of the GetSocial templates and override template placeholders.
  • Create your own template and override template placeholders.

Tip

You still can override any content when using the template notification.

  • To create a notification with text use [NotificationContent withText:(NSString *)text].
  • Add a title using [notification setTitle:(NSString *)title] method.
  • You can add one or few default placeholders prefixed with GetSocial_NotificationPlaceholder_CustomText_ from GetSocialConstants.h to both title and text.

Template Notification

To create a template for notifications:

  1. Login to the GetSocial Dashboard.

  2. Go to the “Notifications” section → “Templates” tab.

  3. Press “New Template” button.

    GetSocial Dashboard - Create New Template

  4. Create a new template by giving a unique name and meaningful description.

  5. 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 taken if it wasn’t sent from the SDK side.

  6. To check the list of your custom templates switch to “Custom” tab using radio button.

Now create and setup notification using GetSocial iOS SDK:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
// 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 *notification = [GetSocialNotificationContent withTemplateName:@"new_level_achieved"];
... // set up your notification
[notification addTemplatePlaceholderValue:@"7" forKey:@"USER_LEVEL"]; // add replacement for your placeholders without brackets

[GetSocialUser sendNotification:receivers withContent:notification success:onSuccess failure:onError];
// 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.

Action

To set an action to the notification click, use [notification setActionType:(GetSocialNotificationActionType)type]. By default it is GetSocialNotificationActionCustom.

Add action data using [notification addActionDataValue:(NSString *)value forKey:(NSString *)key] or [notification addActionData:(NSDictionary<NSString *, BSString *> *)dictionary] - second one will add all the keys and values from parameter map. For the default GetSocial actions use one of Notification.Key constants to guarantee the default behaviour. Also you can pass any custom data you want and handle it on the receiver side.

You can send any of predefined actions that will be handled by the SDK, for example to open Activity Feed with id “funny-stories” use:

1
2
3
GetSocialNotificationContent *notification = [GetSocialNotificationContent withText:@"Hey, join the channel with funny stories and tell yours!"];
[notification setAction:GetSocialNotificationActionOpenActivity];
[notification addActionDataValue:@"funny-stories" forKey:GetSocialNotificationKey_OpenActivity_FeedName];

You can send custom action with custom action data and handle it by your own using Notification Handler.

GetSocial Actions

  1. Open User Profile.

    GetSocialNotificationActionOpenProfile is not handled by SDK but you can handle it with Notification Listener. Parameters:

    • GetSocialNotificationKey_OpenProfile_UserId - GetSocial user ID whose profile should be opened.
  2. Open Activity Feed.

    GetSocialNotificationActionOpenActivity is handled by SDK. Will open activity feed with provided name, or activity feed post itself or scroll to the certain comment if provided. Parameters:

    • GetSocialNotificationKey_OpenActivity_ActivityId - Activity Feed post ID.
    • GetSocialNotificationKey_OpenActivity_CommentId - Comment under that activity. Optional.

    or:

    • GetSocialNotificationKey_OpenActivity_FeedName - Feed to Open. Use ActivitiesQuery.GLOBAL_FEED to open global feed.
  3. Open Smart Invites.

    GetSocialNotificationActionOpenInvites is handled by SDK. Will open Smart Invites screen. Has no required parameters.

  4. Open URL.

    GetSocialNotificationActionOpenUrl is handled by SDK. Wil open a system web browser with provided URL. Parameters:

    • GetSocialNotificationKey_OpenUrl_Url - URL to open.

Next Steps

Give us your feedback! Was this article helpful?

😀 🙁