logo
In-app Chat
SDK Error Codes
Powered Byspreading
On this page

Implement silent push notifications

Introduction

Silent push notifications are a special type of remote notification, mainly used for data synchronization between the App running in the background and the server. For example: when the App is not active and the data within the App is outdated, the server will send a silent push notification, and the App will update the data without any user awareness.

Warning
  • Silent push notifications are primarily used for updating and synchronizing data, without any user awareness. Therefore, silent push notifications generally do not have content, sounds, or badge notifications.
  • When silent push notifications awaken the background App for download tasks, there is a maximum of 30 seconds to perform.
  • Notifications callbacks can be triggered when the App is running in the foreground/background, or when the background process is suspended (retaining the App's memory resources). Callbacks cannot be triggered after the App is closed.
  • Silent push requests are low priority tasks at APNs, and Apple does not guarantee the delivery rate of silent pushes.
  • Do not use silent push notifications to keep the App alive. If APNs detect a high frequency of silent push requests, it may terminate their transmission.
Note

This documentation is applicable to the following platforms: iOS and Android (only smart devices that support Google services).

Prerequisites

Before implementing silent push notifications, please make sure of the following:

Implementation steps

Send silent push notifications

  1. Contact ZEGOCLOUD technical support to configure the resourceID for carrying silent push strategies. There are two types of silent push strategies: iOS and FCM. Depending on your specific requirements, you can choose to only carry one type of silent push strategy's resourceID.
  2. In scenarios where offline push notifications need to be sent (such as call invitation, offline push notification, etc.), fill in the resourceID field of ZIMPushConfig with the pre-configured value obtained from ZEGOCLOUD Technical Support. Additionally, fill in the payload field according to the specific business scenario.
Untitled
pushConfig.resourcesID="Contact ZEGOCLOUD Technical Support to configure resourceID";
pushConfig.payload = "Fill in the desired additional content";
1
Copied!
  1. Once the above steps are completed, you can send silent push notifications to others.

Receive silent push notifications

App in the foreground

To receive online silent push notifications, implement the onThroughMessageReceived event.

Untitled
ZPNsEventHandler.onThroughMessageReceived = (ZPNsMessage message) {
    // Handle online silent push notifications
    String payload = message.extras['payload'] as String;
};
1
Copied!

When the iOS or Android app is in the background or when the Android app is terminated:

  1. Implement a top-level non-anonymous function similar to the code block shown below. Please note that it should be a non-initializing class method.
Untitled
// Note: @pragma('vm:entry-point') must be placed on the function to indicate that it can be resolved, allocated, or called directly from native or VM code in AOT mode.
@pragma('vm:entry-point')
Future<void> _zpnsMessagingBackgroundHandler(ZPNsMessage message) async{
    // The payload variable contains the content of the payload field filled during sending.
    String payload = message.extras['payload'] as String;
    // When the app is in the background and terminated, you can view the print output in logcat.
    print(payload);
}
1
Copied!
  1. Call the setBackgroundMessageHandler method as soon as possible when opening the app to pass the above function. This allows the Android app to trigger the above method and receive offline silent push notifications when it is terminated in the background.
Untitled
ZPNs.setBackgroundMessageHandler(_zpnsMessagingBackgroundHandler);
1
Copied!

By completing the above steps, you will be able to receive silent push notifications from others.

Previous

Cross application offline push

Next

Custom notification icon