最近碰到需要一个推送的需求,决定用第三方的友盟推送来实现功能,本程序只是友盟的简单集成,处理逻辑就是友盟的后台发送一条推送消息,在手机上进行接收。
1.效果图:后台效果图:
前端效果图:
2.添加依赖:app里面的build.gradle
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
//PushSDK必须依赖基础组件库,所以需要加入对应依赖
implementation 'com.umeng.umsdk:common:1.5.4'
//PushSDK必须依赖utdid库,所以需要加入对应依赖
implementation 'com.umeng.umsdk:utdid:1.1.5.3'
//PushSDK
implementation 'com.umeng.umsdk:push:5.0.2'
}
project里面的build.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
maven { url 'https://dl.bintray.com/umsdk/release' }
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.2'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
mavenCentral()
maven { url 'https://dl.bintray.com/umsdk/release' }
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
3.清单文件,添加权限,把新建的MyApplication放在application里面
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<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">
4.新建MyApplication,初始化,注意这个地方需要用到appkey,大家请自行去官网注册,申请appkey,运行以后,查看日志,如果有注册成功,这个输出,就证明环境已经配置好了
package comtest.example.admin.ztest;
import android.app.Application;
import android.util.Log;
import com.umeng.commonsdk.UMConfigure;
import com.umeng.message.IUmengRegisterCallback;
import com.umeng.message.PushAgent;
public class MyApplication extends Application {
// 友盟官方android文档地址: https://developer.umeng.com/docs/66632/detail/98581
@Override
public void onCreate() {
super.onCreate();
// 在此处调用基础组件包提供的初始化函数 相应信息可在应用管理 -> 应用信息 中找到 http://message.umeng.com/list/apps
// 参数一:当前上下文context;
// 参数二:应用申请的Appkey(需替换);
// 参数三:渠道名称;
// 参数四:设备类型,必须参数,传参数为UMConfigure.DEVICE_TYPE_PHONE则表示手机;
// 传参数为UMConfigure.DEVICE_TYPE_BOX则表示盒子;默认为手机;
// 参数五:Push推送业务的secret 填充Umeng Message Secret对应信息(需替换)
UMConfigure.init(this,
"5ce256f64ca357600700016a",
"Umeng",
UMConfigure.DEVICE_TYPE_PHONE,
"32fc68fd9e4622c5d80b31fb1ab7bcef");
///获取消息推送代理示例
PushAgent mPushagent = PushAgent.getInstance(this);
// //注册推送服务,每次调用register方法都会回调该接口
mPushagent.register(new IUmengRegisterCallback() {
@Override
public void onSuccess(String deviceToken) {
//注册成功会返回deviceToken deviceToken是推送消息的唯一标志
Log.e("TAG","wang 注册成功:deviceToken:--------> " + deviceToken);
}
@Override
public void onFailure(String s, String s1) {
Log.e("TAG","wang 注册失败:--------> " + "s:" + s + ",s1:" + s1);
}
});
}
}
5.统一在baseActivity里面,初始化统计调用方法,ps:不使用日活统计的忽略这个
import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import com.umeng.message.PushAgent;
public class BaseActivity extends Activity {
protected Activity mContext;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = this;
//Push后台进行日活统计及多维度推送的必调用方法
PushAgent.getInstance(mContext).onAppStart();
}
}
6.后台发送消息,前台进行接收,推送成功,然后就是出现上面的效果图了,还有分享、统计功能暂时没做,详情去参考官网