Mobile Push SDK

更新时间:
复制 MD 格式

Alibaba Cloud Mobile Push is a smart, big data-powered mobile push service that helps you quickly add push features to your application. The service delivers efficient, precise, and real-time mobile pushes to help reduce development costs and improve user activity and application retention rates.

Initialization

For more information, see SDK initialization.

Usage

  1. Register and start Mobile Push in the application. This operation is included in the SDK initialization, so no extra steps are required.

  2. Create a message receiver that inherits from com.alibaba.sdk.android.push.MessageReceiver. Add your business logic to the corresponding callback as shown in the following code.

    public class MyMessageReceiver extends MessageReceiver {
        // LOG_TAG for the message receiving part
        public static final String REC_TAG = "receiver";
        @Override
        public void onNotification(Context context, String title, String summary, Map<String, String> extraMap) {
            // TODO: Process the push notification
            Log.e("MyMessageReceiver", "Receive notification, title: " + title + ", summary: " + summary + ", extraMap: " + extraMap);
        }
        @Override
        public void onMessage(Context context, CPushMessage cPushMessage) {
                Log.e("MyMessageReceiver", "onMessage, messageId: " + cPushMessage.getMessageId() + ", title: " + cPushMessage.getTitle() + ", content:" + cPushMessage.getContent());
        }
        @Override
        public void onNotificationOpened(Context context, String title, String summary, String extraMap) {
            Log.e("MyMessageReceiver", "onNotificationOpened, title: " + title + ", summary: " + summary + ", extraMap:" + extraMap);
        }
        @Override
        protected void onNotificationClickedWithNoAction(Context context, String title, String summary, String extraMap) {
            Log.e("MyMessageReceiver", "onNotificationClickedWithNoAction, title: " + title + ", summary: " + summary + ", extraMap:" + extraMap);
        }
        @Override
        protected void onNotificationReceivedInApp(Context context, String title, String summary, Map<String, String> extraMap, int openType, String openActivity, String openUrl) {
            Log.e("MyMessageReceiver", "onNotificationReceivedInApp, title: " + title + ", summary: " + summary + ", extraMap:" + extraMap + ", openType:" + openType + ", openActivity:" + openActivity + ", openUrl:" + openUrl);
        }
        @Override
        protected void onNotificationRemoved(Context context, String messageId) {
            Log.e("MyMessageReceiver", "onNotificationRemoved");
        }
    }
  3. Add the receiver to the AndroidManifest.xml file.

    <!-- Message receiver listener (can be extended by users) -->
    <receiver
        android:name=".MyMessageReceiver"
        android:exported="false"> <!-- To ensure receiver security, set it to not be exported. To open it to other applications, use android:permission to set limits. -->
        <intent-filter>
            <action android:name="com.alibaba.push2.action.NOTIFICATION_OPENED" />
        </intent-filter>
        <intent-filter>
            <action android:name="com.alibaba.push2.action.NOTIFICATION_REMOVED" />
        </intent-filter>
        <intent-filter>
            <action android:name="com.alibaba.sdk.android.push.RECEIVE" />
        </intent-filter>
    </receiver>
                            

    The SDK initialization code handles the logic for the following actions. No additional operations are required.

    • Associate Mobile Push with an account

      When a user logs on, the app automatically associates Mobile Push with their account. This action uses the /uc/bindPushChannel API service.

    • Disassociate Mobile Push from an account

      When a user logs off, the app automatically disassociates Mobile Push from their account. This action uses the /uc/unbindPushChannel API service.

Alerting feature

The alerting feature depends on user-device binding. This binding is included in the SDK initialization. Therefore, you only need to focus on the business logic for the alerting feature.

Message types

Mobile Push supports two types of messages.

  • Notifications

    The SDK automatically processes notifications sent from the server. It displays a pop-up notification that contains the title and content based on your configuration. You do not need to add business logic in MyMessageReceiver.

  • Message type

    Messages sent from the server are delivered to the corresponding callback method of the MyMessageReceiver class that you created during initialization. To display a pop-up notification in this mode, you must create one from the received content.

Third-party auxiliary push channels

When your application is not running, third-party auxiliary push channels can deliver offline messages to the device. These push channels do not require extra initialization. You only need to request and configure the third-party auxiliary push channels. For more information, see the Mobile Push Developer Guide.

Third-party auxiliary push channels are currently supported on Xiaomi, Huawei, VIVO, and OPPO devices.

  • For third-party pushes on Xiaomi and OPPO devices, configure the parameters during initialization. For more information, see SDK initialization.

  • For third-party pushes on Huawei and VIVO devices, configure the following information in the AndroidManifest.xml file.

    Note

    When you test the Huawei offline push feature, ensure that the signature of the test app is the same as the app signature that you submitted to the Huawei Push console.

    <!-- huawei push start -->
    <meta-data    
        android:name="com.huawei.hms.client.appid"    
        android:value="your huawei push appid" />
    <!-- huawei push end -->
    
    <!-- vivo push start -->
    <meta-data    
        android:name="com.vivo.push.api_key"    
        android:value="your vivo push api_key" />
    <meta-data    
        android:name="com.vivo.push.app_id"    
        android:value="your vivo push app id" />
    <!-- vivo push end -->

Obfuscation configuration

In the proguard-rules.pro file, add the following code to exclude classes and methods from obfuscation.

-keepclasseswithmembernames class ** {
    native <methods>;
}

-keepattributes Signature

-keep class sun.misc.Unsafe { *; }

-keep class com.taobao.** {*;}

-keep class com.alibaba.** {*;}

-keep class com.alipay.** {*;}

-keep class com.ut.** {*;}

-keep class com.ta.** {*;}

-keep class anet.**{*;}

-keep class anetwork.**{*;}

-keep class org.android.spdy.**{*;}

-keep class org.android.agoo.**{*;}

-keep class android.os.**{*;}

-dontwarn com.taobao.**

-dontwarn com.alibaba.**

-dontwarn com.alipay.**

-dontwarn anet.**

-dontwarn org.android.spdy.**

-dontwarn org.android.agoo.**

-dontwarn anetwork.**

-dontwarn com.ut.**

-dontwarn com.ta.**