Push Notifications

Note: Before using Google Cloud Messaging you should create an application in Google Play Console. So you will avoid errors when linking the play service with the functionality from the Google API console.

More information about creation new application with Play Service in Google Play console you can find here.

Setting project in Google Cloud Console

1. Open the Google Cloud Console.

2. Choose you project name in drop down list.

Once the project has been chosed, a page appears that displays your project ID and a project number. Project number is a GCM sender ID.

3. Copy GCM Sender Id and paste it to plugin settings.

4. Back to the Google Cloud Console and choose API & Services -> Library.

5. Type in search bar Google Cloud Messaging. Click on it and enable.

6. After activation you should create credentials for your project. Click on button in the top right conner of the screen - Go To Credentials.

7. In drop down list choose Google Cloud Messaging, click button "What credentials do I need?". Console will generate Server API Key for you. 

Note: Important! You shouldn't use this key!

8. Click on your Server Key, choose Android Apps, specify Package name and generate SHA1. In order to generate SHA1 you should open terminal(cmd.exe) and specify paths to your keystore. Path shouldn't contains white spaces or special symbols.

keytool -list -v -keystore path_to_your_keystore 

Simply drag & drop it to terminal console. After getting sha1 paste it in console and save.

Note: It may take up to 5 minutes for settings to take effect

Setting project in Firebase Console

1. Open Firebase console. Click on "+" and choose your project in drop down list.

2. Click on project settings.

You can implement your own server-side for sending and managing push notification. The following guide will describe all necessary steps, and you also will find push server PHP example script inside the plugin. But Android Native offers a few alternatives as well. You can use one of the supported 3-rd party push notification services:

Example of server code can be found at:


You will find testgcm.php example file inside which will demonstrate how to send push notification using PHP.

3. Go to Cloud Messaging section and copy Server key.

Replace YOUR_API_KEY with Server key into testgcm.php:

// API access key from Google API's Console
define( 'API_ACCESS_KEY', 'YOU_API_KEY' );

4. In order to send Push Message to your device you should get the device registration Id.

Open Adb as separate application or as alternative use Android Studio or Eclipse. Then open LogCat and create tag Android. Build our Example scene on your device, open Notifications tab and click Get Device Registration id.

Note: Don't copy Registration Id from line which contains info show_message!

Enter received id into testgcm.php:

$registrationIds = array("YOUR_DEVICE_REGISTRATION_ID_HERE");

5. Use console to run testgcm.php script for sending push notification to  your device:

 php <path_to_gcm.php>

Or any other way to run PHP script.

6. There is a possibility, that your PC doesn't have php integration. Go to http://www.wampserver.com/en/ and download it.

After successful installation wampserver you should install php. To check if php is ready for usage, please, open terminal and type:

php -v

You should get information about installed php version. Now you can send push for your device.

Push Notification Data Format

For now, Android Native plugin supports next notification data format.

'title'         => '_title_string_',
'subtitle'   => '_subtitle_string_',
'alert'        => '_message_string_',
'json'        => '_json_formatted_data_string_'

If you are using testgcm.php script for push notifications support in your project, you have to setup next data bundle:

// prep the bundle
$msg = array
    'title'         => 'This is a title. title',
    'subtitle'   => 'This is a subtitle. subtitle',
    'alert'        => 'here is a message. message',
    'json'        => '{"big_picture_url": "http://www.hd-tecnologia.com/imagenes/articulos/2015/03/Unity-5.jpg",
                        "string_extra": "100000583627394",
                        "string_value": "value",
                        "string_key": "key",
                        "is_public": true,
                        "item_type_id": 4,
                        "numeric_extra": 0}'

$fields = array
    'registration_ids'  => $registrationIds,
    'data'              => $msg

You can provide any JSON data you need for your particular case.

One important thing: big_picture_url field is reserved for Big-Picture Notification Style. If you don't use Big-Picture Notification Style, just leave this field string.Empty, but DON'T remove from JSON data. All other fields added as an example. What data fields and fields amounts are up to you as a developer.

