DEV Community

Cover image for Testing Push Notification: Test Push Notification in iOS Simulator & Android
satyaprakash behera
satyaprakash behera

Posted on

Testing Push Notification: Test Push Notification in iOS Simulator & Android

Push notifications have become an important part of modern-day mobile applications, providing a convenient way for apps to communicate with users even when they are not actively using the app. However, implementing push notifications can be challenging, and testing them is essential to ensure their proper functioning. Fortunately, iOS and Android provide simulators that allow developers to test push notifications without the need for physical devices. This article will explore how to test push notifications in both the iOS Simulator and Android, providing developers with the knowledge they need to ensure their push notifications work as intended on these platforms.

Benefits of Testing Push Notifications in Simulator:

  1. There is no need to set up a provisioning profile and certificate to test that Push Notification is functioning.
  2. No third-party tools are required to push the payload.
  3. It even includes background content fetch notifications.
  4. It works remarkably with Rich notifications having videos, images, and controls.

Requirements for Testing Push Notification on the Simulator:

  1. - Xcode 11.4 and above
  2. - Mac OS 10.15.2 and above.

Confirming Xcode Version:

First, the most important thing is to confirm whether the version of Xcode is Xcode 11.4 or above. If not, please upgrade it first before continuing.

Creating an Xcode Project:

First, create an Xcode project, and then include the UserNotifications framework in AppDelegate.swift.

import UserNotifications

In application (_:didDiscardSceneSession:) in AppDelegate.swift, ask users for notification permission.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

UNUserNotificationCenter.current().delegate = self

let options: UNAuthorizationOptions = [.alert, .sound, .badge]

UNUserNotificationCenter.current().requestAuthorization(options: options) { (granted, error) in

if granted {

print(“Permission is granted”)

} else if let error = error {

print(“Permission is not granted: (error)”)

}

}

return true

}

Then, implement UNUserNotificationCenterDelegate to receive Push Notifications. Add the following code in AppDelegate.swift.

extension AppDelegate: UNUserNotificationCenterDelegate {

func userNotificationCenter(_ center: UNUserNotificationCenter,

willPresent notification: UNNotification,

withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

let options = UNNotificationPresentationOptions(arrayLiteral: .alert, .sound, .badge)

completionHandler(options)

}

func userNotificationCenter(_ center: UNUserNotificationCenter,

didReceive response: UNNotificationResponse,

withCompletionHandler completionHandler: @escaping () -> Void) {

completionHandler()

}

}

Finally, set AppDelegate to UNUserNotificationCenter.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

UNUserNotificationCenter.current().delegate = self

}

The project can now receive push notifications. Execute the project, and you can see that the App will request notification permission.

Sending Push Notifications to iOS Simulator:

The Release Note of Xcode 11.4 mentions how to send Push Notifications with simctl.

% xcrun simctl push com.example.my-app ExamplePush.apns

Where the parameters are:

  • : Refers to the identifier of the simulator device. Decide which simulator you use, and then you can find it by the way below.
  • com.example.my-app: This is the bundle identifier of the App to receive push notifications.
  • ExamplePush.apns: It is the content of push notifications you would like to send. To send push notifications to an iOS simulator, you can use the simctl command-line tool provided by Xcode. Here are the steps:
  1. Ensure that your app is running in the simulator. You can launch it from Xcode or by opening the simulator app and selecting the appropriate device and app.
  2. Then, you can open a terminal window and navigate to the directory where you have saved the push notification payload file (e.g., ExamplePush.apns).
  3. Determine the identifier of the simulator device you want to send the push notification to. To do this, run the following command in the terminal:

xcrun simctl list devices:

This will display a list of all the available simulator devices and their identifiers. Choose the identifier of the simulator device you want to target.

  1. Use the following command to send the push notification to the simulator device: xcrun simctl push com.example.my-app ExamplePush.apns
  • Replace with the identifier of the simulator device you want to target, com.example.my-app with the bundle identifier of your app, and ExamplePush.apns with the name of the file containing the push notification payload.
  • After running the command, you can see the push notification appear in your app’s notification center.

Testing push notifications in an Android:

Why do you need to test Push Notification in Android?

Push notifications are an integral part of mobile apps, and they provide an effective way to engage users with relevant and timely information. Testing push notifications in Android is crucial to ensure that they are working correctly and delivering the intended message to the user. Below, we will discuss why you need to test push notifications in Android and provide examples to illustrate their importance.

- Ensure the push notification is delivered on time
One of the primary reasons to test push notifications in Android is to ensure that they are delivered on time. In some cases, push notifications may be delayed or not delivered at all, which can have a significant impact on user engagement. By testing push notifications, you can identify and address any issues that may prevent them from being delivered on time.

For example, let’s say you have a food delivery app that sends push notifications to users when their food is ready for pickup. If the push notification is delayed, the user may arrive at the restaurant before their food is ready, leading to a negative experience. By testing push notifications, you can ensure that the notification is delivered on time, allowing the user to plan their visit accordingly.

