Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
## [1.0.0-pre.4] - 2021-08-26

### Fixed

* notificationOpened analytics events will work correctly on Android
* The host Android app will not forceably reboot if opened from background from a notification
* Updated analytics integration so that the userCountry is correctly reported for push events
* Documentation improvements
  • Loading branch information
Unity Technologies committed Aug 26, 2021
1 parent 435e332 commit ce1ec7a
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 59 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ All notable changes to this package will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [1.0.0-pre.4] - 2021-08-26

### Fixed

* notificationOpened analytics events will work correctly on Android
* The host Android app will not forceably reboot if opened from background from a notification
* Updated analytics integration so that the userCountry is correctly reported for push events
* Documentation improvements

## [1.0.0-pre.3] - 2021-08-19

### Fixed
Expand Down
67 changes: 35 additions & 32 deletions Documentation~/index.md
Original file line number Diff line number Diff line change
@@ -1,50 +1,48 @@
# Push Notifications SDK Documentation

The Push Notifications SDK allows you to send rich push notifications to mobile devices.
The Push Notifications SDK allows you to send push notification campaigns, including rich push notifications with images, to your users.

## Platform Support

This SDK supports both iOS and Android. iOS 10+ and Android SDK > 26 (Oreo) are supported.
This SDK supports both iOS and Android. iOS 10+ and Android SDK >= 26 (Oreo) are supported.

## Quick Start

The SDK comes with a sample script that will register for push notifications. Simply add this to your project to get going.
The SDK comes with a sample script that will register for push notifications. Simply add this to your project, and replace the relevant settings, to get going.

## Registering for Push Notifications

To register for push notifications, two steps are required. Firstly, you need to initialise Unity Services, so that the required analytics events can be sent to Unity Analytics 2.0.

Once that is complete, you can then register for notifications. To ensure no notifications are missed, this should be done in the startup code for your game. Note that on first registration on iOS, a user will be shown a permission request, so ensure this call is made at a convenient place in your game. The SDK will handle the showing of notification content, including images, titles and message body.
Once that is complete, you can then register for notifications. Ideally, to ensure no notifications are missed, this should be done in the startup code for your game. However, note that on first registration on iOS, a user will be shown a permission request, so also ensure this call is made at a convenient place in your game. The SDK will handle the showing of notification content, including images, titles and message body.

A full code sample is shown below.

```cs
// In a monobehaviour in a convenient place in your game.
async void Start()
await UnityServices.InitializeAsync();

// Replace the below values with the relevant settings for your project, as discussed in the documentation below.
PushNotificationSettings settings = new PushNotificationSettings()
{
await UnityServices.InitializeAsync();
PushNotificationSettings settings = new PushNotificationSettings()
{
AndroidApiKey = "API_KEY",
AndroidSenderId = "SENDER_ID",
AndroidApplicationId = "APPLICATION_ID",
AndroidProjectId = "PROJECT_ID"
};
AndroidApiKey = "API_KEY",
AndroidSenderId = "SENDER_ID",
AndroidApplicationId = "APPLICATION_ID",
AndroidProjectId = "PROJECT_ID"
};

try
{
try
{

string pushToken = await PushNotifications.RegisterForPushNotificationsAsync(settings);

PushNotifications.OnNotificationReceived += notificationData =>
{
Debug.Log("Received a notification!");
};
}
catch (Exception e)
string pushToken = await PushNotifications.RegisterForPushNotificationsAsync(settings);

PushNotifications.OnNotificationReceived += notificationData =>
{
Debug.Log("Failed to retrieve a push notification token.");
}
Debug.Log("Received a notification!");
};
}
catch (Exception e)
{
Debug.Log("Failed to retrieve a push notification token.");
}
```

Expand All @@ -61,18 +59,23 @@ The SDK requires a number of settings in order to function correctly. Some setti
* AndroidApplicationId: The application ID for a Firebase application to be used for Android's Firebase Cloud Messaging API. This can be found in your Firebase dashboard.
* AndroidProjectId: The project ID for a Firebase project to be used for Android's Firebase Cloud Messaging API. This can be found in your Firebase dashboard.

It is safe to have one settings object for all platforms, as only the settings relevant to the current platform will be used by the SDK.

### Analytics

The SDK will records two analytics events:

