極光推送服務集成指南(二)

1、開始集成anroid端的推送sdk
https://docs.jiguang.cn/jpush/client/Android/android_guide/
說明 : 使用jcenter自動集成的開發(fā)者,不需要在項目中添加jar和so贞岭,jcenter會自動完成依賴工扎;在AndroidManifest.xml中不需要添加任何JPush SDK 相關的配置汇歹,jcenter會自動導入攘残。

1.確認android studio的 Project 根目錄的主 gradle 中配置了jcenter支持讨惩。(新建project默認配置就支持)
<pre>buildscript {
repositories {
jcenter()
}
......
}

allprojects {
repositories {
jcenter()
}
}</pre>
2.在 module 的 gradle 中添加依賴和AndroidManifest的替換變量滥玷。
<pre>
defaultConfig {
applicationId "com.supermap.pushdemo"
minSdkVersion 19
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
ndk {
//選擇要添加的對應cpu類型的.so庫氏身。
abiFilters 'armeabi', 'armeabi-v7a', 'arm64-v8a'
// 還可以添加 'x86', 'x86_64', 'mips', 'mips64'
}
manifestPlaceholders = [
JPUSH_PKGNAME : applicationId,
JPUSH_APPKEY : "1b530a8462ef9f53d7b3b0da", //JPush上注冊的包名對應的appkey.
JPUSH_CHANNEL : "chaotu", //用戶渠道統(tǒng)計的渠道名稱
]
}
</pre>
<pre>
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:26.+'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
compile 'cn.jiguang.sdk:jpush:3.0.5' // 此處以JPush 3.0.5 版本為例。
compile 'cn.jiguang.sdk:jcore:1.1.2' // 此處以JCore 1.1.2 版本為例惑畴。
}
</pre>
注 : 如果在添加以上 abiFilter 配置之后android Studio出現(xiàn)以下提示:
NDK integration is deprecated in the current plugin. Consider trying the new experimental plugin.
則在 Project 根目錄的gradle.properties文件中添加:
android.useDeprecatedNdk=true
說明:若沒有res/drawable-xxxx/jpush_notification_icon這個資源默認使用應用圖標作為通知icon蛋欣,在5.0以上系統(tǒng)將應用圖標作為statusbar icon可能顯示不正常,用戶可定義沒有陰影和漸變色的icon替換這個文件如贷,文件名不要變陷虎。

配置 AndroidManifest.xml

<pre>
<?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.supermap.pushdemo">

<permission
android:name="${applicationId}.permission.JPUSH_MESSAGE"
android:protectionLevel="signature" />

<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.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.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" />

<application
    android:name=".MyApplication"
    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>
    <service
        android:name="cn.jpush.android.service.PushService"
        android:enabled="true"
        android:exported="false">
        <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核心功能-->
    <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 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>
    <!-- SDK核心功能-->
    <activity
        android:name="cn.jpush.android.ui.PopWinActivity"
        android:configChanges="orientation|keyboardHidden"
        android:exported="false"
        android:theme="@style/MyDialogStyle">
        <intent-filter>
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="${applicationId}" />
        </intent-filter>
    </activity>

    <!-- Required SDK核心功能-->
    <service
        android:name="cn.jpush.android.service.DownloadService"
        android:enabled="true"
        android:exported="false"></service>

    <!-- Required SDK核心功能-->
    <receiver android:name="cn.jpush.android.service.AlarmReceiver" />
    <meta-data
        android:name="JPUSH_CHANNEL"
        android:value="developer-default"
        tools:replace="android:value" />
    <!-- Required. AppKey copied from Portal -->
    <meta-data
        android:name="JPUSH_APPKEY"
        android:value="1b530a8462ef9f53d7b3b0da" />
</application>

</manifest>
</pre>
然后再自己的application中初始化push服務。
<pre>
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
JPushInterface.init(getApplicationContext());
}
}
</pre>
現(xiàn)在運行app在手機上杠袱,就可以接收推送的服務了尚猿。


Paste_Image.png

2、集成java后臺的推送sdk
使用maven管理的包楣富,直接添加依賴包就可以了凿掂。
<pre>

