Android友盟推送接入踩坑记录

SDK初始化

务必在工程的自定义Application类的onCreate()方法中做SDK代码初始化工作

//友盟推送初始化
//UMConfigure.init(this, "Appkey", "Umeng", UMConfigure.DEVICE_TYPE_PHONE, "Umeng Message Secret");
UMConfigure.init(context, "******", "Umeng", UMConfigure.DEVICE_TYPE_PHONE, "******");

推送消息接收

自定义消息处理Handler

mPushAgent.setMessageHandler(MyUmengMessageHandler.getInstance(context));
public class MyUmengMessageHandler extends UmengMessageHandler {
    private static final String TAG = "yezhou";
    private static MyUmengMessageHandler umengMessageHandler;
    private Context context;
    private Handler handler;

    private MyUmengMessageHandler(Context context) {
        this.context = context;
        handler = new Handler(context.getMainLooper());
    }

    public static MyUmengMessageHandler getInstance(Context context) {
        synchronized (MyUmengMessageHandler.class) {
            if (umengMessageHandler == null) {
                synchronized (MyUmengMessageHandler.class) {
                    umengMessageHandler = new MyUmengMessageHandler(context);
                }
            }
        }
        return umengMessageHandler;
    }

    /**
     * 自定义消息的回调方法
     */
    @Override
    public void dealWithCustomMessage(final Context context, final UMessage msg) {
        NLog.i(TAG, "dealWithCustomMessage: " + msg.message_id);
        handler.post(new Runnable() {
            @Override
            public void run() {
                // 对自定义消息的处理方式,点击或者忽略
                boolean isClickOrDismissed = true;
                if (isClickOrDismissed) {
                    //自定义消息的点击统计
                    UTrack.getInstance(context).trackMsgClick(msg);
                    NLog.i(TAG, "UmengMessageHandler");
                } else {
                    //自定义消息的忽略统计
                    UTrack.getInstance(context).trackMsgDismissed(msg);
                }
                Toast.makeText(context, msg.custom, Toast.LENGTH_LONG).show();
            }
        });
    }

    /**
     * 自定义通知栏样式的回调方法
     */
    @Override
    public Notification getNotification(Context context, UMessage msg) {
        NLog.i(TAG, "builder_id: " + msg.builder_id);
        switch (msg.builder_id) {
            case 1:
                Notification.Builder builder = new Notification.Builder(context);
                RemoteViews myNotificationView = new RemoteViews(context.getPackageName(), R.layout.layout_notification_view);
                myNotificationView.setTextViewText(R.id.notification_title, msg.title);
                myNotificationView.setTextViewText(R.id.notification_text, msg.text);
                myNotificationView.setImageViewBitmap(R.id.notification_large_icon, getLargeIcon(context, msg));
                myNotificationView.setImageViewResource(R.id.notification_small_icon, getSmallIconId(context, msg));
                builder.setContent(myNotificationView)
                        .setSmallIcon(getSmallIconId(context, msg))
                        .setTicker(msg.ticker)
                        .setAutoCancel(true);

                return builder.getNotification();
            default:
                //默认为0,若填写的builder_id并不存在,也使用默认。
                return super.getNotification(context, msg);
        }
    }
}

自定义消息接收服务

mPushAgent.setPushIntentServiceClass(PushService.class);
public class PushService extends UmengMessageService {

    @Override
    public void onCreate() {
        super.onCreate();
        NLog.i(Constants.TAG, "PushService.onCreate");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        NLog.i(Constants.TAG, "PushService.onStartCommand");
        return super.onStartCommand(intent, flags, startId);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onMessage(Context context, Intent intent) {
        String body = intent.getStringExtra(AgooConstants.MESSAGE_BODY);
        NLog.i(Constants.TAG, "PushService.onMessage: " + body); //消息体
        if (TextUtils.isEmpty(body))
            return;
        UMessage msg;
        try {
            msg = new UMessage(new JSONObject(body));
            UTrack.getInstance(this).trackMsgClick(msg);
            Map<String, String> extra = msg.extra;
            NLog.d(Constants.TAG, "custom: " + msg.custom); //自定义消息的内容
            NLog.d(Constants.TAG, "title: " + msg.title); //通知标题
            NLog.d(Constants.TAG, "text: " + msg.text); //通知内容
            if (extra != null) {
                //
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        NLog.i(Constants.TAG, "PushService.onDestroy");
    }
}

自定义行为的回调处理

/**
 * 自定义行为的回调处理,参考文档:高级功能-通知的展示及提醒-自定义通知打开动作
 * UmengNotificationClickHandler是在BroadcastReceiver中被调用,故
 * 如果需启动Activity,需添加Intent.FLAG_ACTIVITY_NEW_TASK
 * */
UmengNotificationClickHandler notificationClickHandler = new UmengNotificationClickHandler() {
    @Override
    public void dealWithCustomAction(Context context, UMessage msg) {
        //Toast.makeText(context, msg.custom, Toast.LENGTH_LONG).show();
        NLog.i(Constants.TAG, "UmengNotificationClickHandler: " + msg.message_id + msg.extra.get("cpf"));

        //Intent intent = new Intent(MainApplication.this, MainActivity.class);
        //intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        Map<String, String> extra = msg.extra;

    }
};
//使用自定义的NotificationHandler,来结合友盟统计处理消息通知
//CustomNotificationHandler notificationClickHandler = new CustomNotificationHandler();
mPushAgent.setNotificationClickHandler(notificationClickHandler);

版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/03/18/android-access-umeng-push-record/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
打赏
海报
Android友盟推送接入踩坑记录
SDK初始化 务必在工程的自定义Application类的onCreate()方法中做SDK代码初始化工作 //友盟推送初始化 //UMConfigure.init(this, "Appkey", "……
<<上一篇
下一篇>>
文章目录
关闭
目 录