創(chuàng)建應(yīng)用
進入極光控制臺后汤纸,點擊“創(chuàng)建應(yīng)用”按鈕织狐,進入創(chuàng)建應(yīng)用的界面治泥。 填上你的應(yīng)用程序的名稱以及應(yīng)用包名這二項就可以了筹煮, 最后點擊最下方的 “創(chuàng)建我的應(yīng)用”按鈕,創(chuàng)建應(yīng)用完畢居夹。
Tips:創(chuàng)建應(yīng)用步驟可能會有所出入败潦,僅供參考
創(chuàng)建應(yīng)用
填寫應(yīng)用程序的名稱以及上傳圖標
創(chuàng)建成功
添加應(yīng)用包名
jcenter 自動集成步驟
- 確認android studio的 Project 根目錄的主 gradle 中配置了mavenCentral支持(基本默認支持)
buildscript {
repositories {
mavenCentral()
}
......
}
allprojets {
repositories {
mavenCentral()
}
}
- 在 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", //暫時填寫默認值即可.
]
......
}
......
}
dependencies {
......
implementation 'cn.jiguang.sdk:jpush:4.4.5' // 此處以JPush 4.4.5 版本為例。
implementation 'cn.jiguang.sdk:jcore:3.1.0' // 此處以JCore 3.1.0 版本為例变屁。
......
}
- 在 Androidmanifest 中配置一個Service眼俊,以在更多手機平臺上獲得更穩(wěn)定的支持
public class XService extends JCommonService {
}
- 在AndroidManifest中配置繼承JPushMessageReceiver的廣播
<application
...>
<!-- 注冊繼承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=".jpush.MyReceiver"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="cn.jpush.android.intent.RECEIVE_MESSAGE" />
<!--添加此action,自己處理打開通知粟关,否則默認跳轉(zhuǎn)主頁 -->
<action android:name="cn.jpush.android.intent.NOTIFICATION_OPENED" />
<!--JPush上注冊的包名 -->
<category android:name="com.xxx.xxx" />
</intent-filter>
</receiver>
</application>
調(diào)試以及使用
- 在Application的onCreate()初始化Sdk
@Override
public void onCreate() {
super.onCreate();
JPushInterface.setDebugMode(true);
JPushInterface.init(this);
}
- MyReceiver對推送進行處理
public class MyReceiver extends JPushMessageReceiver {
private static final String TAG = "JIGUANG";
/**
* TODO 連接極光服務(wù)器
*/
@Override
public void onConnected(Context context, boolean b) {
super.onConnected(context, b);
Log.e(TAG, "onConnected");
}
/**
* TODO 注冊極光時的回調(diào)
*/
@Override
public void onRegister(Context context, String s) {
super.onRegister(context, s);
Log.e(TAG, "onRegister" + s);
}
/**
* TODO 注冊以及解除注冊別名時回調(diào)
*/
@Override
public void onAliasOperatorResult(Context context, JPushMessage jPushMessage) {
super.onAliasOperatorResult(context, jPushMessage);
// errorCode=0才是成功
Log.e(TAG, jPushMessage.toString());
}
/**
* TODO 接收到推送下來的通知
* 可以利用附加字段(notificationMessage.notificationExtras)來區(qū)別Notication,指定不同的動作,附加字段是個json字符串
* 通知(Notification)疮胖,指在手機的通知欄(狀態(tài)欄)上會顯示的一條通知信息
*/
@Override
public void onNotifyMessageArrived(Context context, NotificationMessage notificationMessage) {
super.onNotifyMessageArrived(context, notificationMessage);
Log.e(TAG, notificationMessage.toString());
}
/**
* TODO 打開了通知
* notificationMessage.notificationExtras(附加字段)的內(nèi)容處理代碼
* 比如打開新的Activity, 打開一個網(wǎng)頁等..
* 注意:默認會重新打開App主頁闷板,此處要接管澎灸,處理跳轉(zhuǎn)頁面
* 需要在清單文件 MyReceiver 下加入
* <action android:name="cn.jpush.android.intent.NOTIFICATION_OPENED" />
* 極光推送通道會回調(diào)此方法,華為通道不會回調(diào)此方法(具體請看廠商推送)
*/
@Override
public void onNotifyMessageOpened(Context context, NotificationMessage notificationMessage) {
super.onNotifyMessageOpened(context, notificationMessage);
Log.e(TAG, notificationMessage.notificationExtras);
}
/**
* TODO 接收到推送下來的自定義消息
* 自定義消息不是通知遮晚,默認不會被SDK展示到通知欄上性昭,極光推送僅負責(zé)透傳給SDK。其內(nèi)容和展示形式完全由開發(fā)者自己定義县遣。
* 自定義消息主要用于應(yīng)用的內(nèi)部業(yè)務(wù)邏輯和特殊展示需求
*/
@Override
public void onMessage(Context context, CustomMessage customMessage) {
super.onMessage(context, customMessage);
Log.e(TAG, "onMessage");
}
}
- 利用別名精準推送(一定要在回調(diào) onConnected 之后設(shè)置別名才有效)
// 一般登錄之后調(diào)用此方法設(shè)置別名
// sequence 用來標識一次操作的唯一性(退出登錄時根據(jù)此參數(shù)刪除別名)
// alias 設(shè)置有效的別名
// 有效的別名組成:字母(區(qū)分大小寫)糜颠、數(shù)字、下劃線萧求、漢字其兴、特殊字符@!#$&*+=.|。限制:alias 命名長度限制為 40 字節(jié)夸政。
JPushInterface.setAlias(context, int sequence, String alias);
// 退出登錄刪除別名
JPushInterface.deleteAlias(Context context,int sequence);
- 更多的別名用法元旬,請點擊下方鏈接
http://www.reibang.com/p/8646c359577c - 自定義Notification
可通過onMessage(收到自定義消息這個方法里去定義Notification,而不是用發(fā)送通知)
...
@Override
public void onMessage(Context context, CustomMessage customMessage) {
super.onMessage(context, customMessage);
// 收到消息 顯示通知
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, LoginActivity.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.logo120)
.setDefaults(Notification.DEFAULT_ALL)
.setContentIntent(pendingIntent);
notificationManager.notify((int)(System.currentTimeMillis()/1000), notification.build());
}