Setup Push Notifications on iOS¶
Prerequisite¶
- Finished Getting Started with GetSocial iOS SDK guide.
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:
- Open Apple Developer Portal;
- Log into your account;
- Open Certificates, Identifiers & Profiles;
- Find App IDs in Identifiers section and find your application;
- Click Edit and scroll to Push Notifications configurations;
- Click Create Certificate… for Development and Production.
- 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¶
- Login to the GetSocial Dashboard.
- Go to the Notifications section → Settings tab.
- Enable iOS notifications by clicking on the switch and upload
.p12
certificates from the previous section. -
Select what kind of notifications users will receive:
-
Select Sandbox if your application is using development PN, or Production for development.
-
Send a test notification to your test device to check your setup.
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.
Autoregister for Push Notification 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:
-
Add
autoregister-push
parameter togetsocial.sh
in Build Phases section:getsocial.sh --app-id=[GetSocial App Id] --autoregister-push=false
-
To start receiving GetSocial push notifications call:
```objc tab=”Objective-C”
[GetSocial registerForPushNotifications];
```swift tab="Swift" 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 push notification click in your code, we provide GetSocialNotificationHandler
, you can set it in application:didFinishLaunchingWithOptions:
method of your AppDelegate
:
```objc tab=”Objective-C”
[GetSocial setNotificationHandler:^BOOL(GetSocialNotification *notification, BOOL wasClicked) {
if (!wasClicked) {
return NO;
}
BOOL isHandled = [self handleAction:notification.notificationAction];
return isHandled;
}];
```swift tab="Swift"
GetSocial.setNotificationHandler { (notification : GetSocialNotification, wasClicked : Bool) -> Bool in
if !wasClicked {
return false
}
let isHandled: Bool = self.handleAction(notification.notificationAction)
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.
Read how to handle actions.
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 Push Notifications In Foreground¶
If you want to show GetSocial push notifications while app is in foreground, follow next:
-
In your Xcode project, go to: Build Target → Build Phases.
-
Find Run Script section with
getsocial.sh
configured. -
Add
--foreground-notifications
flaggetsocial.sh --app-id=[GetSocial App Id] --foreground-notifications=true
If --foreground-notifications
is set to true
:
- Push notifications will be shown in the system notification center when your application is in foreground.
NotificationHandler
will be called after clicking on the notification; parameterwasClicked
will beYES
.
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.
Rich Push Notifications & Badges¶
Beginning with iOS 10 push notifications can contain media elements, like images and videos.
To support this feature, you need to add a Notification Extension
target to your app, as described here:
- In Xcode Select File → New → Target…
- Select Notification Service Extension.
- Enter Product Name and other information.
- Make sure extension and application have the same target iOS version.
- Select your application target in Embed in application line.
- Click on Finish to create extension.
-
Download GetSocialExtension.framework, unzip and add it to your extension’s Frameworks and Libraries section.
-
Add GetSocialExtension.framework to your application’s Embed Frameworks section.
-
(For badge support) Add App Group for both application and extension target:
- Go to Signing & Capabilities.
- Add App Groups capability.
- In App Groups section press + and add group with name
group.YOUR_BUNDLE_ID.getsocial_extension
, where replaceYOUR_BUNDLE_ID
with actual bundle identifier of your app, it should be something likecom.example.app
. Group name should be the same for both application and extension targets. - Don’t forget to add it for both application and extension targets, otherwise badge won’t be properly updated.
Basic setup is now done, let’s write some code.
Replace the content of your *.m
file in your extension with the one you can download from GitHub.
Congratulations, you’re ready to show rich notifications to your users!
To check if your setup is correct, follow the Testing Guide.
The test notification contains a default image, which should be displayed in the received notification. Badge on your application icon should be increased by 1
. After you open your application, badge should be cleared.
Important
Notification Service Extension
will require a Provisioning Profile
.
Disable Push Notifications For User¶
If you wan to disable push notification for the user, use [GetSocialUser setPushNotificationsEnabled:NO success:success failure:failure]
. When push notifications are disabled, you still can query for GetSocial Notifications via data API.
To enable it back use [GetSocialUser setPushNotificationsEnabled:YES success:success failure:failure]
. To check current setting value use [GetSocialUser isPushNotificationsEnabledWithSuccess:success failure:failure]
.
Next Steps¶
- Send targeted notifications from GetSocial Dashboard
- Send user to user notifications from the client
- Understand push notifications Analytics.