Skip to content

Setup Push Notifications on Unity

Prerequisite

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.

  1. Add the Firebase to your app following the official guide.

    • Import the FirebaseInstanceId.unitypackage to your Unity project.
  2. Login to the GetSocial Dashboard.

  3. Go to the Notifications section → Settings tab.
  4. 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.

    GetSocial Dashboard - Enable Push Notifications

  5. Go to Templates tab and select what kind of notifications users will receive:

    GetSocial Dashboard - Notifications Type

  6. In your Unity project. Go to menu bar → GetSocialEdit Settings.

  7. In Android Settings section ensure that push notifications are enabled:

    Setup Push Notifications iOS in Unity Editor

  8. Send a test notification to your test device to check your setup.

    GetSocial Dashboard - Send Test Notification

iOS

Generate Push Notification Certificate

  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.

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

  1. Login to the GetSocial Dashboard.
  2. Go to the Notifications section → Settings tab.
  3. Enable iOS notifications by clicking on the switch and upload .p12 certificates, downloaded in the previous section.
  4. Select what kind of notifications users will receive:

    GetSocial Dashboard - Notifications Type

  5. Select “Sandbox” if your application is using development push notifications or “Production” for development.

  6. In your Unity project. Go to menu bar → GetSocialEdit Settings.
  7. In iOS Settings section ensure that push notifications are enabled and push notifications environment is correct:

    Setup Push Notifications iOS in Unity Editor

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

  1. Open your Unity Editor.
  2. Find GetSocialEdit Settings in menu.
  3. In General Settings find Push Notification and disable Register Automatically checkbox.

Setup Push Notifications in Unity Editor

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:

  1. Set Has Custom Notification On Click Listener checked in GetSocial settings in Unity Editor.

    GetSocial Unity Editor - Has Custom Listener

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

  1. Open your Unity Editor.
  2. Find GetSocialEdit Settings in menu.
  3. 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:
Setup Rich Push Notifications iOS in Unity Editor

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:

  1. Bundle Id for Notification Extension, like app.bundle.id.notificationextension
  2. 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:

Customized Notification 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"
        }
    }
}

Customized 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 SettingsApps 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

Give us your feedback! Was this article helpful?

😀 🙁