极光推送点击推送过来的消息跳转新的页面,返回以后重启APP

1.需要在继承JPushMessageReceiver的服务类里修改onNotifyMessageOpened方法里的代码块,自己定义一个通知

class MyService : JPushMessageReceiver() {
    override fun onAliasOperatorResult(p0: Context?, p1: JPushMessage?) {
        super.onAliasOperatorResult(p0, p1)
        LogUtil.e(p1.toString())
    }

    override fun onMessage(p0: Context?, p1: CustomMessage?) {
        super.onMessage(p0, p1)

    }

    override fun onNotifyMessageOpened(context: Context?, message: NotificationMessage?) {
        LogUtil.e(message.toString())
        try {
            //打开自定义的Activity
            val intent = Intent(context, NotificationReportDetailsActivity::class.java)
            val bundle = Bundle()
            bundle.putString(JPushInterface.EXTRA_ALERT, message?.notificationExtras)
            intent.putExtras(bundle)
            intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
            val CHANNEL_ID = "my_channel"
            val builder: NotificationCompat.Builder =
                NotificationCompat.Builder(MyApp.context, CHANNEL_ID)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentTitle("报告")
                    .setContentText("报告") //以下为关键的2行
                    .setPriority(NotificationCompat.PRIORITY_HIGH)
                    .setCategory(NotificationCompat.CATEGORY_CALL)
            val notifyManager =
                context?.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            notifyManager.notify(0, builder.build())

            context?.startActivity(intent)
        } catch (throwable: Throwable) {
        }

    }

    override fun onMultiActionClicked(p0: Context?, p1: Intent?) {
        super.onMultiActionClicked(p0, p1)

    }

    override fun onNotificationSettingsCheck(p0: Context?, p1: Boolean, p2: Int) {
        super.onNotificationSettingsCheck(p0, p1, p2)

    }
}

2.修改MyService的清单文件配置

需要填入自己的包名(xxxx.xxx.xxx)
<receiver
            android:name="xxx.xxx.xxx.service.MyService"
            android:enabled="true"
            android:exported="false" >
            <intent-filter>
                <action android:name="cn.jpush.android.intent.RECEIVE_MESSAGE" />
                <action android:name="cn.jpush.android.intent.REGISTRATION" />
                <action android:name="cn.jpush.android.intent.MESSAGE_RECEIVED" />
                <action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED" />
                <action android:name="cn.jpush.android.intent.NOTIFICATION_OPENED" />
                <action android:name="cn.jpush.android.intent.NOTIFICATION_CLICK_ACTION" />
                <action android:name="cn.jpush.android.intent.CONNECTION" />
                <category android:name="xxx.xxx.xxx" />
            </intent-filter>
        </receiver>

3.发送和接收后台推送过来的数据

//可以参考上边第一个代码块
val intent = Intent(context, NotificationReportDetailsActivity::class.java)
val bundle = Bundle()
//notificationExtras存放的后台发送过来的数据,json串格式
bundle.putString(JPushInterface.EXTRA_ALERT, message?.notificationExtras)
intent.putExtras(bundle)
context?.startActivity(intent)

val notificationExtras = 
intent.getStringExtra(JPushInterface.EXTRA_ALERT)
//因为后台发送过的数据是json串的格式,所以这里需要解析json串,取到自己想要的数据
val jsonObject = JSONObject(notificationExtras)
name = jsonObject.get("name").toString()
LogUtil.e(name)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值