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:

1
GetSocial.RegisterForPushNotification();

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

To handle notification click in your code, we provide NotificationListener, you can set it in Awake method of your MonoBehaviour derived class.

1
2
3
4
5
6
7
8
GetSocial.SetNotificationListener ((notification, wasClicked) => {
    if (!wasClicked)
    {
        return false;
    }
    bool isHandled = handleAction(notification.NotificationAction);
    return isHandled;
});

Read how to handle actions.

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: true if notification is handled by your own, or false if not, so it will be handled by our library.

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

By default, when your application is in foreground, GetSocial notifications are not shown. Instead, NotificationListener 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.
  • NotificationListener will be called after clicking on the notification; parameter wasClicked will be true.

iOS 9 and lower

“Foreground Notifications” works on iOS 10+ only, on the older version of iOS the notification will be delegated to the NotificationListener.

Disable Push Notifications For User

If you wan to disable push notification for the user, use GetSocial.User.SetPushNotificationsEnabled(false, onSuccess, onFailure). When push notifications are disabled, you still can query for GetSocial Notifications via data API.

To enable it back use GetSocial.User.setPushNotificationsEnabled(true, onSuccess, onFailure). To check current setting value use GetSocial.User.IsPushNotificationsEnabled(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)

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.

Notification icons with Gradle build

If you’re using Gradle build system in your Build Settings for Android, 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 Android Manifest. For instance to use ic_notification_custom.png from Assets/Plugins/Android/res/drawable/:

1
2
3
4
5
6
7
<application ... >
    ...
    <meta-data
        android:name="im.getsocial.sdk.NotificationIcon"
        android:resource="@drawable/ic_notification_custom" />
    ...
</application>

As a result all notifications coming from GetSocial SDK will use custom icon:

Customized Notification Icon

Also, you can customize large notification icon using manifest. To use ic_large_notification_icon.png from Assets/Plugins/Android/res/drawable:

1
2
3
4
5
6
7
<application ... >
    ...
    <meta-data
        android:name="im.getsocial.sdk.LargeNotificationIcon"
        android:resource="@drawable/ic_large_notification_icon" />
    ...
</application>

Customized Large Notification Icon

Correct file path and name

You should put the correct name of drawable in your AndroidManifest.xml! If your image is named my_custom_image.png, than your android:resource value should be @drawable/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 AndroidManifest.xml:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
<application ... >
    ...
    <meta-data
        android:name="im.getsocial.sdk.NotificationsChannelTitle"
        android:resource="@string/getsocial_notifications_channel_title" />

    <meta-data
        android:name="im.getsocial.sdk.NotificationsChannelDescription"
        android:resource="@string/getsocial_notifications_channel_description" />
    ...
</application>

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?

😀 🙁