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

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默认配置就支持)

buildscript {
repositories {
jcenter()
}
......
}

allprojects {
repositories {
jcenter()
}
}


2.在 module 的 gradle 中添加依赖和AndroidManifest的替换变量。

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", //用户渠道统计的渠道名称
]
}


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 版本为例。
}

注 : 如果在添加以上 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



xmlns:tools="http://schemas.android.com/tools"
package="com.supermap.pushdemo">

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













    
        
            

            
        
    
    
        
            
            
            
            
        
    
    
    
        
            
            
        
        
            
            
        
        
        
            
            

            
        
    

    
    
        
            

            
            
        
    
    
    
        
            
            
        
    

    
    

    
    
    
    
    



然后再自己的application中初始化push服务。

public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
JPushInterface.init(getApplicationContext());
}
}

现在运行app在手机上,就可以接收推送的服务了。


Paste_Image.png

2、集成java后台的推送sdk
使用maven管理的包,直接添加依赖包就可以了。




cn.jpush.api
jpush-client
3.2.17


在controller中

@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收到了推送的消息。

你可能感兴趣的:(极光推送服务集成指南(二))