效果圖:

S91223-140543.jpg
共分為 三步;
第一步:注冊賬號
- 1.1、先到極光官網(wǎng)(https://www.jiguang.cn/)注冊賬號
-
1.2、創(chuàng)建應(yīng)用
創(chuàng)建應(yīng)用
image.png
image.png
image.png
第二步:配置
-
2.1、jcenter 自動集成步驟
確認(rèn)android studio的 Project 根目錄的主 gradle 中配置了jcenter支持(默認(rèn)支持)
image.png 2.2、在 module 的 gradle 中添加依賴
android {
......
defaultConfig {
applicationId "com.xxx.xxx" //JPush上注冊的包名.
......
ndk {
//選擇要添加的對應(yīng)cpu類型的.so庫。
abiFilters 'armeabi', 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64', 'mips', 'mips64'
}
manifestPlaceholders = [
JPUSH_PKGNAME : applicationId,
JPUSH_APPKEY : "你的appkey", //JPush上注冊的包名對應(yīng)的appkey.
JPUSH_CHANNEL : "developer-default", //暫時填寫默認(rèn)值即可.
]
......
}
......
}
dependencies {
......
implementation 'cn.jiguang.sdk:jpush:3.3.4' // 此處以JPush 3.3.4 版本為例。
implementation 'cn.jiguang.sdk:jcore:2.1.0' // 此處以JCore 2.1.0 版本為例。
......
}
第三步:
- 3.1、創(chuàng)建JPushApplication 類,在onCreate()中初始化Sdk
public class JPushApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
JPushInterface.setDebugMode(true);//打印log
JPushInterface.init(this);
}
}
3.2、創(chuàng)建MyReceiver 類 ,對推送進(jìn)行處理
public class MyReceiver extends JPushMessageReceiver {
/**
* TODO 連接極光服務(wù)器
*/
@Override
public void onConnected(Context context, boolean b) {
super.onConnected(context, b);
Log.e("連接極光服務(wù)器", "onConnected");
}
/**
* TODO 注冊極光時的回調(diào)
*/
@Override
public void onRegister(Context context, String s) {
super.onRegister(context, s);
Log.e("注冊極光時的回調(diào)", "onRegister" + s);
}
/**
* TODO 注冊以及解除注冊別名時回調(diào)
*/
@Override
public void onAliasOperatorResult(Context context, JPushMessage jPushMessage) {
super.onAliasOperatorResult(context, jPushMessage);
Log.e("注冊以及解除注冊別名時回調(diào)", jPushMessage.toString());
}
/**
* TODO 接收到推送下來的通知
* 可以利用附加字段(notificationMessage.notificationExtras)來區(qū)別Notication,指定不同的動作,附加字段是個json字符串
* 通知(Notification),指在手機(jī)的通知欄(狀態(tài)欄)上會顯示的一條通知信息
*/
@Override
public void onNotifyMessageArrived(Context context, NotificationMessage notificationMessage) {
super.onNotifyMessageArrived(context, notificationMessage);
Log.e("接收到推送下來的通知", notificationMessage.toString());
}
/**
* TODO 打開了通知
* notificationMessage.notificationExtras(附加字段)的內(nèi)容處理代碼
* 比如打開新的Activity, 打開一個網(wǎng)頁等..
*/
@Override
public void onNotifyMessageOpened(Context context, NotificationMessage notificationMessage) {
super.onNotifyMessageOpened(context, notificationMessage);
Intent intent=new Intent(context,MainActivity.class);
context.startActivity(intent);
Log.e("打開了通知", notificationMessage.notificationExtras);
}
/**
* TODO 接收到推送下來的自定義消息
* 自定義消息不是通知,默認(rèn)不會被SDK展示到通知欄上,極光推送僅負(fù)責(zé)透傳給SDK。其內(nèi)容和展示形式完全由開發(fā)者自己定義。
* 自定義消息主要用于應(yīng)用的內(nèi)部業(yè)務(wù)邏輯和特殊展示需求
*/
@Override
public void onMessage(Context context, CustomMessage customMessage) {
super.onMessage(context, customMessage);
// 收到消息 顯示通知
Log.d("接收到推送下來的自定義消息", "onMessage: ");
// processCustomMessage(context, customMessage.extra);
}
//通知
private void processCustomMessage(Context context, String message) {
String channelID = "1";
String channelName = "channel_name";
// 跳轉(zhuǎn)的Activity
Intent intent = new Intent(context, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
//適配安卓8.0的消息渠道
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelID, channelName, NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(channel);
}
NotificationCompat.Builder notification =
new NotificationCompat.Builder(context, channelID);
notification.setAutoCancel(true)
.setContentText(message)
.setContentTitle("我是Title")
.setSmallIcon(R.drawable.ic_launcher_background)
.setDefaults(Notification.DEFAULT_ALL)
.setContentIntent(pendingIntent);
notificationManager.notify((int)(System.currentTimeMillis()/1000), notification.build());
}
3.3、在AndroidManifest.xml 中配置
<?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.jpushdemo">
<!-- 自己的包名 -->
<permission
android:name="com.example.jpushdemo.permission.JPUSH_MESSAGE"
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 版本的應(yīng)用在6.0 系統(tǒng)上 層疊窗口權(quán)限 -->
<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">
<!--<!– 注冊繼承JCommonService的服務(wù) –>-->
<!--<service android:name=".XService"-->
<!--android:enabled="true"-->
<!--android:exported="false"-->
<!--android:process=":pushcore">-->
<!--<intent-filter>-->
<!--<action android:name="cn.jiguang.user.service.action" />-->
<!--</intent-filter>-->
<!--</service>-->
<!-- 替換原生極光推送接收器 -->
<receiver
android:name=".MyReceiver"
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.jpushdemo" /> <!--JPush上注冊的包名 -->
</intent-filter>
</receiver>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
到這里就已經(jīng)結(jié)束了,這下來可以到極光推送發(fā)送消息看下效果;

image.png




