package com.zhangxin.starlight.receiver;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;

import com.xiaomi.mipush.sdk.MiPushCommandMessage;
import com.xiaomi.mipush.sdk.MiPushMessage;
import com.xiaomi.mipush.sdk.PushMessageReceiver;
import com.zhangxin.starlight.activity.MainActivity;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * @Description: java类作用描述
 * @Author: lidandan
 * @CreateDate: 2022/1/10 4:30 下午
 */
public class MessageReceiver extends PushMessageReceiver {

    @Override
    public void onReceiveRegisterResult(Context context, MiPushCommandMessage miPushCommandMessage) {
        super.onReceiveRegisterResult(context, miPushCommandMessage);
    }

    @Override
    public void onNotificationMessageClicked(Context context, MiPushMessage miPushMessage) {
        super.onNotificationMessageClicked(context, miPushMessage);
        Intent intent = new Intent(context, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra("title", miPushMessage.getTitle());
        if (miPushMessage.getExtra() != null) {
            if (miPushMessage.getExtra().containsKey("ext")) {
                String extValue = miPushMessage.getExtra().get("ext");
                if (!TextUtils.isEmpty(extValue)) {
                    try {
                        JSONObject jsonObject = new JSONObject(extValue);
                        String entity = jsonObject.optString("entity");
                        if (!TextUtils.isEmpty(entity)) {
                            JSONObject childJsonObject = new JSONObject(entity);
                            String senderId = childJsonObject.optString("sender");
                            intent.putExtra("senderId", senderId);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

        context.startActivity(intent);
    }
}
