package com.za.service.jpush

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.os.Handler
import android.os.Looper
import cn.jpush.android.api.JPushInterface
import com.za.common.GlobalData
import com.za.common.log.LogUtil
import com.za.service.ServiceManager

/**
 * Created by zhangj on 2019/4/4.
 */
class JPushReceiver : BroadcastReceiver() {
	override fun onReceive(context : Context, intent : Intent) {
		val bundle = intent.extras
		LogUtil.print("JpushMessage ", "onReceive==" + "action:" + intent.action)
		if (intent.action == null || bundle == null) {
			return
		}
		when (intent.action) {
			JPushInterface.ACTION_REGISTRATION_ID -> {
				val regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID)
				GlobalData.regid = regId
				LogUtil.print("JpushMessage ", "Registration successful: $regId")
			}

			JPushInterface.ACTION_MESSAGE_RECEIVED -> {
				val msg = bundle.getString(JPushInterface.EXTRA_MESSAGE)
				if (msg.isNullOrBlank()) {
					return
				}

				Handler(Looper.getMainLooper()).post {
					LogUtil.print("JpushMessage ", "Received message: $msg")
					ServiceManager.handlerPushMsg(msg)
				}
			}

			JPushInterface.ACTION_NOTIFICATION_RECEIVED -> {
				val title = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE)
				LogUtil.print("JPushReceiver:", "ACTION_NOTIFICATION_RECEIVED title: $title")
			}

			else -> {}
		}
	}
}