<dependency>
<groupId>cn.jpush.api</groupId>
<artifactId>jpush-client</artifactId>
<version>3.2.17</version>
</dependency>
</pre>
在controller中
<code>
@Controller
@RequestMapping(produces = {"application/json;charset=UTF-8"})
public class CommonController {
@RequestMapping(value = "/push", method = RequestMethod.GET)
@ResponseBody
public void push() {
sendMsg();
}
public void sendMsg() {
String MASTER_SECRET = "00dc7c2248858504b0585984";
String APP_KEY = "1b530a8462ef9f53d7b3b0da";
JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY, null, ClientConfig.getInstance());
// For push, all you need do is to build PushPayload object.
PushPayload payload = buildPushObject_all_all_alert();
try {
PushResult result = jpushClient.sendPush(payload);
System.out.println("Got result - " + result);
} catch (APIConnectionException e) {
// Connection error, should retry later
System.out.println("Connection error, should retry later>>" + e);
} catch (APIRequestException e) {
e.printStackTrace();
// Should review the error, and fix the request
System.out.println("Should review the error, and fix the request>>" + e);
System.out.println("HTTP Status: " + e.getStatus());
System.out.println("Error Code: " + e.getErrorCode());
System.out.println("Error Message: " + e.getErrorMessage());
}
}
public static PushPayload buildPushObject_all_all_alert() {
String ALERT = "bruce推送";
return PushPayload.alertAll(ALERT);
}
}
在瀏覽器中模擬調用接口,
Got result - {"msg_id":3798473099,"sendno":2080492529,"statusCode":0}
Got result - {"msg_id":3848738232,"sendno":1571498643,"statusCode":0}
Got result - {"msg_id":3848738233,"sendno":175623989,"statusCode":0}
Got result - {"msg_id":2484149635,"sendno":356310733,"statusCode":0}
顯示這樣的數(shù)據(jù)就表示推送成功了纹蝴,app收到了推送的消息庄萎。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市塘安,隨后出現(xiàn)的幾起案子糠涛,更是在濱河造成了極大的恐慌,老刑警劉巖兼犯,帶你破解...
    沈念sama閱讀 219,039評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件忍捡,死亡現(xiàn)場離奇詭異集漾,居然都是意外死亡,警方通過查閱死者的電腦和手機锉罐,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,426評論 3 395
  • 文/潘曉璐 我一進店門帆竹,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人脓规,你說我怎么就攤上這事栽连。” “怎么了侨舆?”我有些...
    開封第一講書人閱讀 165,417評論 0 356
  • 文/不壞的土叔 我叫張陵秒紧,是天一觀的道長。 經常有香客問我挨下,道長熔恢,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,868評論 1 295
  • 正文 為了忘掉前任臭笆,我火速辦了婚禮叙淌,結果婚禮上,老公的妹妹穿的比我還像新娘愁铺。我一直安慰自己鹰霍,他們只是感情好,可當我...
    茶點故事閱讀 67,892評論 6 392
  • 文/花漫 我一把揭開白布茵乱。 她就那樣靜靜地躺著茂洒,像睡著了一般。 火紅的嫁衣襯著肌膚如雪瓶竭。 梳的紋絲不亂的頭發(fā)上督勺,一...
    開封第一講書人閱讀 51,692評論 1 305
  • 那天,我揣著相機與錄音斤贰,去河邊找鬼智哀。 笑死,一個胖子當著我的面吹牛荧恍,可吹牛的內容都是我干的盏触。 我是一名探鬼主播,決...
    沈念sama閱讀 40,416評論 3 419
  • 文/蒼蘭香墨 我猛地睜開眼块饺,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了雌芽?” 一聲冷哼從身側響起授艰,我...
    開封第一講書人閱讀 39,326評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎世落,沒想到半個月后淮腾,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經...
    沈念sama閱讀 45,782評論 1 316
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,957評論 3 337
  • 正文 我和宋清朗相戀三年谷朝,在試婚紗的時候發(fā)現(xiàn)自己被綠了洲押。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,102評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡圆凰,死狀恐怖杈帐,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情专钉,我是刑警寧澤挑童,帶...
    沈念sama閱讀 35,790評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站跃须,受9級特大地震影響站叼,放射性物質發(fā)生泄漏。R本人自食惡果不足惜菇民,卻給世界環(huán)境...
    茶點故事閱讀 41,442評論 3 331
  • 文/蒙蒙 一尽楔、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧第练,春花似錦阔馋、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,996評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至驹碍,卻和暖如春壁涎,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背志秃。 一陣腳步聲響...
    開封第一講書人閱讀 33,113評論 1 272
  • 我被黑心中介騙來泰國打工怔球, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人浮还。 一個月前我還...
    沈念sama閱讀 48,332評論 3 373
  • 正文 我出身青樓竟坛,卻偏偏與公主長得像,于是被迫代替她去往敵國和親钧舌。 傳聞我的和親對象是個殘疾皇子担汤,可洞房花燭夜當晚...
    茶點故事閱讀 45,044評論 2 355

推薦閱讀更多精彩內容