Platform
Docs
Solutions
ContactLog In

Start Routing Notifications Today!

Courier is a notification service that centralizes all of your templates and messaging channels in one place which increases visibility and reduces engineering time.

Sign-up

gaming-header
ENGINEERING

How I used Unity and Courier to Create a Notification-based Game

Matt Graber

August 19, 2021

Hello, world! I’m Matt Graber. I just finished my undergrad at the University of Maryland. I started my game development career back in freshman year in the UMD AR club. I used to teach other students how to create augmented and virtual reality experiences with Unity, a cross-platform game engine. I also enjoy informal game jams and larger projects in Unity with fellow developer friends.

Recently, I won the sponsored prize at the Bitcamp hackathon for building Package Person —an arcade game that notifies players of their status on the leaderboard. It was an exciting hackathon and also a learning experience for me. I learned how to use Courier and also how to use Unity with C# to create automatic POST requests to an API server—a teaser of what I will share later on.

I got featured in Courier’s live stream to build a notification-based game with Unity Engine, Courier, Twilio, and Mailjet. In this article, I will walk through the design of this game, called Rain Spikes, and provide a step-by-step explanation of how I used Courier to integrate notifications into the game.

Rain Spikes: The design of the game

People are naturally competitive. Even though Rain Spikes is simple, it’s addicting because players get notified about their score via email and text messaging, which prompts them to try it again and again and again.

Once the game starts, the player uses arrow keys to move a square from left to right to dodge the falling spikes. The more spikes they dodge, the more points they gain. If a spike hits your square, the game ends with a prompt to fill in your details—name, phone number, email, and memo (a message to include in the email). When you click submit, an automated email or an SMS message is sent showing your end score and the memo. Here’s how the game works:

gaming-1

Let’s go into a little bit of the design architecture for this game. First, if you don’t know, everything in Unity is object-oriented and is based on game objects. For example, the player game object (the object the player controls) is the square. The player game object has two components attached to it: the Sprite Renderer, which renders 2D and 3D objects, and Box Collider 2D, which detects collisions (in this game, with the falling spikes).

Finally, the last component I implemented as a script is the player control component, which enables the player to move the box with the left and right arrow keys independently of the frame rate.

As I said earlier, I wanted to add notifications to give Rain Spikes a competitive, addicting edge. So here’s how I did it.

How I added notifications using Courier to notify players of their score

Courier’s API allowed me to use the information collected in the end-game form to send notifications whenever the game ends. Of course, the emails and text messages are opt-in, of course—nobody wants to design an app that spams people with unwanted emails.

Step 1: I created a basic game with Unity Engine. While this article focuses on my Courier integration into the game, here is a recommended beginner-friendly tutorial to create a game with Unity Engine. Feel free to check out my code for this game on GitHub.

Step 2: I already had an account on Courier, so all I needed was to login and click on create a notification. If you don’t have a Courier account, it’s a simple sign-up.

gaming-2

Step 3: Courier has different channels to deploy notifications from the exact location. So all I had to do was choose the specific channels for this game: Mailjet for emails and Twilio for SMS messages.

gaming-3

Step 4: Next, I had to write out the email message for Mailjet:

1
Hello {playerName},
2
You recently scored {score} while playing that Courier-Unity integration demo game!
3
You sent the following memo to yourself:
4
{memo}
5
You should play again sometime!

playerName, score, and memo are assigned variables to fetch data from the game. So by assigning these variables, I sent values from the game to display as part of the message. For the memo and score variable, I made them conditionals—if the memo is not empty, the memo will be displayed. So these fields will only be displayed if the player actually submitted a memo or has a score. Adding conditionals on Courier is a lot easier than figuring out all the logic on the C# side.

gaming-4

Step 5: The next thing was to replicate the same message for Twilio. In Courier, it is easy to drag and drop the text because they are shared components. Thus, it saves the time of typing everything out again.

gaming-5

Step 6: After adding the messages, I went over to preview and wrote a test event, and checked if the notifications were working. The test event contains the assigned variables—playerName, score, email, phone number—and test values.

1
{
2
"data": {
3
"playerName":"Billy",
4
"score":4,
5
"memo":"hello billy here"
6
},
7
"profile": {
8
"email":"dummyemail@gmail.com",
9
"phone_number":"555555555"
10
},
11
"override": {}
12
}

gaming-6

Step 7: After previewing the notifications and testing everything was working, I published changes.

gaming-7

Step 8: Now, this is the tricky part. Currently, Courier has SDKs for Ruby, Python, Go, Java, etc. Unfortunately, there is none for C#. These SDKs generate sample code for sending notifications to the programming languages. It was not much of an issue. All I had to do was create a web request from Unity following the curl logic on Courier.

gaming-8

Step 9: Before starting the notification script code, I had to get references for each input field to fetch data to the script. In the Unity Engine inspector, the input field has a component called TextMeshPro (TMP). TMP is the “ultimate text solution” in Unity Engine.

Here’s what the notification script code looks like:

1
using System.Collections;
2
using System.Collections.Generic;
3
using UnityEngine;
4
using UnityEngine.Networking;
5
using UnityEngine.SceneManagement;
6
using TMPro;
7
8
public class Notifications : MonoBehaviour
9
{
10
11
public TMP_InputField nameInput;
12
public TMP_InputField emailInput;
13
public TMP_InputField phoneInput;
14
public TMP_InputField memoInput;
15
private GameManager gameManager;
16
17
const string EVENT_ID = "";
18
const string AUTH_KEY = "";
19
20
// Start is called before the first frame update
21
void Start()
22
{
23
gameManager = GetComponent<GameManager>();
24
}
25
26
public void Submit()
27
{
28
StartCoroutine(SendNotification());
29
}
30
31
private IEnumerator SendNotification()
32
{
33
WWWForm form = new WWWForm();
34
form.AddField("event", EVENT_ID);
35
form.AddField("recipient", nameInput.text);
36
form.AddField("override", "{}");
37
form.AddField("data", "{\"playerName\":\"" + nameInput.text + "\"," +
38
"\"score\":" + gameManager.GetScore() + "," +
39
"\"memo\":\"" + memoInput.text + "\"}");
40
form.AddField("profile", "{\"email\":\"" + emailInput.text + "\"," +
41
"\"phone_number\":\"" + phoneInput.text + "\"}");
42
using (UnityWebRequest www = UnityWebRequest.Post("https://api.courier.com/send", form))
43
{
44
www.SetRequestHeader("Authorization", "Bearer " + AUTH_KEY);
45
yield return www.SendWebRequest();
46
47
if (www.result != UnityWebRequest.Result.Success)
48
{
49
Debug.Log(www.error);
50
}
51
else
52
{
53
Debug.Log("Form upload complete!");
54
// reload the scene to play again
55
SceneManager.LoadScene(0);
56
}
57
}
58
}
59
}

Don’t get overwhelmed: Here’s a breakdown of the different methods and classes I used in this script.

  • public class notifications: public class notifications handle the input fields for name, email, phone number, and memo.The GameManger input field is private because that is where the score will be stored. In order to create this method, I imported the TextMeshPro with a usingstatement. Importing TMP gives the flexibility to use input fields in Unity Engine.
  • void Start: void Start method handles the private GameManager reference to the game object in the Unity engine inspector.
  • public void Submit: The Submit method handles what happens when a player hits the submit button. It basically starts a coroutine (StartCoroutine) in order to send a message. The coroutine in Unity is responsible for executing the game logic over a number of frames. A coroutine is important because it sends web requests in Unity.
  • private IEnumerator SendNotifications: The private IEnumerator method is the coroutine that handles all frame iterations. In this method, I used the class WWWform to construct a form that would hold all the data that will be sent to Courier.
  • UnityWebRequest: UnityWebRequest class is the class that sends the POST request(form) to Courier.
  • SetRequestHeader: For the request to work, I had to set a request header to handle the authorization and authentication.
  • yield return: yield return handles an async operation (POST request to Courier API) and pauses the execution until the request is sent.
  • Debug.Log: Debug.Log is used to print to the console in Unity.
  • SceneManager: The SceneManager is responsible for reloading the scene or restarting the game once the submit request is sent. Check out the Courier Live Stream for some additional explanation of the notifications script code.

Step 10: After coding the Notification script, I attached it to the GameManager gameObject, populated its input fields, and assigned the Submit button to be executed onClick event.

Step 11: Next, I went over to the Courier notification to map out the event ID selected in the code and get the authentication token.

gaming-9

Step 12: Now’s the fun part! I gave the game a test play.

With Courier, I integrated notifications in under an hour

This process would have been a bona fide nightmare without Courier’s API. I would have had to program the logic myself, which would have taken hours, if not days.

With Courier’s pre-built logic and conditionals for the message content, I was able to build email and text SMS notifications into the game in less than an hour. Heck, you can watch me do the entire process in the live stream.

While Rain Spikes was mainly an exercise in building notifications into a game, I see a ton of potential for Courier to be used for more complex and sophisticated projects in the future. For example, notifying players when someone has achieved a special reward or my idea for modernizing Courier messaging in Package Person is to send gamers updates about the game via Discord.

Illustration by Rebekka Dunlap

Start Routing Notifications Today!

Courier is a notification service that centralizes all of your templates and messaging channels in one place which increases visibility and reduces engineering time.

Sign-up

More from Engineering

courier-ios-thumbnail
PRODUCT NEWSENGINEERING

Simplifying notifications with the Courier iOS SDK

Push notifications are a valuable tool for keeping users informed and increasing their engagement with your app. You can use push notifications to alert users about promotions, new content, or any other important updates. While push notifications are a powerful tool, setting up push notifications in iOS can be a daunting task that requires a significant amount of effort and time. Fortunately, the Courier iOS Mobile Notifications Software Development Kit (SDK) simplifies this process.

Mike Miller

Mike Miller

March 23, 2023

Courier Android SDK thumbnail
PRODUCT NEWSENGINEERING

Building Android push notifications with Firebase and Courier’s SDK

Push notifications have become an essential part of modern mobile apps, allowing you to keep your users engaged and informed. However, implementing push for different platforms can be a complex and time-consuming task, requiring developers to set up and handle token management, testing, and other logistical details.

Mike Miller

Mike Miller

March 21, 2023

Build your first notification in minutes

Send up to 10,000 notifications every month, for free.

Get started for free

Email & push notification

Build your first notification in minutes

Send up to 10,000 notifications every month, for free.

Get started for free

Email & push notification

Platform

Users

Content

Channels

Sending

Workflows

Preferences

Inbox

Workspaces

Observability

API Status

Changelog

© 2024 Courier. All rights reserved.