The Firebase Messaging Unity Sample demonstrates receiving messages from Firebase Cloud Messaging using the Firebase Unity SDK.
- Unity The quickstart project requires 2019 or higher.
- Xcode 13.3.1 or higher (when developing for iOS or tvOS).
- Android SDK (when developing for Android).
- Open the sample project in the Unity editor.
- Select the File > Open Project menu item.
- Click Open.
- Navigate to the sample directory
testappin the file dialog and click Open.- You might be prompted to upgrade the project to your version of Unity. Click Confirm to upgrade the project and continue.
- Open the scene
MainScene.- Navigate to
Assets/Firebase/Sample/Messagingin the Project window. - Double click on
MainScenefile to open it.
- Navigate to
- Complete Add Firebase to your Unity project AND Set up a Firebase Cloud Messaging client app with Unity but do not implement the code sections as this codebase already has those functionalities.
- If at any point you run into issues with building and installing the sample, complete Debugging the Game Build, Install and Run Process and follow the instructions therein.
- 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 or tvOS, 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.
tvOS NOTE: This testapp was designed for use on iOS and Android targets, and when running in the Unity editor. While the code will also execute on tvOS, the buttons will be unresponsive as there isn't an easy way to provide the app with the click / touch events to orchestrate the UI elements on that platform.
NOTE: Dispatching messages from your server is a more extensible way of sending notifications or data messages to clients. The following is an easy way to demo this functionality. If you are interested in learning more about how to send messages in production from the command line or the Firebase Admin SDK, read more about it starting with Your server environment and FCM.
- Navigate to the FCM REST API Docs for Method: projects.messages.send
- Look for the "Try this method" panel
- This might require you to expand your browser window.
- Follow the API doc to understand what IDs and fields to fill in for your particular use case.
- Your project number is available from Project settings
- Fill in the
Request bodyreferencing the FCM REST API docs. - Finally, execute.
Reminder, while this process is currently done from our website, it uses the FCM v1 Send API directly and provides the simplest starting point to switch to using the Send API or Admin SDKs in production.
NOTE: This is a simple but less flexible way of sending notifications to clients. It is recommended that in production you Send a message from your server environment instead.
-
You can send a notification to a single device or group of devices with this token.
- Using the Firebase Console:
- Select Notifications in the left menu.
- Change Target to Single Device and paste in the Registration Token from the device.
- Fill out the rest of the field and press Send Message to send a notification.
- Using the Firebase Console:
-
You can send a notification to a topic (e.g "TestTopic") which notifies all devices subscribed to the topic.
- Using the Firebase Console:
- Select Notifications in the left menu.
- Change Target to Topic and either:
- Select the topic from the list.
- This can take a few hours to appear after devices have subscribed.
- Type the topic in manually.
- Do this if the device has subscribed but the topic has not yet appeared in the list of options.
- Select the topic from the list.
- Fill out the rest of the field and press Send Message to send a notification.
- Using the Firebase Console:
- Please see the Known Issues section of the Unity Setup Guide for other troubleshooting topics.
- When running the app, if all that you see is a blue horizon, then please
ensure that you followed the steps to Open the scene
MainSceneabove. - Again, if at any point you run into issues with building and installing the sample, complete Debugging the Game Build, Install and Run Process and follow the instructions therein.
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.