Google push channel integration

更新时间:
复制 MD 格式

This topic describes how to integrate the Google push channel.

Preparations

  • You have integrated the Mobile Push software development kit (SDK) as described in Android SDK integration.

  • You have created a project in Firebase. If not, log on to the Firebase console to create a project. After the project is created, go to the project, add an app, and download the google-services.json file for the app. Record the values of the `project_number`, `mobilesdk_app_id`, `project_id`, and `current_key` keys from the google-services.json file. These values correspond to `sendId`, `applicationId`, `projectId`, and `apiKey`.

  • In the Firebase console, go to Project settings and select Service accounts. Generate and download a private key file and store it securely.

  • You have uploaded the Firebase Cloud Messaging (FCM) service account key JSON file to the Enterprise Mobile Application Studio (EMAS) console. If not, log on to the EMAS Mobile Push console and upload the FCM service account key JSON file that you generated and downloaded from the Firebase console. For more information, see Configure third-party channel keys.

Step 1: Add the SDK to your application

1.1 Configure the Maven repository

The following sections describe two ways to configure the repository. The dependencyResolutionManagement method is recommended for Gradle 7.0 and later. The allprojects method is recommended for versions earlier than Gradle 7.0.

1.1.1 dependencyResolutionManagement method

In your root-level Gradle file (<project>/settings.gradle), add the Maven repository address to the repositories block within the dependencyResolutionManagement block.

dependencyResolutionManagement {
  repositories {
    google()
  }
}

1.1.2 allprojects method

In your root-level Gradle file (<project>/build.gradle), add the Maven repository address to the repositories block within the allprojects block.

allprojects {
  repositories {
    google()
  }
}

1.2 Add SDK dependencies

In your module-level Gradle file (usually <project>/<app-module>/build.gradle), add the SDK dependencies to the dependencies block.

Important
dependencies {
    implementation 'com.aliyun.ams:alicloud-android-third-push:x.x.x'
    implementation 'com.aliyun.ams:alicloud-android-third-push-fcm:x.x.x'
}
Note

The Mobile Push third-party channel SDK only supports Maven dependencies.

Step 2: Configure and use the SDK

1. SDK initialization

Important
  • Before integration, ensure Google Play Services is installed on the mobile device. Otherwise, registration will fail. Google services have been removed from most Android phones in the Chinese mainland.

  • Initialize the SDK in the Application class after you initialize the Mobile Push SDK. If you do not follow this order, third-party channel registration may fail.

  • If you are upgrading from a Mobile Push SDK version earlier than 3.7.7, update the `sendId`, `applicationId`, `projectId`, and `apiKey` parameters in your code. For more information, see Preparations.

Initialize the SDK as follows:

// Register the GCM/FCM channel.
// For sendId, applicationId, projectId, and apiKey, see the information in the Preparations section.
GcmRegister.register(applicationContext, sendId, applicationId, projectId, apiKey);
// Register the GCM/FCM channel.
// For sendId, applicationId, projectId, and apiKey, see the information in the Preparations section.
GcmRegister.register(applicationContext, sendId, applicationId, projectId, apiKey); 
Note

The registration method does not automatically check whether the device supports Google system push. You must perform this check manually. Alternatively, you can execute this method only if the initialization for all other third-party channels returns false.

2. Obfuscation configuration

If you enabled code obfuscation for your project, add the following ProGuard configuration for the third-party channel.

# GCM/FCM channel
-keep class com.google.firebase.**{*;}
-dontwarn com.google.firebase.**

Step 3: Verify the integration

1. Enable SDK logs

val pushService = PushServiceFactory.getCloudPushService()
// This line is for debug packages only. It is not needed for release packages.
pushService.setLogLevel(CloudPushService.LOG_DEBUG)
CloudPushService pushService = PushServiceFactory.getCloudPushService();
// This line is for debug packages only. It is not needed for release packages.
pushService.setLogLevel(CloudPushService.LOG_DEBUG);      

2 Method to confirm success

When the GCM/FCM channel is successfully initialized, the following logs appear:

05-19 19:18:44.530 19153-19177/com.xxx D/MPS:GcmRegister: fcm token is eWIXLYCNP0Q:APA91bFUAgxj6XYf5okyoCBnRPw1UwITndzXrvPDgbdI2N44PYm17hFEBiNXNQJrJ8bOG_xjw3c3UPDAhzNMTLNjlAKcjUanKyLA6E3k4wEmgZuhgUT02UMmMvH2LVA1L2Z4-l-cT_Ug

Message from the GCM/FCM channel:

05-19 19:20:04.900 19153-20391/com.alibaba.push2 D/MPS:GcmRegister: onReceiveMessage payload msg:[......]

What to do next

You can now send push notifications to the notification bar on mobile devices from the EMAS console using third-party channels. To handle the logic that runs after a user taps a notification, you must use an auxiliary pop-up window to receive the push data. For more information, see Auxiliary pop-up window integration.

Important
  • When you use Mobile Push to send notifications through a third-party channel from the Mobile Push console or by calling an OpenAPI operation, you must configure the server-side parameters as described in the auxiliary pop-up window document. If you do not set the server-side parameters, notifications are not sent to the third-party channel.

GCM/FCM scenarios

Using the native Android system

  • During normal use, the GCM/FCM channel can deliver push notifications whether the application is running in the foreground, running in the background, or closed from the multitasking manager (which kills the process).

  • If you force-stop the application from Settings > Application Management, the application cannot start automatically. As a result, the application cannot receive push notifications. The following log message indicates that the system cannot wake up a force-stopped application.

    GCM: broadcast intent callback: result=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE flg=0x10000000 pkg=com.aliyun.emas.pocdemo (has extras) }
  • When Battery optimization is enabled, you can configure whether the application is allowed to run in the background. If background activity is allowed, the push channel is not affected, and the application can receive push notifications even if its process is killed. If background activity is not allowed, killing the process is treated by the system as a force-stop, and the application may not receive push notifications.

Using a device manufacturer's Android system

Some manufacturers in the Chinese mainland customize the Android system. On these systems, killing a process is treated as force-stopping the application. This prevents messages from being delivered through the GCM/FCM channel. To improve the push notification delivery rate after an application's process is killed, integrate the specific manufacturer's push channel, such as the channels for Huawei or Xiaomi devices.

Vendor Channel FAQ

Troubleshooting steps for Android third-party channels and pop-up windows

Conflicts between the Android third-party channel SDK and other vendor SDKs

Tapping a push notification from an Android third-party channel does not open the corresponding Activity

An error occurs when parsing the body during the startup of an auxiliary pop-up window on an Android device

"register not in main process, return" is displayed after integrating the Mobile Push third-party channel