The Firebase Messaging Unity Sample demonstrates receiving messages from from Firebase Cloud Messaging using the Firebase Unity SDK.
- Unity 5.3 or higher.
- Xcode 7.3 or higher (when developing for iOS).
- Android SDK (when developing for Android).
- Register your iOS app with Firebase.
- Create a project in the
Firebase console,
and associate your iOS application.
- You should use
com.google.firebase.unity.messaging.testappas the package name while you're testing.- If you do not use the prescribed package name you will need to update
the bundle identifier as described in the
Optional: Update the Project Bundle Identifierbelow.
- If you do not use the prescribed package name you will need to update
the bundle identifier as described in the
- You should use
- Associate the project with an
APNs certificate
- Inside your project in the Firebase console, select the gear icon,
select
Project Settings, and then select theCloud Messagingtab. - Select the
Upload Certificatebutton for your development certificate, your production certificate, or both. At least one is required. - For each certificate, select the .p12 file, and provide the password, if any. Make sure the bundle ID for this certificate matches the bundle ID of your app. Select Save.
- Inside your project in the Firebase console, select the gear icon,
select
- Download the
GoogleService-Info.plistfile associated with your Firebase project from the console. This file identifies your iOS app to the Firebase backend, and will need to be included in the sample later. - For further details please refer to the general instructions which describes how to configure a Firebase application for iOS.
- Create a project in the
Firebase console,
and associate your iOS application.
- Download the Firebase Unity SDK and unzip it somewhere convenient.
- Open the sample project in the Unity editor.
- Select the
File > Open Projectmenu item. - Click
Open. - Navigate to the sample directory
testappin the file dialog and clickOpen.
- Select the
- Open the scene
MainScene.- Navigate to
Assets/TestApp/MainScenein theProjectwindow. - Double click on
MainScenefile to open.
- Navigate to
- Import the
Firebase Messagingplugin.- Select the
Assets > Import Package > Custom Packagemenu item. - Import
FirebaseMessaging.unitypackagefrom the Firebase Unity SDK, downloaded previously. - Click the
Importwhen theImport Unity Packagewindow appears.
- Select the
- Add the
GoogleService-Info.plistfile to the project.-
Navigate to the
Assets\TestAppfolder in theProjectwindow. -
Drag the
GoogleService-Info.plistdownloaded from the Firebase console into the folder.NOTE:
GoogleService-Info.plistcan be placed anywhere in the project.
-
- Optional: Update the Project Bundle Identifier
- If you did not use
com.google.firebase.unity.messaging.testappas the project package name you will need to update the sample's Bundle Identifier.- Select the
File > Build Settingsmenu option. - Select
iOSin thePlatformlist. - Click
Player Settings - In the
Player Settingspanel scroll down toBundle Identifierand update the value to the package name you provided when you registered your app with Firebase.
- Select the
- If you did not use
- Build for iOS
- Select the
File > Build Settingsmenu option. - Select
iOSin thePlatformlist. - Click
Switch Platformto selectiOSas the target platform. - Wait for the spinner (compiling) icon to stop in the bottom right corner of the Unity status bar.
- Click
Build and run, when Xcode opens stop the build.- NOTE If you click
Build and runand let the sample run, it will not be able to receive messages.
- NOTE If you click
- Configure the Xcode project for push messaging.
- Select the
Unity-iPhoneproject from theNavigator area. - Select the
Unity-iPhonetarget from theEditor area. - Select the
Generaltab from theEditor area. - Scroll down to
Linked Frameworks and Librariesand click the+button to add a framework.- In the window that appears, scroll to
UserNotifications.frameworkand click on that entry, then click onAdd. This framework will only appear in Xcode version 8 and higher, required by this library.
- In the window that appears, scroll to
- Select the
Capabilitiestab from theEditor area. - Switch
Push NotificationstoOn. - Scroll down to
Background Modesand switch it toOn. - Tick the
Remote notificationsbox underBackground Modes.
- Select the
- Build the Xcode project by selecting
Project->Runfrom the menu.
- Select the
- See the Using the Sample section below.
- Register your Android app with Firebase.
- Create a project in the
Firebase console,
and attach your Android app to it.
-
You should use
com.google.firebase.unity.messaging.testappas the package name while you're testing.- If you do not use the prescribed package name you will need to update
the bundle identifier as described in the
Optional: Update the Project Bundle Identifierbelow.
- If you do not use the prescribed package name you will need to update
the bundle identifier as described in the
-
To generate a SHA1, first you will need to set the keystore in the Unity project.
- Locate the
Publishing SettingsunderPlayer Settings. - Select an existing keystore, or create a new keystore using the toggle.
- Select an existing key, or create a new key using "Create a new key".
- Locate the
-
After setting the keystore and key, you can generate a SHA1 by running this command:
keytool -exportcert -list -v -alias <key_name> -keystore <path_to_keystore>
-
- Download the
google-services.jsonfile associated with your Firebase project from the console. This file identifies your Android app to the Firebase backend, and will need to be included in the sample later.- For further details please refer to the general instructions which describes how to configure a Firebase application for Android.
- Create a project in the
Firebase console,
and attach your Android app to it.
- Download the Firebase Unity SDK and unzip it somewhere convenient.
- Open the sample project in the Unity editor.
- Select the
File > Open Projectmenu item. - Click
Open. - Navigate to the sample directory
testappin the file dialog and clickOpen.
- Select the
- Open the scene
MainScene.- Navigate to
Assets/TestApp/MainScenein theProjectwindow. - Double click on
MainScenefile to open.
- Navigate to
- Import the
Firebase Messagingplugin.- Select the
Assets > Import Package > Custom Packagemenu item. - Import
FirebaseMessaging.unitypackagefrom the Firebase Unity SDK, downloaded previously. - Click the
Importwhen theImport Unity Packagewindow appears.
- Select the
- Add the
google-services.jsonfile to the project.-
Navigate to the
Assets\TestAppfolder in theProjectwindow. -
Drag the
google-services.jsondownloaded from the Firebase console into the folder.NOTE:
google-services.jsoncan be placed anywhere in the project.
-
- Optional: Update the Project Bundle Identifier
- If you did not use
com.google.firebase.unity.messaging.testappas the project package name you will need to update the sample's Bundle Identifier.- Select the
File > Build Settingsmenu option. - Select
Androidin thePlatformlist. - Click
Player Settings - In the
Player Settingspanel scroll down toBundle Identifierand update the value to the package name you provided when you registered your app with Firebase.
- Select the
- If you did not use
- Build for Android
- Select the
File > Build Settingsmenu option. - Select
Androidin thePlatformlist. - Click
Switch Platformto selectAndroidas the target platform. - Wait for the spinner (compiling) icon to stop in the bottom right corner of the Unity status bar.
- Click
Build and Run.
- Select the
- See the Using the Sample section below.
Before running the sample on iOS you must create a APNs certificate if you haven't already, then associate it with your sample project in the Firebase console:
- Inside your project in the Firebase console, select the gear icon,
select
Project Settings, and then select theCloud Messagingtab. - Select the
Upload Certificatebutton for your development certificate, your production certificate, or both. At least one is required. - For each certificate, select the .p12 file, and provide the password, if any. Make sure the bundle ID for this certificate matches the bundle ID of your app. Select Save.
Failure to associate the sample with an APNs certificate will result in the iOS application being unable to receive messages.
-
When you run the app, it will print:
Received Registration Token: <registration_token>this token can be used to send a notification to a single device.- When running the app on iOS, the token can be accessed via Xcode's console output.
- When running the app on Android, the token can be accessed using the
ADB command line with the
adb logcatcommand.
-
To send messages from your own server or the command line you will need the
Server Key.- Open your project in the Firebase Console
- Click the gear icon then
Project settingsin the menu on the left - Select the
Cloud Messagingtab. - Copy the
Server Key
-
You can send a notification to a single device or group of devices with this token.
- Using the Firebase Console:
- Open the Firebase Console.
- Select
Notificationsin the left menu. - Change
TargettoSingle Deviceand paste in theRegistration Tokenfrom the device. - Fill out the rest of the field and press
Send Messageto send a notification.
- Using the command line:
- Replace
<Server Key>and<Registration Token>in this command and run it from the command line.
- Replace
- Using the Firebase Console:
curl --header "Authorization: key=<Server Key>" --header "Content-Type: application/json" https://android.googleapis.com/gcm/send -d '{"notification":{"title":"Hi","body":"Hello from the Cloud"},"data":{"score":"lots"},"to":"<Registration Token>"}'
- You can send a notification to a topic
(e.g "TestTopic") which notifies all devices subscribed to the topic.
- Using the Firebase Console:
- Open the Firebase Console.
- Select
Notificationsin the left menu. - Change
TargettoTopicand select the topic (this can take a few hours to appear after devices have subscribed). - Fill out the rest of the field and press
Send Messageto send a notification.
- Using the command line:
- Replace
<Server Key>and<Topic>in this command and run it from the command line.
- Replace
- Using the Firebase Console:
curl --header "Authorization: key=<Server Key>" --header "Content-Type: application/json" https://android.googleapis.com/gcm/send -d '{"notification":{"title":"Hi","body":"Hello from the Cloud"},"data":{"score":"lots"},"to":"/topics/<Topic>"}'
https://firebase.google.com/support/
Copyright 2016 Google, Inc.
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.