The OnTokenRefresh method is invoked when the registration token is created or changed. Before you build your app, make sure that its package name matches the package name that you entered into the Firebase Console when you created your Firebase project. Rebuild the app, run it, and watch the Output window until the token is displayed. For example:. On the Compose message page, enter in a message under Message text.
Select Single device as the target and paste in the registration token that you copied from the IDE Output window:. The notification icon should appear on the Android device:.
Open the notification to view the message; the notification message should be exactly what you typed into the Firebase Console:. This simple example only touches the surface of what you can do with FCM and push notifications using Visual Studio.
To learn more about Firebase Cloud Messaging with Xamarin. Comments are closed. Community Resources. NET Meetups. Visual Studio Blog. This merge process automatically adds any permissions and other FCM elements that are needed for connection to FCM servers. An initial layout for the app's UI will be created first.
This TextView will be used to display messages that indicate whether Google Play Services is installed. Save the changes to Main. Add the following method to the MainActivity class:. If it is not installed, a message is displayed in the TextBox that instructs the user to download an APK from the Google Play Store or to enable it in the device's system settings.
Apps that are running on Android 8. Add the following method to the MainActivity class which will create the notification channel if necessary :. The method CreateNotificationChannel is called to ensure that a notification channel exists for devices running Android 8 or higher.
Completely rebuild and run the app. If all is configured properly, you should see a screen that looks like the following screenshot:. Also verify that you have added the Xamarin.
Base package to your FCMClient project as explained earlier. The next step is to add a service that extends FirebaseInstanceIdService to handle the creation, rotation, and updating of Firebase registration tokens. When the FirebaseInstanceIdService service is added to the client app, the app will automatically receive FCM messages and display them as notifications whenever the app is backgrounded.
Edit AndroidManifest. Declares a FirebaseInstanceIdReceiver implementation that provides a unique identifier for each app instance. This receiver also authenticates and authorizes actions. Declares an internal FirebaseInstanceIdInternalReceiver implementation that is used to start services securely. The app ID is stored in the google-services. The Xamarin. FirebaseInstanceIdService performs the following steps:. In return, the app gets back a registration token from FCM.
This service implements an OnTokenRefresh method that is invoked when the registration token is initially created or changed. Token property which is updated asynchronously by FCM.
In this example, the refreshed token is logged so that it can be viewed in the output window:. OnTokenRefresh is invoked infrequently: it is used to update the token under the following circumstances:. OnTokenRefresh also calls SendRegistrationToAppServer to associate the user's registration token with the server-side account if any that is maintained by the application:.
Because this implementation depends on the design of the app server, an empty method body is provided in this example. Note that the token is opaque to the client app.
When a token is sent to the app server, SendRegistrationToAppServer should maintain a boolean to indicate whether the token has been sent to the server.
If this boolean is false, SendRegistrationToAppServer sends the token to the app server — otherwise, the token was already sent to the app server in a previous call. In some cases such as this FCMClient example , the app server does not need the token; therefore, this method is not required for this example. Now that the receiver services are in place, client app code can be written to take advantage of these services.
In the following sections, a button is added to the UI to log the registration token also called the Instance ID token , and more code is added to MainActivity to view Intent information when the app is launched from a notification:. The code added in this step is intended only for demonstration purposes — a production client app would have no need to log registration tokens.
This code logs the current token to the output window when the Log Token button is tapped. When the user taps a notification issued from FCMClient , any data accompanying that notification message is made available in Intent extras. The app's launcher Intent is fired when the user taps its notification message, so this code will log any accompanying data in the Intent to the output window.
Build and run the FCMClient app. The Log Token button is displayed:. Tap the Log Token button. This section describes how to retrieve the token and how to monitor changes to the token.
Because the token could be rotated after initial startup, you are strongly recommended to retrieve the latest updated registration token. When you need to retrieve the current token, call FirebaseMessaging. The onNewToken callback fires whenever a new token is generated.
After you've obtained the token, you can send it to your app server and store it using your preferred method. It is recommended to do this in two places: in the main activity's onCreate method, and in its onResume method. The check in onCreate ensures that the app can't be used without a successful check. The check in onResume ensures that if the user returns to the running app through some other means, such as through the back button, the check is still performed. If the device doesn't have a compatible version of Google Play services, your app can call GoogleApiAvailability.
Prevent auto initialization When an FCM registration token is generated, the library uploads the identifier and configuration data to Firebase. If you prefer to prevent token autogeneration, disable Analytics collection and FCM auto initialization you must disable both by adding these metadata values to your AndroidManifest. For example:. After the client app is set up, you are ready to start sending downstream messages with the Notifications composer.
This functionality is demonstrated in the quickstart sample , which you can download, run, and review. To add other, more advanced behavior to your app, you can declare an intent filter and implement an activity to respond to incoming messages.
For details, see the guides for sending messages from an app server:. Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.
For details, see the Google Developers Site Policies. Products Build. Firebase Documentation. Emulator Suite. Overview Analytics. Remote Config. What can I do with Remote Config? Key concepts and strategies. Products Build. Firebase Documentation. Emulator Suite. Overview Analytics. Remote Config. What can I do with Remote Config? Key concepts and strategies. API reference. Use case examples. Legacy Predictions deprecated.
Use Cases. Cloud Messaging. Web JavaScript.
0コメント