Alibaba Cloud - Mobile Push Integration and Key Points

    Alibaba Cloud Push was integrated into the original project. Here are some key points to share with you.

 

Introduction:

Alibaba Cloud Mobile Push is a mobile intelligent push service based on big data, which helps apps quickly integrate the mobile push function, and greatly reduces development costs while realizing efficient, accurate, and real-time mobile push. Allow developers to stay connected with users most effectively, thereby increasing user activity and improving application retention.

integrated:

The main here is only the Maven library for quick integration

 

  • Configure the maven library URL in the build.gradle file in the Project root directory:

     

  • Add the corresponding dependencies to the build.gradle file under the corresponding module

     

 

Note: If android Studio shows the following prompt after adding the above abiFilter configuration:

NDK integration is deprecated in the current plugin. Consider trying the new experimental plugin.

 

Then add in the gradle.properties file of the Project root directory:

   android.useDeprecatedNdk=true

 

  • Set appKey, appSecret in AndroidManifest file:

Register call:

1. Register the cloud channel in the Application first.

package com.haocaiqi.voiceannouncements_spd; import android.app.Application; import android.content.Context; import android.util.Log; import com.alibaba.sdk.android.push.CloudPushService; import com.alibaba.sdk.android.push.CommonCallback; import com.alibaba.sdk.android.push.noonesdk.PushServiceFactory; import com.haocaiqi.voiceannouncements_spd.utils.LogUtil; public class MainApplication extends Application {    private static final String TAG = "Init";    @Override    public void onCreate() {        super.onCreate();        initCloudChannel(this);    }    /**     * 初始化云推送通道     * @param applicationContext     */    private void initCloudChannel(Context applicationContext) {        PushServiceFactory.init(applicationContext);        final CloudPushService pushService = PushServiceFactory.getCloudPushService();        pushService.register(applicationContext, new CommonCallback() {            @Override            public void onSuccess(String response) {                Log.d(TAG, "init cloudchannel success");                    //获取设备号                pushService.getDeviceId();                LogUtil.logConsole("初始化成功   "+response+  "   "+pushService.getDeviceId());            }            @Override            public void onFailed(String errorCode, String errorMessage) {                Log.d(TAG, "init cloudchannel failed -- errorcode:" + errorCode + " -- errorMessage:" + errorMessage);                LogUtil.logConsole("初始化失败"+errorMessage);            }        });    } }

2.通过广播接收推送通知或消息

package com.haocaiqi.voiceannouncements_spd.receiver; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Build; import android.support.annotation.RequiresApi; import android.util.Log; import com.alibaba.sdk.android.push.MessageReceiver; import com.alibaba.sdk.android.push.notification.CPushMessage; import com.android.print.sdk.PrinterInstance; import com.android.qzs.voiceannouncementlibrary.VoiceUtils; import com.haocaiqi.voiceannouncements_spd.MainActivity; import com.haocaiqi.voiceannouncements_spd.R; import com.haocaiqi.voiceannouncements_spd.utils.LogUtil; import com.haocaiqi.voiceannouncements_spd.utils.MapUtils; import java.util.Map; /** * Created by qzs on 2017/9/1. */ public class MyMessageReceiver extends MessageReceiver {    // 消息接收部分的LOG_TAG    public static final String REC_TAG = "receiver";    private PrinterInstance mPrinter;    @Override    public void onNotification(Context context, String title, String summary, Map<String, String> extraMap) {        // TODO 处理推送通知        // 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");    } }

要点:

1.移动推送的初始化必须在Application中,不能放到Activity中执行。移动推送在初始化过程中将启动后台进程channel,必须保证应用进程和channel进程都执行到推送初始化代码。

 

2.后端可以通过设备号进行推送,设备号在Application中的获取,如下:

                   pushService.getDeviceId();

也可以绑定一个ID号来推送,如下代码:

PushServiceFactory.getCloudPushService().bindAccount(你要绑定的ID, new CommonCallback() {    @Override    public void onSuccess(String s) {        LogUtil.logConsole("阿里推送绑定成功   "+s+  "   ");    }    @Override    public void onFailed(String s, String s1) {        LogUtil.logConsole("阿里推送绑定失败   "+s+  "     "+s1);    } });

3.如果你绑定了ID,当你不想接收推送消息时必须解绑,下面是解绑的方法:

   PushServiceFactory.getCloudPushService().unbindAccount(new CommonCallback() {                        @Override                        public void onSuccess(String s) { //                Toast.makeText(MainActivity.this, "赞! 阿里云账号绑定成功 ~", //                        Toast.LENGTH_SHORT).show();                                          }                        @Override                        public void onFailed(String s, String s1) { //                Toast.makeText(MainActivity.this, "赞! 阿里云账号解绑失败 ~", //                        Toast.LENGTH_SHORT).show();                                    }                    });

4.如果用户将应用进程手动杀死,不同的ROM对应用进程的权限管理限制不同,部分ROM环境下应用进程可以被拉起响应推送信息,部分机型受限无法被拉起。

5.如果设备是小米或者华为,建议配置小米和华为辅助通道以及小米托管弹窗,可以提升到达率。

6. Set offline message saving when pushing in the background: pushRequest.setStoreOffline(true);//Whether offline messages are saved. If saved, when the push is made, even if the user is not online, the user will receive it the next time he goes online. It will be used in conjunction with the expirationTime parameter.

7. Push notification and push consumption have different methods of receiving callbacks in broadcast.

Recommended reading

 

 

  Deep understanding of ANR

 

  Deep dive into the MV** pattern

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326088121&siteId=291194637