package com.zhangxin.starlight.receiver;

import android.os.Bundle;
import android.text.TextUtils;

import com.huawei.android.hms.tpns.HWHmsMessageService;
import com.huawei.hms.push.RemoteMessage;
import com.zhangxin.starlight.entity.HWPushBean;

import org.greenrobot.eventbus.EventBus;

/**
 * Created by lidandan
 * on 2022/1/13
 * Description：
 */
public class HWMessageService extends HWHmsMessageService {

    @Override
    public void onNewToken(String token, Bundle bundle) {
        // 判断token是否为空
        if (!TextUtils.isEmpty(token)) {
            refreshedTokenToServer(token);
        }
    }

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
        if (remoteMessage != null) {
//            Intent intent = new Intent(this, MainActivity.class);
//            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

//            Intent intent = new Intent(Intent.ACTION_VIEW);
//            // Scheme协议（pushscheme://com.huawei.codelabpush/deeplink?）需要开发者自定义
//            intent.setData(Uri.parse("pushscheme://com.huawei.codelabpush/deeplink?"));
//            // 往intent中添加参数，用户可以根据自己的需求进行添加参数：
//            intent.putExtra("title", remoteMessage.getData());
//            intent.putExtra("senderId", remoteMessage.getSendMode()+"");
//            // 必须带上该Flag
//            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//            String intentUri = intent.toUri(Intent.URI_INTENT_SCHEME);
//            // 打印出的intentUri值就是设置到推送消息中intent字段的值
//            Log.d("intentUri", intentUri);
//            startActivity(intent);
        }
    }

    private void refreshedTokenToServer(String token) {
        EventBus.getDefault().post(new HWPushBean(token));
    }
}