Usage API


After this you can send the registration Id  to your backend.

To find out if the push notification has been received, you can use:


it will trigger ActionCouldMessageLoaded  event. And you can get the message using getter:


See the code snippet below:

GoogleCloudMessageService.ActionCouldMessageLoaded += OnMessageLoaded;

private void OnMessageLoaded(string lastMessage) {
    GoogleCloudMessageService.ActionCouldMessageLoaded -= OnMessageLoaded;
	AndroidMessage.Create("Message Loaded", "Last GCM Message: " + GoogleCloudMessageService.instance.lastMessage);

If you want to remove last message info, you can use RemoveLastMessageInfo method as showed below:


Setting Push Notifications

First, you have to Setup Push Notifications Settings in Inspector.

Next Settings are available:

  • Show in a foreground. 
  • Replace old notification with the new one.
  • Disable/Enable Vibration 
  • Setup Notification Icon.
  • Setup Notification Sound.
  • Sender Id for Google Push Service.
  • Choose Push Services. 


If you don't set up Push Notification settings in the inspector, then default settings will be applied.

To submit PushNotifications Settings, just call GoogleCloudMessageService.Instance.Init(); method at the start of your Application.



Manifest Requirements

  • The android.permission.GET_ACCOUNTS permission as GCM requires a Google account (necessary only if  the device is running a version lower than Android 4.0.4)
  • The android.permission.WAKE_LOCK permission so the application can keep the processor from sleeping when a message is received. Optionalâuse only if the app wants to keep the device from sleeping.
  • An applicationPackage + ".permission.C2D_MESSAGE" permission to prevent other Android applications from registering and receiving the Android application's messages. The permission name must exactly match this patternâotherwise the Android application will not receive the messages.

Here are excerpts from a sample manifest that supports GCM:

<manifest package="com.example.gcm" ...>

    <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17"/>
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

    <permission android:name="com.example.gcm.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />
    <uses-permission android:name="com.example.gcm.permission.C2D_MESSAGE" />

    <application ...>
            android:permission="com.google.android.c2dm.permission.SEND" >
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <category android:name="com.example.gcm" />
        <service android:name="com.androidnative.gcm.GcmIntentService" />



Warning: Plugin will add all necessary receivers and permissions automatically, but since permissions names depend on your app bundle id name, if you change application bundle id with the Unity Project settings, it will triggers adding new permission to your manifest, so you need to clean up manually permissions which use your old bundle id.

Check if  Notifications are ON?

Assuming you are referring to Google Cloud Messaging (since you are using the android and push-notification tag), there are no general settings used to enable/disable the GCM service (unlike the Apple Push Notifications service for iOS devices, not mix with the general notifications app settings).

When you install an app, if the app uses Google Cloud Messaging, it will be listed in the list of permissions that this app requires in order to work. By choosing to install the app you allow it to send push notifications to you.

That is said, in order for the app actually to receive GCM messages, your app must programmatically register to the GCM service. You can do that in any place you wish within your app, and you can create an app settings screen in which the user can enable/disable GCM (which would trigger registration/unregistration to/from GCM). Your app can contain a persistent store that would hold the registration ID you receive upon registration to GCM, and you can use that store to determine if your app is registered to GCM.

You can ask your user to turn it on if the app is already running (if you have some code that does that). If it's not running and the app is not registered for GCM on that device, your server can't send GCM messages to it.

If you want to overcome that obstacle, you can register to GCM automatically (when the app is first launched). Then you can send messages to the app whenever you wish. If you still want to let the user decide whether they want to see those notifications, you can have a flag in your app settings that enables/disables the UI notification that you create as a result of an incoming GCM message. The GCM service will always be enabled, but unless the user chooses to view the notifications, you app won't display anything when a GCM message arrives.

Example Scenes

  • Assets/Extensions/AndroidNative/xExample/Scenes/Other/Notifications