Unity. Push Notifications

Attention! Push-notifications are available only for platforms supporing iOS, Android, Windows Store/Windows Phone 8.1/10.

To enable PUSH-notifications you will have to perform the following actions:

  • Add the application to your space in devtodev system
  • Android. Get API key from Google APIs Console. It is nessesary to activate Google Cloud Messaging for Android before key generation. Detailed information on how to receive an API key you can find in native Android devtodev SDK documentation.
  • iOS. Generate Developer or Production Certificate for the application and get Private key file (.p12) on its basis. Detailed information on how to receive a Private key file you can find in native iOS devtodev SDK documentation.
  • Submit the data to the application settings in devtodev system.
  • Integrate devtodev SDK to the application (see the "SDK integration" division to learn more about integrating and initializing devtodev SDK).
  • Add several lines of the code to switch in the push-notification to the SDK.
  • Create a campaign for sending push-notifications in "Push" section.

SDK Integration

Android platform features:

Go to Firebase console and then to your project or creat a new one. Here is complete guide on adding your project to Firebase console and enabling Cloud messaging.

Open the Cloud messaging tab and copy the Sender ID of your project.

Follow these steps to integrate Push-notifications into your app using devtodev SDK:

Windows Store/Windows Phone 8.1/10 platform features:

Build a Windows Store App in Unity. After the app is built,Visual Studio project will be created. Proceed with the following changes.

Attention! There is a difference in the implementation of the elements mentioned below for Windows 8.1+ and Windows 10+ projects.  

Windows 8.1+: Put the following source in your Application class (usually it is App.xaml.cs file) at the end of the OnLaunched(LaunchActivatedEventArgs e) function. For Example:

protected override void OnLaunched(LaunchActivatedEventArgs e) {
   //...other source
   DevToDev.PushHandler.HandleApplicationNavigation(e.Arguments);
}

Windows 10+: Put the following source in your Application class (usually it is App.xaml.cs file) at the end of the OnLaunched(LaunchActivatedEventArgs e) and OnActivated(IActivatedEventArgs args)​ functions. For Example:

protected override void OnLaunched(LaunchActivatedEventArgs e) {
   //...other source
   DevToDev.PushHandler.HandleApplicationNavigation(e.Arguments);
}

protected override void OnActivated(IActivatedEventArgs args) {
   //...other source
   if (args.Kind == ActivationKind.ToastNotification) {
       var toastArgs = args as ToastNotificationActivatedEventArgs;
       DevToDev.PushHandler.HandleApplicationNavigation(toastArgs.Argument);
   }
}

Make sure that these functions are enabled in Package.appmanifest of you project (the flag "Toast capable" is enabled by default for Windows 10+ projects, it is absent in the manifest).

 Add the following two Background Tasks in Package.appmanifest:

You must also associate your application with the Windows Store app (otherwise push-notifications will not be delivered). Open "Store->Associate App with the Store" menu, login with your Live ID and pick the appropriate application form the list. A file Package.StoreAssociation.xml will be added into the Project.

The specificity of integration on iOS platform

Build an iOS App in Unity. After the app is built, Xcode project will be created. Proceed with the following changes.

Enable push notifications in your Xcode project.

The library provides support for iOS 10 notification attachments, such as images, animated gifs, and video. In order to take advantage of this functionality, you will need to create a notification service extension alongside your main application.

Create a new iOS target in Xcode (File -> New -> Target) and select the Notification Service Extension type.

​In Member Center, the Push Notifications service will appear as Configurable (not Enabled) until you create a client SSL certificate.

Add devtodevAppExtensions.framework to newly created extension. Make sure that Deployment Target is pointed as iOS 10.0 or higher:

Make sure that field Architectures contains "Standard architectures armv7, arm64" setting both in the project and the extension build settings:

Modify your extension.

  1. Delete all dummy source code for your new extension.
  2. Inherit from DTDMediaAttachmentExtension in NotificationService.