* `notificationServices`: This event is recorded whenever a new token is registered on the client. It contains the push token and is used to register this token with the backend service.
* `notificationServices`: This event is recorded whenever a new token is registered on the client. It contains the push token and is used to register this token with the backend service. This allows you to send notifications from the Unity Dashboard, and is required for proper functionality of this product.
* `notificationOpened`: This event is recorded whenever a notification is opened by a user. It contains data regarding which campaign and cohort the user was in, and whether the app was launched from the notification.

## Analytics Only Mode
#### Analytics Only Mode

*WARNING: This section is only applicable if you are trying to use the Unity Dashboard Push Notification service alongside a separate Push Notification implementation. For most users this section is not required or recommended as it will lead to reduced functionality of the product*

It is possible to integrate the SDK with an existing push notification service if required. To do so, do not call the registration methods as indicated above, and instead use the two methods in the `PushNotifications.Analytics` class alongside your existing implementation.

> **Note:** This section is only required if you have an existing push notification solution you wish to use alongside the Unity Push Notifications package.
> This will result in reduced functionality of this package - e.g. images in notifications will not be displayed.
`RecordPushTokenUpdated` should be called when you receive a new push token for a device. Note that the OS may create a new token at multiple points in the app's lifecycle, so be sure to call this whenever the token changes, and not just at startup.

It is possible to integrate the SDK with an existing push notification service if required. To do so, use the two methods in the `PushNotifications.Analytics` class.
`RecordNotificationOpened` should be called when a notification is opened. It takes a dictionary that is the data the notification payload contained, and a boolean flag to indicate whether the app was launched from the notification or not.

`RecordPushTokenUpdated` should be called when you receive
Note that this should allow you to send and schedule notifications from the Unity Dashboard. However, it greatly depends on the other push notification implementation you have implemented, and may lead to missing images or other content in notifications, so it is strongly recommended to use the standard set up, with this SDK being the only Push Notification solution integrated, if possible.
Binary file modified Plugins/Android/PushNotificationsAndroidLib-release.aar
Binary file not shown.
2 changes: 2 additions & 0 deletions Runtime/Analytics/EventsWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public void RecordCustomEvent(string eventName, Dictionary<string, object> param
{
evt.Parameters.Set(parameter.Key, parameter.Value);
}

evt.Parameters.AddUserCountry();

Events.Buffer.PushEvent(evt);
}
Expand Down
8 changes: 3 additions & 5 deletions Runtime/Analytics/PushNotificationAnalytics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Unity.Services.PushNotifications
static class SdkVersion
{
// This value should always match what is in the package.json for this package
public static readonly string SDK_VERSION = "1.0.0-pre.2";
public static readonly string SDK_VERSION = "1.0.0-pre.4";
}

interface IPushNotificationsAnalytics
Expand Down Expand Up @@ -41,8 +41,7 @@ public void RecordPushTokenUpdated(string pushToken)
{ "clientVersion", m_AnalyticsPlatformWrapper.ApplicationVersion() },
{ "sdkVersion", SdkVersion.SDK_VERSION },
{ "sdkMethod", "com.unity.services.pushNotifications.PushNotificationsAnalytics.RecordPushTokenUpdated" },
{ "platform", m_AnalyticsPlatformWrapper.AnalyticsPlatform() },
{ "userCountry", m_AnalyticsPlatformWrapper.UserCountry() }
{ "platform", m_AnalyticsPlatformWrapper.AnalyticsPlatform() }
};

RuntimePlatform runtimePlatform = m_AnalyticsPlatformWrapper.RuntimePlatform();
Expand Down Expand Up @@ -72,8 +71,7 @@ public void RecordNotificationOpened(Dictionary<string, object> payload, bool di
{ "clientVersion", m_AnalyticsPlatformWrapper.ApplicationVersion() },
{ "sdkVersion", SdkVersion.SDK_VERSION },
{ "sdkMethod", "com.unity.services.pushNotifications.PushNotificationsAnalytics.RecordNotificationOpened" },
{ "platform", m_AnalyticsPlatformWrapper.AnalyticsPlatform() },
{ "userCountry", m_AnalyticsPlatformWrapper.UserCountry() }
{ "platform", m_AnalyticsPlatformWrapper.AnalyticsPlatform() }
};

