Setup Push Notifications on Unity¶
Prerequisite¶
- Finished Getting Started with GetSocial Unity SDK guide.
Setup Unity Push Notifications¶
Android¶
GetSocial supports sending push notifications via Firebase Cloud Messaging (FCM) or Google Cloud Messaging (GCM). If you’ve already have a Push Notifications set up in your project, skip the Step 1.
-
Add the
Firebase
to your app following the official guide.- Import the
FirebaseInstanceId.unitypackage
to your Unity project.
- Import the
-
Login to the GetSocial Dashboard.
- Go to the Notifications section → Settings tab.
-
Enable Android notifications by clicking on the switch and fill in API Key and Sender ID values.
You can find API Key (Server key) and Sender ID in your application settings in Firebase developer console.
-
Go to Templates tab and select what kind of notifications users will receive:
-
In your Unity project. Go to menu bar → GetSocial → Edit Settings.
-
In Android Settings section ensure that push notifications are enabled:
-
Send a test notification to your test device to check your setup.
iOS¶
Generate Push Notification Certificate¶
- 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.
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.
Adding Push Notification Configs to 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, downloaded in the previous section. -
Select what kind of notifications users will receive:
-
Select “Sandbox” if your application is using development push notifications or “Production” for development.
- In your Unity project. Go to menu bar → GetSocial → Edit Settings.
-
In iOS Settings section ensure that push notifications are enabled and push notifications environment is correct:
-
Check setup sending a test notification to your test device.
Autoregister for Push Notification on the Client Side¶
By default GetSocial SDK automatically registers itself for push notifications during initialization. To prevent this behavior, follow next steps:
- Open your Unity Editor.
- Find GetSocial → Edit Settings in menu.
- In General Settings find Push Notification and disable Register Automatically checkbox.
In this case, GetSocial will not automatically register your device on our Push Server. But you still can register for push notifications manually:
Notifications.RegisterDevice();
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, UiActionListener, action button handler, etc. In this case, we recommend you to override the default behavior and open GetSocial View by yourself.
Notification Listener¶
Receive Listener¶
You can set OnNotificationReceivedListener
, which will be triggered when notification is received and app is in foreground:
Notifications.SetOnNotificationReceivedListener(notification =>
{
Debug.Log("Notification received: " + notification.Text);
});
Click Listener¶
You can customize the default behavior and handle clicks on GetSocial push notifications on your own. To do so:
-
Set Has Custom Notification On Click Listener checked in GetSocial settings in Unity Editor.
-
Set a listener:
Notifications.SetOnNotificationClickedListener(notification => { Debug.Log("Notification clicked: " + notification.Text); if (!HandleAction(notification.Action)) { GetSocial.Handle(notification.Action); // fallback to default behaviour if notification was not handled by your code } });
Read how to handle actions.
Configuration flag customListener
Don’t forget to set this flag, otherwise listener won’t be invoked. If you have it set to true
, GetSocial won’t perform any default handling, you have to call GetSocial.handle(...)
explicitly.
Tip
If you are not using GetSocial UI, we recommend to handle push notifications by yourself - better user experience would be to react to notification actions.
Show Push Notifications In Foreground¶
By default, when your application is in foreground, GetSocial notifications are not shown. Instead, OnNotificationReceivedListener
is called. To show notifications while application is in foreground, follow next steps:
- Open your Unity Editor.
- Find GetSocial → Edit Settings in menu.
- In General Settings find Push Notification and enable Foreground Notifications checkbox.
If Foreground Notifications is checked:
- Push notifications will be shown when your application is in foreground.
OnNotificationReceivedListener
will be called.
iOS 9 and lower
“Foreground Notifications” works on iOS 10+ only, on the older version of iOS the notification will be delegated to the OnNotificationReceivedListener
.
Disable Push Notifications For User¶
If you wan to disable push notification for the user, use Notifications.SetPushNotificationsEnabled(false, onSuccess, onFailure)
. When push notifications are disabled, you still can query for GetSocial Notifications via data API.
To enable it back use Notifications.setPushNotificationsEnabled(true, onSuccess, onFailure)
. To check current setting value use Notifications.ArePushNotificationsEnabled(onSuccess, onFailure)
.
Receiving Rich Push Notifications And Badges (iOS Only)¶
Beginning with iOS 10 push notifications can contain media elements, like images and videos.
Important
Rich Notifications
settings only available in Unity Editor 2018 or above.
By default rich notification support is disabled. In order to enable it click on the checkbox:
If Rich notifications are enabled, a new Notification Service Extension Target
will be created under your Xcode
project.
Important
Notification Service Extension Target
requires a Provisioning Profile
.
Fill in the following values:
Bundle Id
for Notification Extension, likeapp.bundle.id.notificationextension
- Provisioning profile’s name to sign the Extension target with
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.
Customize Notification Icon (Android Only)¶
Notification icon has to be monochromic white image on the transparent background.
By default, GetSocial SDK will use application icon as the push notification icon.
There are two ways to customize the icon. First option is to put image with name getsocial_notification_icon
into Assets/Plugins/Android/res/drawable/
folder.
Gradle build
you have to pack your resources to .aar
file and place it under Assets/Plugins/Android
.
If you want to use an existing icon for notifications, you can set the resource name in the StreamingAssets/getsocial.json
. For instance to use ic_notification_custom.png
from Assets/Plugins/Android/res/drawable/
:
// getsocial.json
{
...
"pushNotifications": {
...
"android": {
...
"icon": "ic_notification_custom"
}
}
}
As a result all notifications coming from GetSocial SDK will use custom icon:
To change the color of the notification(in hex format):
// getsocial.json
{
...
"pushNotifications": {
...
"android": {
...
"color": "#FFCCBBDD"
}
}
}
Also, you can customize large notification icon using StreamingAssets/getsocial.json
. To use ic_large_notification_icon.png
from Assets/Plugins/Android/res/drawable
:
// getsocial.json
{
...
"pushNotifications": {
...
"android": {
...
"largeIcon": "ic_large_notification_icon"
}
}
}
Correct file path and name
You should put the correct name of drawable in your StreamingAssets/getsocial.json
! If your image is named my_custom_image.png
, than your largeIcon
value should be my_custom_image
without extension.
Customize Notifications Channel (Android only)¶
All GetSocial notifications come to separate notifications channel with ID getsocial_channel_id
. By default, channel is called Social
and has empty description. Name is localized to all GetSocial supported languages.
To customize channel name or description, add the following meta data pointing to the string resource to StreamingAssets/getsocial.json
:
// getsocial.json
{
...
"pushNotifications": {
...
"android": {
...
"channelName": "getsocial_notifications_channel_title",
"channelDescription": "getsocial_notifications_channel_description"
}
}
}
You have to create the string resource in strings.xml
with the same name, it will be picked up. You can also localize it as any other string resource.
Gradle build
You have to pack your resources to .aar
file and place it under Assets/Plugins/Android
.
To check your customization, on your phone (Android O, API 26 and higher) go to Settings → Apps and Notifications → [YOUR APP NAME] → Notifications and find your channel. Description can be checked after pressing the channel name at the bottom of the screen.
Next Steps¶
- Send targeted notifications from GetSocial Dashboard
- Send user to user notifications from the client
- Understand push notifications Analytics.