package com.aiosman.ravenow

import android.content.Context
import android.content.Intent
import android.util.Log
import cn.jpush.android.api.NotificationMessage
import cn.jpush.android.service.JPushMessageReceiver
import com.google.gson.Gson
import com.google.gson.annotations.SerializedName

data class ActionExtra(
    @SerializedName("action")
    val action: String,
    @SerializedName("postId")
    val postId: String?,
    @SerializedName("commentId")
    val commentId: String?
)

class JpushReciver : JPushMessageReceiver() {
    val gson = Gson()
    override fun onInAppMessageClick(p0: Context?, p1: NotificationMessage?) {
        super.onInAppMessageClick(p0, p1)
        // 打开自定义的页面
        Log.d("JpushReciver", "onInAppMessageClick")
    }

    override fun onNotifyMessageOpened(context: Context?, message: NotificationMessage) {
        super.onNotifyMessageOpened(context, message)
        // 打开自定义的页面
        Log.d("JpushReciver", "onNotifyMessageOpened")
        val actionExtra = message.notificationExtras?.let {
            gson.fromJson(it, ActionExtra::class.java)
        }
        val intent = Intent(context, MainActivity::class.java).apply {
            flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
        }
        actionExtra?.postId?.let {
            intent.putExtra("POST_ID", it)
        }
        actionExtra?.commentId?.let {
            intent.putExtra("COMMENT_ID", it)
        }
        actionExtra?.action?.let {
            intent.putExtra("ACTION", it)
        }
        context?.startActivity(intent)
    }
}