效果圖(這里就簡單實現了別名推送和廣播(所有人)的推送功能):
2、需要下載資源包
3、添加一些重要的類
5、極光官網
build.gradle配置文件入手(需要加入紅框中的代碼,我這里就直接展示我的配置文件):
build.gradle配置文件(Appkey換成自己的):
apply plugin: 'com.android.application' android { compileSdkVersion 29 buildToolsVersion "29.0.0" defaultConfig { applicationId "com.example.test_push" minSdkVersion 24 targetSdkVersion 29 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" manifestPlaceholders = [ JPUSH_PKGNAME: applicationId, JPUSH_APPKEY : "Appkey", //JPush 上注冊的包名對應的 Appkey. JPUSH_CHANNEL: "developer-default", //暫時填寫默認值即可. ] } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } sourceSets { main { jniLibs.srcDirs = ['libs'] } } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'androidx.appcompat:appcompat:1.0.2' implementation 'androidx.constraintlayout:constraintlayout:1.1.3' testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test:runner:1.2.0' androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' }
下載資源包點我獲取(提取碼:ccd4,把資源包放到項目中的相應位置)
添加一些重要的類(這里我就不一一列出代碼了,直接從百度雲上下載,點我下載,提取碼:01bd):
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" package="com.example.test_push"> <!-- Required --> <permission android:name="${applicationId}.permission.JPUSH_MESSAGE" android:protectionLevel="signature" /> <!-- Required --> <uses-permission android:name="${applicationId}.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" tools:ignore="ProtectedPermissions" /> <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.ACCESS_BACKGROUND_LOCATION" /> <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" /> <uses-permission android:name="android.permission.VIBRATE" /> <application 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> <!-- Required SDK 核心功能 --> <!-- 可配置android:process參數將PushService放在其他進程中 --> <service android:name="cn.jpush.android.service.PushService" android:exported="false" android:process=":pushcore"> <intent-filter> <action android:name="cn.jpush.android.intent.REGISTER" /> <action android:name="cn.jpush.android.intent.REPORT" /> <action android:name="cn.jpush.android.intent.PushService" /> <action android:name="cn.jpush.android.intent.PUSH_TIME" /> </intent-filter> </service> <!-- Required SDK核心功能 --> <activity android:name="cn.jpush.android.ui.PushActivity" android:configChanges="orientation|keyboardHidden" android:exported="false" android:theme="@android:style/Theme.NoTitleBar"> <intent-filter> <action android:name="cn.jpush.android.ui.PushActivity" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="${applicationId}" /> </intent-filter> </activity> <!-- since 3.3.0 Required SDK核心功能 --> <activity android:name="cn.jpush.android.service.JNotifyActivity" android:exported="true" android:taskAffinity="jpush.custom" android:theme="@android:style/Theme.Translucent.NoTitleBar"> <intent-filter> <action android:name="cn.jpush.android.intent.JNotifyActivity" /> <category android:name="${applicationId}" /> </intent-filter> </activity> <!-- User defined. For test only 用戶自定義接收消息器,3.0.7開始支持,目前新tag/alias接口設置結果會在該廣播接收器對應的方法中回調 --> <!-- since 3.3.0 接收JPush相關事件 --> <receiver android:name=".PushMessageReceiver"> <intent-filter> <action android:name="cn.jpush.android.intent.RECEIVE_MESSAGE" /> <category android:name="${applicationId}" /> </intent-filter> </receiver> <!-- Rich push 核心功能 since 2.0.6 --> <activity android:name="cn.jpush.android.ui.PopWinActivity" android:exported="false" android:theme="@style/MyDialogStyle"></activity> <!-- since 3.0.9 Required SDK 核心功能 --> <provider android:name="cn.jpush.android.service.DataProvider" android:authorities="${applicationId}.DataProvider" android:exported="false" android:process=":pushcore" /> <!-- since 1.8.0 option 可選項。用於同一設備中不同應用的JPush服務相互拉起的功能。 --> <!-- 若不啟用該功能可刪除該組件,將不拉起其他應用也不能被其他應用拉起 --> <service android:name="cn.jpush.android.service.DaemonService" android:enabled="true" android:exported="true"> <intent-filter> <action android:name="cn.jpush.android.intent.DaemonService" /> <category android:name="${applicationId}" /> </intent-filter> </service> <!-- since 3.1.0 Required SDK 核心功能 --> <provider android:name="cn.jpush.android.service.DownloadProvider" android:authorities="${applicationId}.DownloadProvider" android:exported="true" /> <!-- Required SDK核心功能 --> <receiver android:name="cn.jpush.android.service.PushReceiver" android:enabled="true" android:exported="false"> <intent-filter android:priority="1000"> <action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED_PROXY" /> <!-- Required 顯示通知欄 --> <category android:name="${applicationId}" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.USER_PRESENT" /> <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> </intent-filter> <!-- Optional --> <intent-filter> <action android:name="android.intent.action.PACKAGE_ADDED" /> <action android:name="android.intent.action.PACKAGE_REMOVED" /> <data android:scheme="package" /> </intent-filter> </receiver> <!-- Required SDK核心功能 --> <receiver android:name="cn.jpush.android.service.AlarmReceiver" android:exported="false" /> <!-- User defined. For test only 用戶自定義的廣播接收器 --> <receiver android:name=".MyReceiver" android:enabled="true" android:exported="false"> <intent-filter> <action android:name="cn.jpush.android.intent.REGISTRATION" /> <!-- Required 用戶注冊SDK的intent --> <action android:name="cn.jpush.android.intent.MESSAGE_RECEIVED" /> <!-- Required 用戶接收SDK消息的intent --> <action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED" /> <!-- Required 用戶接收SDK通知欄信息的intent --> <action android:name="cn.jpush.android.intent.NOTIFICATION_OPENED" /> <!-- Required 用戶打開自定義通知欄的intent --> <action android:name="cn.jpush.android.intent.CONNECTION" /> <!-- 接收網絡變化 連接/斷開 since 1.6.3 --> <category android:name="${applicationId}" /> </intent-filter> </receiver> <!-- since 3.3.0 Required SDK 核心功能 --> <!-- 可配置android:process參數將PushService放在其他進程中 --> <!-- User defined. For test only 繼承自cn.jpush.android.service.JCommonService --> <service android:name=".PushService" android:process=":pushcore"> <intent-filter> <action android:name="cn.jiguang.user.service.action" /> </intent-filter> </service> <!-- Required . Enable it you can get statistics data with channel --> <meta-data android:name="JPUSH_CHANNEL" android:value="developer-default" /> <meta-data android:name="JPUSH_APPKEY" android:value="${JPUSH_APPKEY}" /> </application> </manifest>
極光官網(點我跳轉官網,注冊什么的就自己弄了):
1.創建一個應用
2.應用名稱必填
3.創建完成后就有Appkey了,然后去關聯項目的包名(點擊完成推送設置):
4.填寫項目的包名: