How to Add Push Notifications to Your Native Mobile App

Push notifications are a powerful tool for maintaining user engagement and delivering timely updates. They can help you communicate with your users, provide them with updates, and even drive certain actions within your app. This article will guide you through the process of adding push notifications to your native mobile app.

What are Push Notifications?

Push notifications are messages that pop up on a user's mobile device. They don't require the user to be in the app or using their device to receive them. They can do a lot of things; for example, they can show the latest sports scores, get a user to take action, such as downloading a coupon, or let a user know about an event, such as a flash sale.

Why Use Push Notifications?

Push notifications provide convenience and value to app users. For new users, they can be a call to action to engage with the app for the first time. For regular users, they can help promote a loyalty program or other incentives.

How Do Push Notifications Work?

Push notifications are delivered from an app server to the user's device. A push notification interface handles the delivery and presentation of the notification to the user.

Here's a simplified diagram of how push notifications work:


Adding Push Notifications to Your App

The process of adding push notifications to your app involves several steps:

1. Setting Up a Firebase Project

Firebase Cloud Messaging (FCM) is a free service offered by Google that lets you send notifications and messages to users across platforms—Android, iOS, and the web. You'll need to set up a Firebase project and add Firebase to your app before you can send push notifications.

2. Getting a User's Device Token

Each app instance on a user's device is assigned a unique registration token. You'll need to request this token because it's used to send messages to each unique app instance.

3. Sending a Notification Message

Once you have the user's device token, you can send a notification message. This message is sent from your app server to the FCM, which then sends the message to the user's device.

4. Handling Messages

When your app receives a message, it needs to handle the incoming data. The way you handle the data depends on the type of message and the data payload it contains.

Implementing Push Notifications in Android

To add push notifications to your Android app, you'll need to use Firebase Cloud Messaging. Here's a step-by-step guide on how to do it:

1. Set Up the SDK

You'll need to add Firebase to your Android project. If you haven't already, follow the instructions on the Firebase documentation to do so.

2. Edit Your App Manifest

Your app's manifest file needs to be updated to include a service that extends FirebaseMessagingService. This is required if you want to do any message handling beyond receiving notifications on apps in the background.

3. Request Runtime Notification Permission

Starting from Android 13, a new runtime permission for showing notifications has been introduced. Your app will need to request this permission.

4. Access the Device Registration Token

On initial startup of your app, the FCM SDK generates a registration token for the client app instance. You'll need to access this token to target single devices or create device groups.

5. Monitor Token Generation

The onNewToken callback fires whenever a new token is generated. This is important because the registration token may change when:

  • The app is restored on a new device.

  • The user uninstalls/reinstalls the app.

  • The user clears app data.

To monitor token generation, you need to extend FirebaseMessagingService and override onNewToken. Here's how you can do it in Kotlin:



  1. override fun onNewToken(token: String) {  
  2.     Log.d(TAG, "Refreshed token: $token")  
  3.     // If you want to send messages to this application instance or  
  4.     // manage this apps subscriptions on the server side, send the  
  5.     // FCM registration token to your app server.  
  6.     sendRegistrationToServer(token)  
  7. }  


After you've obtained the token, you can send it to your app server and store it using your preferred method[^1^].

6. Check for Google Play Services

Apps that rely on the Play Services SDK should always check the device for a compatible Google Play services APK before accessing Google Play services features. 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[^1^].

7. 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.xml:



  1. <meta-data  
  2.     android:name="firebase_messaging_auto_init_enabled"  
  3.     android:value="false" />  
  4. <meta-data  
  5.     android:name="firebase_analytics_collection_enabled"  
  6.     android:value="false" />  


To re-enable FCM auto-init, make a runtime call:



  1. Firebase.messaging.isAutoInitEnabled = true  


These values persist across app restarts once set[^1^].

8. The Role and Benefits of Using Mobile Cascading Style Sheets (MCSS) in the Development Process

MCSS plays a crucial role in the development process of mobile applications. It provides a way to improve efficiency, maintain design consistency across platforms, enable real-time updates, and simplify the overall development process.

Improve Efficiency

MCSS allows developers to write CSS once and use it across multiple platforms. This means less time spent writing and debugging code, leading to improved efficiency.

Maintain Design Consistency

With MCSS, you can ensure that your app maintains a consistent look and feel across different platforms. This is because the same styles are applied regardless of the platform, ensuring a uniform user experience.

