生也有涯,知也无涯。这篇文章主要讲述Android友盟推送相关的知识,希望能为你提供帮助。
当前版本号:v3.0.5
1.下载SDK解压并导入(import module,compile project(‘:PushSDK‘)),里面有demo,用demo的包名去官网添加一个应用,然后替换demo中的manifest文件里面的appkey和message_secret,就可以直接运行。
2.初始化SDK
- public class App extends Application {
- public PushAgent mPushAgent;
- @Override
- public void onCreate() {
- super.onCreate();
- mPushAgent = PushAgent.getInstance(this);
- //注册推送服务,每次调用register方法都会回调该接口
- mPushAgent.register(new IUmengRegisterCallback() {
- @Override
- public void onSuccess(String deviceToken) {
- //注册成功会返回device token
- Log.e("deviceToken-------",deviceToken);
- }
- @Override
- public void onFailure(String s, String s1) {
- }
- });
- mPushAgent.setPushIntentServiceClass(UmengPushIntentService.class);
- }
- }
3.打开指定页面
包名加activity名,com.umeng.demo.SecondActivity
4.自定义参数
自定义一个service去继承UmengMessageService
public class UmengPushIntentService extends UmengMessageService {
- @Override
- public void onMessage(Context context, Intent intent) {
- Intent data = https://www.songbingjia.com/android/new Intent(intent);
- data.setClass(context, TestActivity.class);
- //需为Intent添加Flag:Intent.FLAG_ACTIVITY_NEW_TASK,否则无法启动Activity。
- data.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- context.startActivity(data);
- }
- }
- public class TestActivity extends AppCompatActivity {
- public static final String TAG = "TestActivity";
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_test);
- final String message = getIntent().getStringExtra(MESSAGE_BODY);
- if (TextUtils.isEmpty(message))
- return;
- final UMessage msg;
- try {
- msg = new UMessage(new JSONObject(message));
- UTrack.getInstance(this).trackMsgClick(msg);
- Map< String, String> extra = msg.extra;
- Log.d(TAG, "message=" + message); //消息体
- Log.d(TAG, "custom=" + msg.custom); //自定义消息的内容
- Log.d(TAG, "title=" + msg.title); //通知标题
- Log.d(TAG, "text=" + msg.text); //通知内容
- AlertDialog dialog;
- if (null != extra & & extra.containsKey("msgType"))
- dialog = new AlertDialog.Builder(this)
- .setTitle("if消息提示")
- .setMessage(msg.text+"11111")
- .setCancelable(false)
- .setPositiveButton("确认", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- }
- })
- .create();
- else
- dialog = new AlertDialog.Builder(this)
- .setTitle("else消息提示")
- .setMessage(msg.text)
- .setCancelable(false)
- .setPositiveButton("确认", null)
- .create();
- dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
- @Override
- public void onDismiss(DialogInterface dialog) {
- onBackPressed();
- }
- });
- dialog.show();
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- }
mPushAgent.setPushIntentServiceClass(UmengPushIntentService.class);
同时需要在manifest在注册
【Android友盟推送】
- < service android:name=".UmengPushIntentService"
- android:process=":push"/>
官方文档:http://dev.umeng.com/push/android/integration#4
推荐阅读
- 如何用Android Studio查看build.gradle源码
- ??????????????????app????????????
- Android系统执行Java jar程序 -- dalvik运行dex Java工程
- OCR移动端车牌识别SDK(打造优质APP;)
- android studio 调试安装
- 解决clnt_create: RPC: Port mapper failure - Unable to receive: errno 113 (No route to
- android存储
- SQL Server集成服务(SSIS使用教程详细图解)
- Web服务教程入门介绍