- Verify the content of the push notification
Another reason to test push notifications in Android is to verify the content of the notification. Push notifications can include text, images, and links, and it is essential to ensure that the content is accurate and relevant to the user.

For example, let’s say you have a fitness app that sends push notifications to users to remind them to exercise. If the notification includes incorrect or irrelevant information, the user may become frustrated and disengage from the app. By testing push notifications, you can ensure that the content is accurate and relevant, providing a positive user experience.

- Test the functionality of the push notification
Another reason to test push notifications in Android is to test the functionality of the notification. Push notifications can include actions that allow the user to interact with the app without opening it, such as replying to a message or completing a task.

For example, let’s say you have a messaging app that allows users to reply to messages directly from the notification. If the reply functionality is not working correctly, the user may not be able to respond to messages, leading to a negative experience. By testing push notifications, you can ensure that the functionality is working correctly, providing a seamless user experience.

Requirements to test the push notifications on Android:

To test push notifications on Android, you will need the following:

  1. Android device: You will need an Android device to test push notifications on Android. You can use a physical device or an Android emulator. 2.** Android Studio:** Android Studio is an integrated development environment (IDE) for Android app development. It includes tools for building, testing, and debugging Android apps.
  2. Firebase project: Firebase is a mobile and web application development platform owned by Google. To test push notifications, you will need to set up a Firebase project and add your app to the project.
  3. Google Play Services: Google Play Services is a set of APIs that provides features such as push notifications, location services, and authentication. To test push notifications, you must have Google Play Services installed on your device or emulator.
  4. Implementation of Firebase Cloud Messaging: FCM is a cross-platform messaging solution that allows you to send messages and notifications to Android, iOS, and web applications. You will need to implement FCM in your app to receive push notifications.
  5. A server to send push notifications: To send push notifications, you will need a server that can send messages to the FCM server. You can use the Firebase console or a third-party service to send push notifications.
  6. Code to handle the push notification: You will need to write code to handle the push notification when it is received by the app. This code will be executed when the app is in the foreground, background, or closed. You will need to handle each scenario differently. By meeting these requirements, you can test push notifications on Android and ensure that they are working correctly.

Testing push notifications in an Android app:

To test push notifications in an Android app, you can follow these steps:

- Set up a Firebase project:
The first step is to create or select an existing Firebase project in the Firebase console. To create a new project, you need to sign in to the console using your Google account and then click on the “Add Project” button. Enter the name of the project and click on “Create Project”. Once you’ve created the project, add your app to the project by clicking on “Add App” and selecting “Android” as the platform. Follow the on-screen instructions to register your app.

After adding your app, you need to download the google-services.json file. This file contains the Firebase configuration details that your app needs to use Firebase services. To download the file, click on the “Download google-services.json” button on the Firebase console and place the file in the app-level directory of your Android Studio project.

- Add Firebase dependencies:
To use Firebase Cloud Messaging (FCM) in your app, you need to add the Firebase Messaging dependency to your app-level build.gradle file. You can do this by adding the following code to the dependencies block:

dependencies {

// …

implementation ‘com.google.firebase:firebase-messaging:22.0.0’

}

After adding the dependency, sync your project with Gradle files.

- Configure the Firebase Messaging service:
Next, you need to create a new class that extends FirebaseMessagingService and override the onMessageReceived method. This method is called when a push notification is received by the device. In this method, you can handle the received notification.

public class MyFirebaseMessagingService extends FirebaseMessagingService {

@override

public void onMessageReceived(RemoteMessage remoteMessage) {

super.onMessageReceived(remoteMessage);

// Handle the notification

}

}

In the above code, MyFirebaseMessagingService is the class that extends FirebaseMessagingService, and onMessageReceived is the method that is called when a push notification is received. You can customize this method according to your app’s requirements.

Register the service in the manifest file
After creating the FirebaseMessagingService class, you need to register it in the manifest file. To do this, add the following code to the manifest file:

In the above code, MyFirebaseMessagingService is the name of the service that you created in step 3.

- Test the push notification
Finally, you need to test the push notification. You can use the Firebase console or a third-party tool to send a push notification to your app. The onMessageReceived method in MyFirebaseMessagingService should be called when the notification is received.

Note that, to receive the push notification, your app needs to be in the foreground or background. If the app is closed, the notification will be handled differently by the system. Also, make sure that you have granted the required permissions to your app to receive the push notifications.

That’s it! By following these steps, you can test push notifications in your Android app.

Conclusion:

Testing push notifications is a crucial step in the development of any mobile application. In this article, we have covered the process of testing push notifications in iOS Simulator and Android, providing a step-by-step guide to help you get started.

Source: This article was originally published at testgrid.io.

Top comments (1)

Collapse
 
aniruddhaadak profile image
ANIRUDDHA ADAK

Absolutely incredible! 😻.