Android Push Notification Using Firebase Cloud Messaging in Kotlin| Part -1
Автор: Kotlin App Dev
Загружено: 2022-09-17
Просмотров: 2203
Описание:
Android Push Notification Using Firebase Cloud Messaging in Kotlin| Part -1 #pushnotifications
Edit your app manifest
Add the following to your app's manifest:
A service that extends FirebaseMessagingService. This is required if you want to do any message handling beyond receiving notifications on apps in the background. To receive notifications in foregrounded apps, to receive data payload, to send upstream messages, and so on, you must extend this service.
(Optional) From Android 8.0 (API level 26) and higher, notification channels are supported and recommended. FCM provides a default notification channel with basic settings. If you prefer to create and use your own default channel, set default_notification_channel_id to the ID of your notification channel object as shown; FCM will use this value whenever incoming messages do not explicitly set a notification channel. To learn more, see Manage notification channels.
Request runtime notification permission on Android 13+
Android 13 introduces a new runtime permission for showing notifications. This affects all apps running on Android 13 or higher that use FCM notifications.
By default, the FCM SDK (version 23.0.6 or higher) includes the POST_NOTIFICATIONS permission defined in the manifest. However, your app will also need to request the runtime version of this permission via the constant, android.permission.POST_NOTIFICATIONS. Your app will not be allowed to show notifications until the user has granted this permission.
Notification permissions for apps targeting Android 12L (API level 32) or lower
Android automatically asks the user for permission the first time your app creates a notification channel, as long as the app is in the foreground. However, there are important caveats regarding the timing of channel creation and permission requests:
If your app creates its first notification channel when it is running in the background (which the FCM SDK does when receiving an FCM notification), Android will not allow the notification to be displayed and will not prompt the user for the notification permission until the next time your app is opened. This means that any notifications received before your app is opened and the user accepts the permission will be lost.
We strongly recommend that you update your app to target Android 13+ to take advantage of the platform’s APIs to request permission. If that is not possible, your app should create notification channels before you send any notifications to the app in order to trigger the notification permission dialog and ensure no notifications are lost. See notification permission best practices for more information.
Access the device registration token
Send a test message to a backgrounded app
bookmark_border
To get started with FCM, build out the simplest use case: sending a test notification message from the Notifications composer to a development device when the app is in the background on the device. This page lists all the steps to achieve this, from setup to verification — it may cover steps you already completed if you have set up an Android client app for FCM.
On initial startup of your app, the FCM SDK generates a registration token for the client app instance. If you want to target single devices or create device groups, you'll need to access this token by extending FirebaseMessagingService and overriding onNewToken.
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.
The registration token may change when:
The app is restored on a new device
The user uninstalls/reinstall the app
The user clears app data.
Create a Firebase project
Before you can add Firebase to your Android app, you need to create a Firebase project to connect to your Android app. Visit Understand Firebase Projects to learn more about Firebase projects.
Add a Firebase configuration file
Download and then add the Firebase Android configuration file (google-services.json) to your app:
Click Download google-services.json to obtain your Firebase Android config file.
Move your config file into the module (app-level) root directory of your app.
Firebase notifications behave differently depending on the foreground/background state of the receiving app. If you want foregrounded apps to receive notification messages or data messages, you’ll need to write code to handle the onMessageReceived callback. For an explanation of the difference between notification and data messages, see Message types.
What do you need to know about this config file?
To make the values in your google-services.json config file accessible to Firebase SDKs, you need the Google services Gradle plugin (google-services).
Send messages to foregrounded apps
Once you have successfully sent notification messages while your app is in the background, see Receive Messages in an Android
Повторяем попытку...

Доступные форматы для скачивания:
Скачать видео
-
Информация по загрузке: