小记接入阿里云推送

Posted dzqdzq

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了小记接入阿里云推送相关的知识,希望对你有一定的参考价值。

搞这个sdk居然花了3天时间. 无语.....

 

有几个注意的点,免得以后接入的时候再次躺坑

1, 一定要接入厂商通道,

2, 一定要实现MainApplication

3, 一定要在MainApplication这样初始化,

public void onCreate() {
        super.onCreate();
        initCloudChannel(this);
        MiPushRegister.register(this, "x x x x xxxxx", "5721838183847");

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            // 通知渠道的id
            String id = "2";
            String name = "比赛";
            String description = "开赛提醒";
            int importance = NotificationManager.IMPORTANCE_HIGH;
            NotificationChannel mChannel = new NotificationChannel(id, name, importance);
            // 配置通知渠道的属性
            mChannel.setDescription(description);
            // 设置通知出现时的闪灯(如果 android 设备支持的话)
            mChannel.enableLights(true);
            mChannel.setLightColor(Color.RED);
            // 设置通知出现时的震动(如果 android 设备支持的话)
            mChannel.enableVibration(true);
            mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
            //最后在notificationmanager中创建该通知渠道
            mNotificationManager.createNotificationChannel(mChannel);
        }
    }

 4, 一定要实现: PopupPushActivity, MyMessageReceiver

 

技术图片

 

package com.bjcardsports.halo.aliyun;

import android.os.Bundle;
import android.util.Log;

import com.alibaba.sdk.android.push.AndroidPopupActivity;

import java.util.Map;

public class PopupPushActivity extends AndroidPopupActivity {
    static final String TAG = "PopupPushActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void onSysNoticeOpened(String title, String summary, Map<String, String> extMap) {
        Log.d("OnMiPushSysNoticeOpened", "title: " + title + ", content: " + summary + ", extMap: " + extMap);
    }
}

 

package com.bjcardsports.halo.aliyun;

import android.content.Context;
import android.util.Log;

import com.alibaba.sdk.android.push.MessageReceiver;
import com.alibaba.sdk.android.push.notification.CPushMessage;

import java.util.Map;

public class MyMessageReceiver extends MessageReceiver {
    // 消息接收部分的LOG_TAG
    public static final String REC_TAG = "receiver";
    @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");
    }
}

 5, 后台一定要添加通道:

技术图片

 

6, 用网页测试推送时,一定要全部写

技术图片

 

以上是关于小记接入阿里云推送的主要内容,如果未能解决你的问题,请参考以下文章

公安备案接入服务商如何填写?(网站接入信息)

Python对阿里云物联网MQTT设备接入端开发

Python对阿里云物联网MQTT设备接入端开发

Python对阿里云物联网MQTT设备接入端开发

Python对阿里云物联网MQTT设备接入端开发

阿里云ECS部署Grafana接入zabbix