|
| 1 | +Firebase Cloud Messaging Quickstart |
| 2 | +=================================== |
| 3 | + |
| 4 | +The Firebase Cloud Messaging Test Application (testapp) demonstrates receiving |
| 5 | +Firebase Cloud Messages using the Firebase Cloud Messaging C++ SDK. This |
| 6 | +application has no user interface and simply logs actions it's performing to the |
| 7 | +console. |
| 8 | + |
| 9 | +Introduction |
| 10 | +------------ |
| 11 | + |
| 12 | +- [Read more about Firebase Cloud Messaging](https://developers.google.com/cloud-messaging/) |
| 13 | + |
| 14 | +Building and Running the testapp |
| 15 | +-------------------------------- |
| 16 | +### iOS |
| 17 | + - Link your iOS app to the Firebase libraries. |
| 18 | + - Get access to the Firebase SDK git repo via |
| 19 | + [git cookie](https://cpdc-eap.googlesource.com/new-password). |
| 20 | + - Get CocoaPods version 1 or later by running, |
| 21 | + |
| 22 | + ``` |
| 23 | + $ sudo gem install CocoaPods --pre |
| 24 | + ``` |
| 25 | + - From the testapp directory, install the CocoaPods listed in the Podfile by |
| 26 | + running, |
| 27 | + ``` |
| 28 | + $ pod install |
| 29 | + ``` |
| 30 | + - Open the generated Xcode workspace (which now has the CocoaPods), |
| 31 | +
|
| 32 | + ``` |
| 33 | + $ open testapp.xcworkspace |
| 34 | + ``` |
| 35 | + - For further details please refer to the |
| 36 | + [general instructions for setting up an iOS app with Firebase](https://developers.google.com/firebase/docs/ios/setup). |
| 37 | +
|
| 38 | + - Register your iOS app with Firebase. |
| 39 | + - Create a new app on |
| 40 | + [developers.google.com](https://developers.google.com/mobile/add?platform=android&cntapi=messaging&cntapp=Cloud%20Messaging%20Test%20App&cntpkg=com.google.android.messaging.testapp) |
| 41 | + , and attach your iOS app to it. |
| 42 | + - For Messaging, you will need an App Store ID. Use something random such |
| 43 | + as 12345678." |
| 44 | + - You can use "com.google.ios.messaging.testapp" as the iOS Bundle ID |
| 45 | + while you're testing. |
| 46 | + - Add the GoogleService-Info.plist that you downloaded from Firebase |
| 47 | + console to the testapp root directory. This file identifies your iOS app |
| 48 | + to the Firebase backend. |
| 49 | + - Add the following frameworks from the Firebase C++ SDK to the project: |
| 50 | + - frameworks/ios/universal/firebase.framework |
| 51 | + - frameworks/ios/universal/firebase_messaging.framework |
| 52 | + - You will need to either, |
| 53 | + 1. Check "Copy items if needed" when adding the frameworks, or |
| 54 | + 2. Add the framework path in "Framework Search Paths" |
| 55 | + - e.g. If you downloaded the Firebase C++ SDK to |
| 56 | + `/Users/me/firebase_cpp_sdk`, |
| 57 | + then you would add the path |
| 58 | + `/Users/me/firebase_cpp_sdk/frameworks/ios/universal`. |
| 59 | + - To add the path, in XCode, select your project in the project |
| 60 | + navigator, then select your target in the main window. |
| 61 | + Select the "Build Settings" tab, and click "All" to see all |
| 62 | + the build settings. Scroll down to "Search Paths", and add |
| 63 | + your path to "Framework Search Paths". |
| 64 | + - You need a valid |
| 65 | + [APNs](https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/ApplePushService.html) |
| 66 | + certificate. If you don't already have one, see |
| 67 | + [Provisioning APNs SSL Certificates.](https://developers.google.com/firebase/docs/cloud-messaging/ios/certs) |
| 68 | + - In XCode, build & run the sample on an iOS device or simulator. |
| 69 | + - The testapp has no user interface. The output of the app can be viewed |
| 70 | + via the console. In Xcode, select |
| 71 | + `View --> Debug Area --> Activate Console` from the menu. |
| 72 | +
|
| 73 | +### Android |
| 74 | +**Register your Android app with Firebase.** |
| 75 | +
|
| 76 | +- Create a new app on |
| 77 | +[developers.google.com](https://developers.google.com/mobile/add?platform=android&cntapi=messaging&cntapp=Cloud%20Messaging%20Test%20App&cntpkg=com.google.android.messaging.testapp) |
| 78 | +, and attach your Android app to it. |
| 79 | +- You can use "com.google.android.messaging.testapp" as the Package Name |
| 80 | +while you're testing. |
| 81 | +- To [generate a SHA1](https://developers.google.com/android/guides/client-auth) |
| 82 | +run this command (_Note: the default password is 'android'_): |
| 83 | + * Mac and Linux: |
| 84 | +
|
| 85 | + ``` |
| 86 | + keytool -exportcert -list -v -alias androiddebugkey -keystore ~/.android/debug.keystore |
| 87 | + ``` |
| 88 | + * Windows: |
| 89 | +
|
| 90 | + ``` |
| 91 | + keytool -exportcert -list -v -alias androiddebugkey -keystore %USERPROFILE%\.android\debug.keystore |
| 92 | + ``` |
| 93 | +- If keytool reports that you do not have a debug.keystore, you can |
| 94 | +[create one](http://developer.android.com/tools/publishing/app-signing.html#signing-manually) |
| 95 | +with: |
| 96 | +
|
| 97 | + ``` |
| 98 | + keytool -genkey -v -keystore ~/.android/debug.keystore -storepass android -alias androiddebugkey -keypass android -dname "CN=Android Debug,O=Android,C=US" |
| 99 | + ``` |
| 100 | +- Add the `google-services.json` file that you downloaded from Firebase |
| 101 | + console to the root directory of testapp. This file identifies your |
| 102 | + Android app to the Firebase backend. |
| 103 | +- For further details please refer to the |
| 104 | +[general instructions for setting up an Android app with Firebase](https://developers.google.com/firebase/docs/android/setup). |
| 105 | +
|
| 106 | +**Configure your SDK paths** |
| 107 | +
|
| 108 | +- Configure the location of the Firebase C++ SDK by setting the |
| 109 | +`firebase_cpp_sdk.dir` Gradle property to the SDK install directory. |
| 110 | + * Run this command in the project directory, and modify the path to match your |
| 111 | + local installation path: |
| 112 | +
|
| 113 | + ``` |
| 114 | + > echo "systemProp.firebase_cpp_sdk.dir=/User/$USER/firebase_cpp_sdk" >> gradle.properties |
| 115 | + ``` |
| 116 | +- Ensure the Android SDK and NDK locations are set in Android Studio. |
| 117 | + * From the Android Studio launch menu, go to |
| 118 | + `Configure/Project Defaults/Project Structure` and download the SDK and NDK |
| 119 | + if the locations are not yet set. |
| 120 | + * Android Studio will write these paths to `local.properties`. |
| 121 | +
|
| 122 | +**Build & Run** |
| 123 | +
|
| 124 | +- Open `build.gradle` in Android Studio. |
| 125 | + - From the Android Studio launch menu, "Open an existing Android Studio |
| 126 | + project", and select `build.gradle`. |
| 127 | +- Install the SDK Platforms that Android Studio reports missing. |
| 128 | +- Build the testapp and run it on an Android device or emulator. |
| 129 | +- See [below](#using_the_test_app) for usage instructions. |
| 130 | +
|
| 131 | +Using the Test App |
| 132 | +------------------ |
| 133 | +
|
| 134 | +- Install and run the test app on your iOS or Android device or emulator. |
| 135 | +- The application has minimal user interface. The output of the app can be |
| 136 | +viewed via the console: |
| 137 | + * **iOS**: Open select "View --> Debug Area --> Activate Console" from the |
| 138 | + menu in Xcode. |
| 139 | + * **Android**: View the logcat output in Android studio or by running |
| 140 | + "adb logcat" from the command line. |
| 141 | +- When you first run the app, it will print: |
| 142 | +`Recieved Registration Token: <code>`. Copy this code to a text editor. |
| 143 | +- Copy the ServerKey from the firebase console: |
| 144 | + - Open your project in the [firebase console](g.co/firebase) |
| 145 | + - Click `Notifications` in the menu on the left |
| 146 | + - Select the `Credentials` tab. |
| 147 | + - Copy the `Server Key` |
| 148 | +- Replace `<Server Key>` and `<Registration Token>` in this command and run it |
| 149 | +from the command line. |
| 150 | +``` |
| 151 | +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>"}' |
| 152 | +``` |
| 153 | +- Observe the command received in the app, via the console output. |
| 154 | +
|
| 155 | +Support |
| 156 | +------- |
| 157 | +
|
| 158 | +[https://developers.google.com/firebase/support/]() |
| 159 | +
|
| 160 | +License |
| 161 | +------- |
| 162 | +
|
| 163 | +Copyright 2016 Google, Inc. |
| 164 | +
|
| 165 | +Licensed to the Apache Software Foundation (ASF) under one or more contributor |
| 166 | +license agreements. See the NOTICE file distributed with this work for |
| 167 | +additional information regarding copyright ownership. The ASF licenses this |
| 168 | +file to you under the Apache License, Version 2.0 (the "License"); you may not |
| 169 | +use this file except in compliance with the License. You may obtain a copy of |
| 170 | +the License at |
| 171 | +
|
| 172 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 173 | +
|
| 174 | +Unless required by applicable law or agreed to in writing, software |
| 175 | +distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 176 | +WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 177 | +License for the specific language governing permissions and limitations under |
| 178 | +the License. |
| 179 | +
|
0 commit comments