极光推送服务集成指南(二)

1、开始集成anroid端的推送sdk
https://docs.jiguang.cn/jpush/client/Android/android_guide/
说明 : 使用jcenter自动集成的开发者,不需要在项目中添加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”, //用户渠道统计的渠道名称
]
}
</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出现以下提示:
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以上系统将应用图标作为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>
现在运行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}
显示这样的数据就表示推送成功了,app收到了推送的消息。

    原文作者:Bruce杨
    原文地址: https://www.jianshu.com/p/09ea8d73cc86
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