//NotificationService.h
#import <devtodevAppExtensions/devtodevAppExtensions.h>

@interface NotificationService : DTDMediaAttachmentExtension

@end

//NotificationService.m
#import "NotificationService.h"

@interface NotificationService ()

@end

Adding Capabilities

Use Xcode to enable push notifications in the target’s Capabilities pane:

Enable Background Modes and Remote notifications under the target’s Capabilities section:

Two ways to integrate push-notifications:

  • Using the graphic interface:
    1. Open the Window/devtodev menu element;
    2. Switch Push-notifications tumbler on;
    3. If you need to use push token for some aims or to handle the getting of notifications by user, add the GameObject with the following function to the scene::
      public void PushReceived(IDictionary<string, string> pushAdditionalData) {
           //pushAdditionalData - push-notification data that you send to your app
      }
      
      public void PushOpened(DevToDev.PushMessage pushMessage, DevToDev.ActionButton actionButton) {
           //pushMessage - DevToDev.PushMessage. Represents toast notification message
           //actionButton - DevToDev.ActionButton. Represents toast button that was clicked. Could be null if toast body was clicked
      }
      
      public void PushTokenFailed(string error) {
           //handle push-notifications error here
      }
      
      public void PushTokenReceived(string pushToken) {
           //pushToken - your push token
      }
      
    4. Set the target game object, needed script and functions in the interface:
  • Using code:
    Before calling Analytics.Initialize add the following strings:
public void PushReceived(IDictionary<string, string> pushAdditionalData) {
     //pushAdditionalData - push-notification data that you send to your app
}

public void PushOpened(DevToDev.PushMessage pushMessage, DevToDev.ActionButton actionButton) {
     //pushMessage - DevToDev.PushMessage. Represents toast notification message
     //actionButton - DevToDev.ActionButton. Represents toast button that was clicked.
     //               Could be null if toast body was clicked
}

public void PushTokenFailed(string error) {
     //handle push-notifications error here
}

public void PushTokenReceived(string pushToken) {
     //pushToken - your push token
}

DevToDev.PushManager.PushReceived = PushReceived;
DevToDev.PushManager.PushOpened = PushOpened;
DevToDev.PushManager.PushTokenFailed = PushTokenFailed;
DevToDev.PushManager.PushTokenReceived = PushTokenReceived;

DevToDev.PushManager.PushNotificationsOptions = (DTDNotificationOptions.Alert | DTDNotificationOptions.Badge | DTDNotificationOptions.Sound | DTDNotificationOptions.Provisional); //Notification options for iOS, optional property
DevToDev.PushManager.FirebaseSenderId = FirebaseSenderId; //SenderId From firebase console for Android
DevToDev.PushManager.PushNotificationsEnabled = true; 

// FOR ANDROID ONLY! Optional. Using custom push-notification icons on Android.
// <summary> To set the custom icons to be shown in push-notification on the SDK part,
// use the following methods.
// Attention! Icons which set in the push-notification wizard 
// have priority over the icons which set in these methods.</summary>
// <param name="iconName">Icon file from resources of your app
// (from Assets/Plugins/Android/res folder)</param>

//FOR ANDROID ONLY! To set the small icon:
DevToDev.PushManager.CustomSmallIcon = iconName;

//FOR ANDROID ONLY! To set the large icon: 
DevToDev.PushManager.CustomLargeIcon = iconName;

 

Creating a new push-notification in devtodev interface

  1. Open PUSH tag and click on ADD NEW PUSH button.
    19.png

  2. Fill in campaign name, select an app for delivery.
    Attention! You can create a campaign only after at least one push token comes from devtodev SDK integrated to your application. Otherwise the app will not be displayed in the list.
    10_2.png

  3. Choose user group to send a message. You can choose existing segment or create a new one.
    10_3.png

  4. Enter notification details

  5. Schedule the delivery

  6. Thats it!
    ​

Checking the events
Analytics integration