Jpush的简单使用记录

本文详细记录了Jpush在Android应用中的集成步骤,包括在project和module的build.gradle中添加依赖,配置清单文件,设置广播接收器,以及确保包名、appkey的准确性及网络通畅以接收通知。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

Jpush的简单使用记录

一、采用jcenter自动集成方式:

1.1、project的build.gradle中引入仓库地址jcenter()

1.2、module的build.gradle中增加配置如下(遇到问题可见官网FAQ,可能是ndk的版本问题吧,反正问题不大):

1.3、清单文件的配置(加上自动以的广播接收者来接收极光推送的通知等信息状态)

1.4、代码块:

最后需要注意的是:通过Portal主动下发通知,客户端接收到的条件是:包名一定要一致、appkey正确,网络必须要通畅,具体可见官网


Jpush的简单使用记录

一、采用jcenter自动集成方式:

1.1、project的build.gradle中引入仓库地址jcenter()

1.2、module的build.gradle中增加配置如下(遇到问题可见官网FAQ,可能是ndk的版本问题吧,反正问题不大):

 defaultConfig {
  //Jpush自动配置所需
        ndk {
            //选择要添加的对应 cpu 类型的 .so 库。
            abiFilters 'armeabi', 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64'
            // 还可以添加 'x86', 'x86_64', 'mips', 'mips64'
        }
        manifestPlaceholders = [
                JPUSH_PKGNAME: applicationId,
                JPUSH_APPKEY : "139dc8f154e5807d0448e180", //JPush 上注册的包名对应的 Appkey.
                JPUSH_CHANNEL: "developer-default", //暂时填写默认值即可.
        ]
}

dependencies { 
 //Jpush自动配置依赖
    implementation 'cn.jiguang.sdk:jpush:3.2.0'  // 此处以JPush 3.2.0 版本为例。
    implementation 'cn.jiguang.sdk:jcore:1.2.7'  // 此处以JCore 1.2.7 版本为例。
}

1.3、清单文件的配置(加上自动以的广播接收者来接收极光推送的通知等信息状态)

    <!--采用Jcenter方式自动集成,不需要再清单Manifest中配置-->
    <!-- Jpush Required -->
    <permission
        android:name="com.zbv.testJpush.permission.JPUSH_MESSAGE"
        android:protectionLevel="signature" />
    <!-- Jpush Required -->
    <uses-permission android:name="com.zbv.testJpush.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.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.MOUNT_UNMOUNT_FILESYSTEMS" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_SETTINGS" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

    <!--极光自定义接收广播接收者-->
        <receiver
            android:name=".MyReceiver"
            android:enabled="true"
            android:exported="false">
            <intent-filter>
                <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="com.zbv.testJpush" />
            </intent-filter>
        </receiver>

1.4、代码块:

package com.zbv.testJpush

import android.app.Application
import cn.jpush.android.api.JPushInterface

/**
 * author: qzx
 * Date: 2019/4/8 10:20
 * 在Application中初始化极光推送
 */
class MyApplication : Application() {

    override fun onCreate() {
        super.onCreate()
        //开发阶段可开启极光的日志输出
        JPushInterface.setDebugMode(true)
        JPushInterface.init(this)
    }
}
package com.zbv.testJpush

import android.app.NotificationManager
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.util.Log
import cn.jpush.android.api.JPushInterface

/**
 * author: qzx
 * Date: 2019/4/8 10:53
 * 接收者接收极光推送的回调信息
 */
class MyReceiver : BroadcastReceiver() {

    private var notificationManager: NotificationManager? = null

    companion object {
        const val TAG = "zbv"
    }

    override fun onReceive(context: Context?, intent: Intent?) {
        if (notificationManager == null) {
            notificationManager = context?.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        }

        if (intent != null) {
            val bundle: Bundle = intent.extras

            when (intent.action) {
                JPushInterface.ACTION_REGISTRATION_ID -> {
                    Log.d(TAG, "JPush 用户注册成功")
                }
                JPushInterface.ACTION_MESSAGE_RECEIVED -> {
                    Log.d(TAG, "接受到推送下来的自定义消息")
                }
                JPushInterface.ACTION_NOTIFICATION_RECEIVED -> {
                    Log.d(TAG, "接受到推送下来的通知")

                    receivingNotification(context, bundle)
                }
                JPushInterface.ACTION_NOTIFICATION_OPENED -> {
                    Log.d(TAG, "用户点击打开了通知")

                    openNotification(context, bundle)
                }
                else -> {
                    Log.d(TAG, "Unhandled intent - " + intent.action)
                }
            }
        }
    }

    private fun receivingNotification(context: Context?, bundle: Bundle) {

        val title = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE)
        val message = bundle.getString(JPushInterface.EXTRA_ALERT)
        val extra = bundle.getString(JPushInterface.EXTRA_EXTRA)

        Log.d(TAG, "title=$title;message=$message;extra=$extra")

    }

    private fun openNotification(context: Context?, bundle: Bundle) {

    }
}
//说明下面三个方法皆是按钮的点击事件,用于在之后修改客户端接收到通知显示的样式

//带按钮的通知栏样式    
fun setAddActionsStyle(view: View) {
        val builder: MultiActionsNotificationBuilder = MultiActionsNotificationBuilder(this)
        builder.addJPushAction(R.drawable.jpush_ic_richpush_actionbar_back, "first", "my_extra1")
        builder.addJPushAction(R.drawable.jpush_ic_richpush_actionbar_back, "second", "my_extra2")
        builder.addJPushAction(R.drawable.jpush_ic_richpush_actionbar_back, "third", "my_extra3")
        JPushInterface.setPushNotificationBuilder(10, builder)
    }

//基础的改变通知栏样式
    fun setStyleBasic(view: View) {
        val builder: BasicPushNotificationBuilder = BasicPushNotificationBuilder(this)
        builder.statusBarDrawable = R.mipmap.ic_launcher
        builder.notificationFlags = Notification.FLAG_AUTO_CANCEL  //设置为点击后自动消失
        builder.notificationDefaults = Notification.DEFAULT_SOUND  //设置为铃声( Notification.DEFAULT_SOUND)或者震动( Notification.DEFAULT_VIBRATE)
        JPushInterface.setPushNotificationBuilder(1, builder)
    }

//自定义通知栏样式
    fun setStyleCustom(view: View) {
        val builder: CustomPushNotificationBuilder = CustomPushNotificationBuilder(this,
                R.layout.customer_notitfication_layout, R.id.icon, R.id.title, R.id.text)
        builder.layoutIconDrawable = R.mipmap.ic_launcher
        builder.developerArg0 = "developerArg2"
        JPushInterface.setPushNotificationBuilder(2, builder)
    }

最后需要注意的是:通过Portal主动下发通知,客户端接收到的条件是:包名一定要一致、appkey正确,网络必须要通畅,具体可见官网

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值