简单配置
1.在app的build下的配置
ndk
ndk {
//选择要添加的对应 cpu 类型的 .so 库。
abiFilters 'armeabi', 'armeabi-v7a', 'arm64-v8a'
// 还可以添加 'x86', 'x86_64', 'mips', 'mips64'
}
manifestPlaceholders = [
JPUSH_PKGNAME : applicationId,
JPUSH_APPKEY : "d33eeb7a3e6afb56deacece9", //极光开发平台上注册的包名对应的appkey.
JPUSH_CHANNEL : "developer-default", //暂时填写默认值即可.
]
依赖
compile 'cn.jiguang.sdk:jpush:3.8.5' // 此处以JPush 3.8.5 版本为例。
compile 'cn.jiguang.sdk:jcore:2.5.5' // 此处以JCore 2.5.5 版本为例。
2.在清单文件下的配置
权限
<!-- 自己的包名 -->
<permission
android:name="com.example.testjiguang"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.jpushdemo.permission.JPUSH_MESSAGE" />
<uses-permission android:name="android.permission.RECEIVE_USER_PRESENT" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <!-- Optional. Required for location feature -->
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> <!-- 用于开启 debug 版本的应用在6.0 系统上 层叠窗口权限 -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.GET_TASKS" />
<application
android:name=".JPushApplication"//记得注册!!!!!!!!!!!!!!!!!!!!!!!!!!!!
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
//这Service是在手机平台上推送时用的,我做平板不需要
<!-- Since JCore2.0.0 Required SDK核心功能-->
<!-- 可配置android:process参数将Service放在其他进程中;android:enabled属性不能是false -->
<!-- 这个是自定义Service,要继承极光JCommonService,可以在更多手机平台上使得推送通道保持的更稳定 -->
<!-- <service android:name=".JPushService"-->
<!-- android:enabled="true"-->
<!-- android:exported="false"-->
<!-- android:process=":pushcore">-->
<!-- <intent-filter>-->
<!-- <action android:name="cn.jiguang.user.service.action" />-->
<!-- </intent-filter>-->
<!-- </service>-->
<!-- Required since 3.0.7 -->
<!-- 新的 tag/alias 接口结果返回需要开发者配置一个自定的广播 -->
<!-- 3.3.0开始所有事件将通过该类回调 -->
<!-- 该广播需要继承 JPush 提供的 JPushMessageReceiver 类, 并如下新增一个 Intent-Filter -->
<!-- 替换原生极光推送接收器 -->
<receiver
android:name=".JPushReceiver"
android:enabled="true"
android:exported="false"
tools:node="replace">
<intent-filter>
<action android:name="cn.jpush.android.intent.RECEIVE_MESSAGE" />
<category android:name="com.example.testjiguang" />
</intent-filter>
</receiver>
</application>
代码
Application
初始化
class JPushApplication : Application() {
override fun onCreate() {
super.onCreate()
JPushInterface.setDebugMode(true)//打开log
JPushInterface.init(this)
}
}
Receiver
对推送进行处理
class JPushReceiver : JPushMessageReceiver() {
/**
* TODO 连接极光服务器
*/
override fun onConnected(p0: Context?, p1: Boolean) {
super.onConnected(p0, p1)
Log.d("连接极光服务器", "onConnected")
}
/**
* TODO 注册极光时的回调
*/
override fun onRegister(p0: Context?, p1: String?) {
super.onRegister(p0, p1)
Log.d("注册极光时的回调", "onRegister$p1")
}
/**
* TODO 注册以及解注册别名时回调
*/
override fun onAliasOperatorResult(p0: Context?, p1: JPushMessage?) {
super.onAliasOperatorResult(p0, p1)
Log.e("注册以及解除注册别名时回调", p1.toString())
}
/**
* TODO 接收到推送下来的通知
* 可以利用附加字段(notificationMessage.notificationExtras)来区别Notication,指定不同的动作,附加字段是个json字符串
* 通知(Notification),指在手机的通知栏(状态栏)上会显示的一条通知信息
*/
override fun onNotifyMessageArrived(
context: Context?,
notificationMessage: NotificationMessage
) {
super.onNotifyMessageArrived(context, notificationMessage)
Log.e("接收到推送下来的通知", notificationMessage.toString())
}
/**
* TODO 打开了通知
* notificationMessage.notificationExtras(附加字段)的内容处理代码
* 比如打开新的Activity, 打开一个网页等..
*/
override fun onNotifyMessageOpened(
context: Context,
notificationMessage: NotificationMessage
) {
super.onNotifyMessageOpened(context, notificationMessage)
val intent = Intent(context, MainActivity::class.java)
context.startActivity(intent)
Log.e("打开了通知", notificationMessage.notificationExtras)
}
/**
* TODO 接收到推送下来的自定义消息
* 自定义消息不是通知,默认不会被SDK展示到通知栏上,极光推送仅负责透传给SDK。其内容和展示形式完全由开发者自己定义。
* 自定义消息主要用于应用的内部业务逻辑和特殊展示需求
*/
override fun onMessage(
context: Context?,
customMessage: CustomMessage?
) {
super.onMessage(context, customMessage)
// 收到消息 显示通知
Log.d("接收到推送下来的自定义消息", "onMessage: ")
// processCustomMessage(context, customMessage.extra);
}
//通知
private fun processCustomMessage(
context: Context,
message: String
) {
val channelID = "1"
val channelName = "channel_name"
// 跳转的Activity
val intent = Intent(context, MainActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
val pendingIntent = PendingIntent.getActivity(context, 0, intent, 0)
val notificationManager =
context.getSystemService(NOTIFICATION_SERVICE) as NotificationManager
//适配安卓8.0的消息渠道
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel =
NotificationChannel(channelID, channelName, NotificationManager.IMPORTANCE_HIGH)
notificationManager.createNotificationChannel(channel)
}
val notification =
NotificationCompat.Builder(context, channelID)
notification.setAutoCancel(true)
.setContentText(message)
.setContentTitle("我是Title")
.setSmallIcon(R.drawable.ic_lock_idle_alarm)
.setDefaults(Notification.DEFAULT_ALL)
.setContentIntent(pendingIntent)
notificationManager.notify(
(System.currentTimeMillis() / 1000).toInt(),
notification.build()
)
}
}
网友评论