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 {
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("报告")
.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()
bundle.putString(JPushInterface.EXTRA_ALERT, message?.notificationExtras)
intent.putExtras(bundle)
context?.startActivity(intent)
val notificationExtras =
intent.getStringExtra(JPushInterface.EXTRA_ALERT)
val jsonObject = JSONObject(notificationExtras)
name = jsonObject.get("name").toString()
LogUtil.e(name)