bool insertCommunicationAttrs = false;
Expand Down
10 changes: 4 additions & 6 deletions Runtime/Analytics/PushNotificationsAnalyticsPlatformWrapper.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System;
using Unity.Services.Analytics.Internal;
using UnityEngine;

namespace Unity.Services.PushNotifications
Expand All @@ -8,11 +10,12 @@ interface IPushNotificationAnalyticsPlatformWrapper
RuntimePlatform RuntimePlatform();
bool IsApplicationFocused();
string AnalyticsPlatform();
string UserCountry();
}

class PushNotificationsAnalyticsPlatformWrapper: IPushNotificationAnalyticsPlatformWrapper
{
const string k_UnknownCountryCode = "ZZ";

public string ApplicationVersion()
{
return Application.version;
Expand All @@ -38,10 +41,5 @@ public string AnalyticsPlatform()
return "UNKNOWN";
#endif
}

public string UserCountry()
{
return "GB";
}
}
}
6 changes: 0 additions & 6 deletions Tests/Runtime/MockPlatformWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ class MockPlatformWrapper: IPushNotificationAnalyticsPlatformWrapper
public const string mockApplicationVersion = "1.0.0";
public RuntimePlatform mockRuntimePlatform = UnityEngine.RuntimePlatform.Android;
public const string mockAnalyticsPlatform = "IPHONE";
public const string mockUserCountry = "UK";
public bool mockIsApplicationFocused = true;

public string ApplicationVersion()
Expand All @@ -25,11 +24,6 @@ public string AnalyticsPlatform()
return mockAnalyticsPlatform;
}

public string UserCountry()
{
return mockUserCountry;
}

