How do you implement push notifications in Android, and what are the best practices?
Instruction: Describe the process of integrating and optimizing push notifications in an Android application.
Updated
Official answer
Integrate the supported Firebase Messaging SDK and project configuration, then retrieve the app-instance registration token and update the trusted backend when onNewToken reports a change. A token is a delivery address for an app instance, not a permanent user ID or proof of authorization. Maintain its association with the signed-in account and remove stale account associations on logout.
Send through an authorized server environment; do not ship server credentials in the app. Use FirebaseMessagingService for relevant callbacks, but do not assume every message reaches onMessageReceived. Foreground messages normally do; a notification payload received while backgrounded is normally displayed by the system, and accompanying data is delivered through the launch intent on tap. Keep callback work short and schedule eligible longer work appropriately.
Configure notification channels on Android 8 and later, and request POST_NOTIFICATIONS in context for non-exempt notifications on Android 13 and later. Respect denied permission, channel settings and user preferences. Validate deep-link input and recheck account authorization when opening protected content. Avoid exposing sensitive details on the lock screen.
Handle changed/invalid tokens, duplicate or delayed messages and missing delivery by synchronizing authoritative state when needed. Test foreground/background behavior and denied notification permission separately. Measure delivery, opens and downstream actions with defined denominators; personalization is a hypothesis, not a guaranteed retention improvement.
FCM client setup, FCM receive behavior, Notification permission, Notification channels