Enable Real-Time Updates

One of the key benefits of MCSS is the ability to make real-time updates to your app's styles. This means you can make changes to your app's appearance without having to release a new version of the app.

Simplify the Development Process

By using MCSS, you can simplify the development process. Instead of having to write separate styles for each platform, you can write one set of styles that work across all platforms. This not only simplifies the development process but also makes it easier to maintain your codebase.

MCSS can be integrated into various native Integrated Development Environments (IDEs) like Android Studio and Xcode. It helps organize the project by separating the presentation styles from the structure of the app, making it easier to manage and update the app's design.

Here's a diagram illustrating the benefits of using MCSS in the development process:


Implementing Push Notifications in iOS

Adding push notifications to your iOS app involves a similar process but uses Apple's Push Notification service (APNs) instead of FCM. Here's a step-by-step guide on how to do it:

1. Configure Your App for Push Notifications

In the Apple Developer Member Center, you'll need to configure your app for push notifications. This involves creating an App ID, enabling the push notifications capability, and creating a provisioning profile.

2. Create a Certificate

You'll need to create a push notification certificate in the Apple Developer Member Center. This certificate will be used to authenticate your server's push notification requests.

3. Register for Push Notifications

In your app's code, you'll need to register for push notifications. This involves calling the registerForRemoteNotifications method and implementing the didRegisterForRemoteNotificationsWithDeviceToken and didFailToRegisterForRemoteNotificationsWithError delegate methods.

4. Send a Push Notification

With the device token and certificate, you can now send a push notification. This involves making a POST request to the APNs servers with the device token and push notification payload.

For more detailed instructions, refer to the Apple documentation.

Conclusion

Adding push notifications to your native mobile app can greatly enhance user engagement and retention. With the help of MCSS, the process can be made more efficient and consistent. Remember to consider your users' preferences and respect their choice to opt-in or out of receiving notifications.

For more information on MCSS and its benefits, check out our press releases and documentation.


References

  • . (2023, January 5). . - YouTube. Retrieved May 31, 2023, from https://webengage.com/blog/mobile-app-retention-strategies

  • . (2023, January 5). . - YouTube. Retrieved May 31, 2023, from https://dev.to/mayurkadampro/angular-8-firebase-cloud-messaging-push-notifications-97a

  • . (2023, January 5). . - YouTube. Retrieved May 31, 2023, from https://niesinity575.amebaownd.com/posts/27781970

  • android - Firebase message not received on emulator. (2017, September 28). Stack Overflow. Retrieved May 31, 2023, from https://stackoverflow.com/questions/46464356/firebase-message-not-received-on-emulator

  • android - What is FCM token in Firebase? (2016, June 7). Stack Overflow. Retrieved May 31, 2023, from https://stackoverflow.com/questions/37671380/what-is-fcm-token-in-firebase

  • Best practices for FCM registration token management clarification. (2021, November 15). Stack Overflow. Retrieved May 31, 2023, from https://stackoverflow.com/questions/69980863/best-practices-for-fcm-registration-token-management-clarification

  • Checking if Google Play Service is enabled for FCM. (2017, October 19). Stack Overflow. Retrieved May 31, 2023, from https://stackoverflow.com/questions/46834353/checking-if-google-play-service-is-enabled-for-fcm

  • Push Notifications Explained. (n.d.). Airship. Retrieved May 31, 2023, from https://www.airship.com/resources/explainer/push-notifications-explained

  • Set up a Firebase Cloud Messaging client app on Android. (n.d.). Firebase. Retrieved May 31, 2023, from https://firebase.google.com/docs/cloud-messaging/android/client

  • Tondolo, M. (2018, July 27). Understanding Firebase: Part 1(Firebase Cloud Messaging) | by Musa Tondolo | Droid Wall. Medium. Retrieved May 31, 2023, from https://medium.com/geeks-wall/understanding-firebase-part-1-firebase-cloud-messaging-c6e2b8e9a33f

  • Waingankar, P. (2019, June 8). /* If you want to send messages to this application instance or ... Medium. Retrieved May 31, 2023, from https://medium.com/@prajwalwaingankar/if-you-want-to-send-messages-to-this-application-instance-or-manage-this-apps-subscriptions-on-6fa25cd93bb6

Subscribe newsletter

You confirm that you agree to the processing of your personal data as described in the Privacy Statement .