極光推送SDK
1、注冊
在官網注冊,並創建安卓應用,然后根據提示下載SDK包,內含SDK和Demo,可以導入嘗試,如何導入sdk包中有md文檔
2、集成到安卓中
-
在模塊下的build.grade中加入如下配置:
apply plugin: 'com.android.application' android { compileSdkVersion 29 buildToolsVersion "29.0.3" defaultConfig { applicationId "你得應用包名" minSdkVersion 16 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' } } } dependencies { // 此處加入依賴 compile 'cn.jiguang.sdk:jpush:3.8.6' // 此處以JPush 3.8.6 版本為例。 compile 'cn.jiguang.sdk:jcore:2.5.5' // 此處以JCore 2.5.5 版本為例。 }
3、創建InitApplication
繼承於Application
package com.liqitech.parking;
import android.app.Application;
import android.util.Log;
import java.util.HashSet;
import java.util.Set;
import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;
public class InitApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
Log.e("xxx", "開始初始化!");
/*開啟debug*/
JPushInterface.setDebugMode(true);
JPushInterface.init(this);
// 如果不想設置別名的話就不調用這個方法
initJgAlias();
}
/**
* 程序停止,取消連接
*/
@Override
public void onTerminate() {
// 在這里可以斷開連接
super.onTerminate();
}
/**
* 初始化極光推送的別名
*/
public void initJgAlias() {
//在jpush上設置別名,第一個參數就是applicationcontext,第二個隨意,別重復就行,第三個就是你的別名
JPushInterface.setAlias(this.getApplicationContext(), 57, "testAlias");
}
}
4、創建Receiver
package com.liqitech.parking;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import org.json.JSONException;
import org.json.JSONObject;
import cn.jpush.android.api.CmdMessage;
import cn.jpush.android.api.CustomMessage;
import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.JPushMessage;
import cn.jpush.android.api.NotificationMessage;
import cn.jpush.android.service.JPushMessageReceiver;
public class PushMessageReceiver extends JPushMessageReceiver {
private static final String TAG = "PushMessageReceiver";
@Override
public void onMessage(Context context, CustomMessage customMessage) {
Log.e(TAG, "[onMessage] " + customMessage);
processCustomMessage(context, customMessage);
}
@Override
public void onNotifyMessageArrived(Context context, NotificationMessage message) {
Log.e(TAG, "[onNotifyMessageArrived] " + message);
}
@Override
public void onNotifyMessageDismiss(Context context, NotificationMessage message) {
Log.e(TAG, "[onNotifyMessageDismiss] " + message);
}
@Override
public void onRegister(Context context, String registrationId) {
Log.e(TAG, "[onRegister] " + registrationId);
}
@Override
public void onConnected(Context context, boolean isConnected) {
Log.e(TAG, "[onConnected] " + isConnected);
}
@Override
public void onCommandResult(Context context, CmdMessage cmdMessage) {
Log.e(TAG, "[onCommandResult] " + cmdMessage);
}
@Override
public void onTagOperatorResult(Context context, JPushMessage jPushMessage) {
// TagAliasOperatorHelper.getInstance().onTagOperatorResult(context,jPushMessage);
super.onTagOperatorResult(context, jPushMessage);
Log.e("xxx", "onTagOperatorResult");
}
@Override
public void onCheckTagOperatorResult(Context context, JPushMessage jPushMessage) {
super.onCheckTagOperatorResult(context, jPushMessage);
Log.e("xxx", "onCheckTagOperatorResult");
}
@Override
public void onAliasOperatorResult(Context context, JPushMessage jPushMessage) {
super.onAliasOperatorResult(context, jPushMessage);
Log.e("xxx", "onAliasOperatorResult+--->"+jPushMessage);
if (jPushMessage.getErrorCode()==6002){
InitApplication initApplication = (InitApplication) context.getApplicationContext();
Log.e("xxx", "errorCode=6002,重連");
initApplication.initJgAlias();
}
}
@Override
public void onMobileNumberOperatorResult(Context context, JPushMessage jPushMessage) {
super.onMobileNumberOperatorResult(context, jPushMessage);
Log.e("xxx", "onMobileNumberOperatorResult");
}
//send msg to MainActivity
private void processCustomMessage(Context context, CustomMessage customMessage) {
/* if (MainActivity.isForeground) {
String message = customMessage.message;
String extras = customMessage.extra;
Intent msgIntent = new Intent(MainActivity.MESSAGE_RECEIVED_ACTION);
msgIntent.putExtra(MainActivity.KEY_MESSAGE, message);
if (!ExampleUtil.isEmpty(extras)) {
try {
JSONObject extraJson = new JSONObject(extras);
if (extraJson.length() > 0) {
msgIntent.putExtra(MainActivity.KEY_EXTRAS, extras);
}
} catch (JSONException e) {
}
}
LocalBroadcastManager.getInstance(context).sendBroadcast(msgIntent);*/
// }
Log.e("xxx", "processCustomMessage");
}
@Override
public void onNotificationSettingsCheck(Context context, boolean isOn, int source) {
super.onNotificationSettingsCheck(context, isOn, source);
Log.e(TAG, "[onNotificationSettingsCheck] isOn:" + isOn + ",source:" + source);
}
}
注意onAliasOperatorResult方法中是設置別名結果的回調,如果errorcode是6002,那么就重連
4、AndroidMainfest.xm中配置
配置權限:
<!-- Required -->
<permission android:name="你的包名.permission.JPUSH_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="你的包名.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" />
在application節點中加入屬性name,並設置為.InitApplication。
在application節點中加入配置:
<!-- 極光推送配置開始 -->
<service
android:name="cn.jpush.android.service.PushService"
android:enabled="true"
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>
<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>
<!--<service android:name=".push.jg.JGService"
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="cn.jpush.android.service.PushReceiver"
android:enabled="true" >
<intent-filter android:priority="1000">
<action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED_PROXY" />
<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 since 3.0.7 -->
<!-- 新的 tag/alias 接口結果返回需要開發者配置一個自定的廣播 -->
<!-- 3.3.0開始所有事件將通過該類回調 -->
<!-- 該廣播需要繼承 JPush 提供的 JPushMessageReceiver 類, 並如下新增一個 Intent-Filter -->
<receiver
android:name=".PushMessageReceiver"
android:enabled="true"
android:exported="false" >
<intent-filter>
<action android:name="cn.jpush.android.intent.RECEIVE_MESSAGE" />
<category android:name="${applicationId}" />
</intent-filter>
</receiver>
<receiver android:name="cn.jpush.android.service.AlarmReceiver" />
<meta-data android:name="JPUSH_CHANNEL" android:value="${JPUSH_CHANNEL}"/>
<meta-data android:name="JPUSH_APPKEY" android:value="${JPUSH_APPKEY}" /> <!-- </>值來自開發者平台取得的AppKey-->
<!-- 極光推送配置結束 -->
5、在極光開發者平台中測試推送自定義消息,即可在控制台中看到提示:
看到onMessage日志出現即為成功