public bool IsApplicationFocused()
{
return mockIsApplicationFocused;
Expand Down
6 changes: 0 additions & 6 deletions Tests/Runtime/PushNotificationAnalyticsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ public void WhenPushTokenIsUpdatedAndThePlatformIsIOSRecordTheRightData()
Dictionary<string, object> calledParams = m_MockEventWrapper.LastCalledParams;
Assert.AreEqual(MockPlatformWrapper.mockApplicationVersion, calledParams["clientVersion"]);
Assert.AreEqual(MockPlatformWrapper.mockAnalyticsPlatform, calledParams["platform"]);
Assert.AreEqual(MockPlatformWrapper.mockUserCountry, calledParams["userCountry"]);
Assert.AreEqual(SdkVersion.SDK_VERSION, calledParams["sdkVersion"]);
Assert.AreEqual("com.unity.services.pushNotifications.PushNotificationsAnalytics.RecordPushTokenUpdated", calledParams["sdkMethod"]);
Assert.AreEqual(token, calledParams["pushNotificationToken"]);
Expand All @@ -64,7 +63,6 @@ public void WhenPushTokenIsUpdatedAndThePlatformIsAppleTVRecordTheRightData()
Dictionary<string, object> calledParams = m_MockEventWrapper.LastCalledParams;
Assert.AreEqual(MockPlatformWrapper.mockApplicationVersion, calledParams["clientVersion"]);
Assert.AreEqual(MockPlatformWrapper.mockAnalyticsPlatform, calledParams["platform"]);
Assert.AreEqual(MockPlatformWrapper.mockUserCountry, calledParams["userCountry"]);
Assert.AreEqual(SdkVersion.SDK_VERSION, calledParams["sdkVersion"]);
Assert.AreEqual("com.unity.services.pushNotifications.PushNotificationsAnalytics.RecordPushTokenUpdated", calledParams["sdkMethod"]);
Assert.AreEqual(token, calledParams["pushNotificationToken"]);
Expand All @@ -84,7 +82,6 @@ public void WhenPushTokenIsUpdatedAndThePlatformIsAndroidRecordTheRightData()
Dictionary<string, object> calledParams = m_MockEventWrapper.LastCalledParams;
Assert.AreEqual(MockPlatformWrapper.mockApplicationVersion, calledParams["clientVersion"]);
Assert.AreEqual(MockPlatformWrapper.mockAnalyticsPlatform, calledParams["platform"]);
Assert.AreEqual(MockPlatformWrapper.mockUserCountry, calledParams["userCountry"]);
Assert.AreEqual(SdkVersion.SDK_VERSION, calledParams["sdkVersion"]);
Assert.AreEqual("com.unity.services.pushNotifications.PushNotificationsAnalytics.RecordPushTokenUpdated", calledParams["sdkMethod"]);
Assert.AreEqual(token, calledParams["androidRegistrationID"]);
Expand All @@ -101,7 +98,6 @@ public void WhenNotificationOpenedIsCalledWithFullDataAndDidLaunchTheRightFields
Dictionary<string, object> calledParams = m_MockEventWrapper.LastCalledParams;
Assert.AreEqual(MockPlatformWrapper.mockApplicationVersion, calledParams["clientVersion"]);
Assert.AreEqual(MockPlatformWrapper.mockAnalyticsPlatform, calledParams["platform"]);
Assert.AreEqual(MockPlatformWrapper.mockUserCountry, calledParams["userCountry"]);
Assert.AreEqual(SdkVersion.SDK_VERSION, calledParams["sdkVersion"]);
Assert.AreEqual("com.unity.services.pushNotifications.PushNotificationsAnalytics.RecordNotificationOpened", calledParams["sdkMethod"]);
Assert.AreEqual(true, calledParams["notificationLaunch"]);
Expand All @@ -125,7 +121,6 @@ public void WhenNotificationOpenedIsCalledWithMinimalDataAndDidLaunchTheRightFie
Dictionary<string, object> calledParams = m_MockEventWrapper.LastCalledParams;
Assert.AreEqual(MockPlatformWrapper.mockApplicationVersion, calledParams["clientVersion"]);
Assert.AreEqual(MockPlatformWrapper.mockAnalyticsPlatform, calledParams["platform"]);
Assert.AreEqual(MockPlatformWrapper.mockUserCountry, calledParams["userCountry"]);
Assert.AreEqual(SdkVersion.SDK_VERSION, calledParams["sdkVersion"]);
Assert.AreEqual("com.unity.services.pushNotifications.PushNotificationsAnalytics.RecordNotificationOpened", calledParams["sdkMethod"]);
Assert.AreEqual(true, calledParams["notificationLaunch"]);
Expand All @@ -143,7 +138,6 @@ public void WhenNotificationOpenedIsCalledWithMinimalDataAndDidNotLaunchTheRight
Dictionary<string, object> calledParams = m_MockEventWrapper.LastCalledParams;
Assert.AreEqual(MockPlatformWrapper.mockApplicationVersion, calledParams["clientVersion"]);
Assert.AreEqual(MockPlatformWrapper.mockAnalyticsPlatform, calledParams["platform"]);
Assert.AreEqual(MockPlatformWrapper.mockUserCountry, calledParams["userCountry"]);
Assert.AreEqual(SdkVersion.SDK_VERSION, calledParams["sdkVersion"]);
Assert.AreEqual("com.unity.services.pushNotifications.PushNotificationsAnalytics.RecordNotificationOpened", calledParams["sdkMethod"]);
Assert.AreEqual(false, calledParams.ContainsValue("notificationLaunch"));
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "com.unity.services.push-notifications",
"displayName": "Push Notifications",
"version": "1.0.0-pre.3",
"version": "1.0.0-pre.4",
"unity": "2020.3",
"description": "This package adds support for Push Notifications to your game. It allows sending rich push notifications with images, and provides analytics on the number of received push notifications.",
"dependencies": {
"com.unity.services.analytics": "2.0.7-pre.2",
"com.unity.services.analytics": "2.0.7-pre.6",
"com.unity.nuget.newtonsoft-json": "2.0.0",
"com.unity.services.core": "1.1.0-pre.8"
},
Expand All @@ -17,11 +17,11 @@
}
],
"upmCi": {
"footprint": "9d6c795c8bd032072185cb9105b9e2c5af550356"
"footprint": "83669ea32462b5b183f92df38eb96df64c4e1f2f"
},
"repository": {
"url": "https://github.cds.internal.unity3d.com/unity/Catapult.mono.git",
"type": "git",
"revision": "24e0850287ee78608686d6fe92bf0526a965afd4"
"revision": "51276eb7d2448aa9beb59c7ecbb641b0d16a6159"
}
}

0 comments on commit ce1ec7a

Please sign in to comment.