Android友盟推送接入
16lz
2021-01-25
1、集成SDK之前, 请在 http://push.umeng.com 上使用Demo的包名com.umeng.message.example
创建应用,获取应用对应的AppKey和Umeng Message Secret。
2、下载SDK。http://dev.umeng.com/push/android/sdk-download
3、新建项目,
修改module下build.gradle
添加:
ext{ // Sdk and tools minSdkVersion = 15 targetSdkVersion = 25 compileSdkVersion = 25 buildToolsVersion = '25.0.2' // App dependencies supportLibraryVersion = '25.2.0'}
具体如下: // Top-level build file where you can add configuration options common to all sub-projects/modules.buildscript { repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:2.3.0' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files }}allprojects { repositories { jcenter() }}task clean(type: Delete) { delete rootProject.buildDir}ext{ // Sdk and tools minSdkVersion = 15 targetSdkVersion = 25 compileSdkVersion = 25 buildToolsVersion = '25.0.2' // App dependencies supportLibraryVersion = '25.2.0'}
在app目录下build.gradle下,修改 的 applicationId的值为后台的包名,并且绝对路径也要是后台的包名。 如图:
要不然,会出现后台说已经接收到推送消息,但是客户端却看不到通知栏信息。
①
compileSdkVersion 25 buildToolsVersion "25.0.0"
改为 compileSdkVersion rootProject.ext.compileSdkVersion buildToolsVersion rootProject.ext.buildToolsVersion
② minSdkVersion 15 targetSdkVersion 25
改为
minSdkVersion rootProject.ext.minSdkVersion targetSdkVersion rootProject.ext.targetSdkVersion
③ compile 'com.android.support:appcompat-v7:25.2.0'
改为
compile "com.android.support:appcompat-v7:$rootProject.supportLibraryVersion"//
4、把PushSDK,copy放在工程下面,修改PushSDK工程下的build.gradle中的
compileSdkVersion 25 buildToolsVersion "25.0.2"
为 compileSdkVersion rootProject.ext.compileSdkVersion buildToolsVersion rootProject.ext.buildToolsVersion
② minSdkVersion 15 targetSdkVersion 25
改为 minSdkVersion rootProject.ext.minSdkVersion targetSdkVersion rootProject.ext.targetSdkVersion
settings.xml,修改为
include ':app','PushSDK'
然后sync now。 在app下build.gradle 中引入PushSDK。
compile project(':PushSDK')
具体:
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:$rootProject.supportLibraryVersion" // compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha8' testCompile 'junit:junit:4.12' compile project(':PushSDK')}
5、AndroidManifest.xml
xxxxxxxxxxxxxxx改为你对应的UMENG_APPKEY和UMENG_MESSAGE_SECRET,Channel ID 改为你的渠道号,这个是自定义的。 6、注册推送广播:
务必
在工程的Application类的 onCreate()
方法中注册推送服务,无论推送是否开启都需要调用此方法:(ps:要在AndroidManifest.xml中引用Application)
PushAgent mPushAgent = PushAgent.getInstance(this);//注册推送服务,每次调用register方法都会回调该接口mPushAgent.register(new IUmengRegisterCallback() { @Override public void onSuccess(String deviceToken) { //注册成功会返回device token } @Override public void onFailure(String s, String s1) { }})
7、 在所有的Activity 的 onCreate
方法或在应用的 BaseActivity
的 onCreate
方法中添加:
PushAgent.getInstance(context).onAppStart();
8、设置通知栏图标 ①在Application中oncreate添加:
package com.umeng.message.example;import android.app.Application;import android.app.Notification;import android.content.Context;import android.content.Intent;import android.os.Handler;import android.util.Log;import android.widget.RemoteViews;import android.widget.Toast;import com.umeng.message.IUmengRegisterCallback;import com.umeng.message.MsgConstant;import com.umeng.message.PushAgent;import com.umeng.message.UTrack;import com.umeng.message.UmengMessageHandler;import com.umeng.message.UmengNotificationClickHandler;import com.umeng.message.entity.UMessage;/** * Created by Administrator on 2017/3/11 0011. */public class MyApplication extends Application { public static final String UPDATE_STATUS_ACTION = "com.umeng.message.example.action.UPDATE_STATUS"; @Override public void onCreate() { super.onCreate(); onUmengPush(); } private void onUmengPush() { PushAgent mPushAgent = PushAgent.getInstance(this); mPushAgent.setDebugMode(false);//PushSDK的调试日志默认是输出的.建议调用mPushAgent.setDebugMode(false)关闭日志输出。 mPushAgent.setNoDisturbMode(23, 0, 7, 0);//SDK默认在“23:00”到“7:00”之间收到通知消息时不响铃,不振动,不闪灯 mPushAgent.setNotificationPlaySound(MsgConstant.NOTIFICATION_PLAY_SERVER); //声音 mPushAgent.setNotificationPlayLights(MsgConstant.NOTIFICATION_PLAY_SERVER);//呼吸灯 mPushAgent.setNotificationPlayVibrate(MsgConstant.NOTIFICATION_PLAY_SERVER);//振动 mPushAgent.setDisplayNotificationNumber(2);//通知栏按数量显示 mPushAgent.register(new IUmengRegisterCallback() { @Override public void onSuccess(String deviceToken) { //注册成功会返回device token Log.d("debug","deviceToken:"+deviceToken); } @Override public void onFailure(String s, String s1) { } }); UmengMessageHandler messageHandler = new UmengMessageHandler() { /** * 自定义消息的回调方法 * */ @Override public void dealWithCustomMessage(final Context context, final UMessage msg) { new Handler().post(new Runnable() { @Override public void run() { // TODO Auto-generated method stub // 对自定义消息的处理方式,点击或者忽略 boolean isClickOrDismissed = true; if (isClickOrDismissed) { //自定义消息的点击统计 UTrack.getInstance(getApplicationContext()).trackMsgClick(msg); } else { //自定义消息的忽略统计 UTrack.getInstance(getApplicationContext()).trackMsgDismissed(msg); } Toast.makeText(context, msg.custom, Toast.LENGTH_LONG).show(); } }); } /** * 自定义通知栏样式的回调方法 * */ @Override public Notification getNotification(Context context, UMessage msg) { switch (msg.builder_id) { case 1: Notification.Builder builder = new Notification.Builder(context); RemoteViews myNotificationView = new RemoteViews(context.getPackageName(), R.layout.notification_view); myNotificationView.setTextViewText(R.id.notification_title, msg.title); myNotificationView.setTextViewText(R.id.notification_text, msg.text); myNotificationView.setImageViewBitmap(R.id.notification_large_icon, getLargeIcon(context, msg)); myNotificationView.setImageViewResource(R.id.notification_small_icon, getSmallIconId(context, msg)); builder.setContent(myNotificationView) .setSmallIcon(getSmallIconId(context, msg)) .setTicker(msg.ticker) .setAutoCancel(true); return builder.getNotification(); default: //默认为0,若填写的builder_id并不存在,也使用默认。 return super.getNotification(context, msg); } } }; mPushAgent.setMessageHandler(messageHandler); /** * 自定义行为的回调处理 * UmengNotificationClickHandler是在BroadcastReceiver中被调用,故 * 如果需启动Activity,需添加Intent.FLAG_ACTIVITY_NEW_TASK * */ UmengNotificationClickHandler notificationClickHandler = new UmengNotificationClickHandler() { @Override public void dealWithCustomAction(Context context, UMessage msg) { Toast.makeText(context, msg.custom, Toast.LENGTH_LONG).show();//自定义通知打开动作 } }; //使用自定义的NotificationHandler,来结合友盟统计处理消息通知 //参考http://bbs.umeng.com/thread-11112-1-1.html //CustomNotificationHandler notificationClickHandler = new CustomNotificationHandler(); mPushAgent.setNotificationClickHandler(notificationClickHandler); //注册推送服务 每次调用register都会回调该接口 mPushAgent.register(new IUmengRegisterCallback() { @Override public void onSuccess(String deviceToken) { sendBroadcast(new Intent(UPDATE_STATUS_ACTION)); } @Override public void onFailure(String s, String s1) { sendBroadcast(new Intent(UPDATE_STATUS_ACTION)); } }); //此处是完全自定义处理设置 mPushAgent.setPushIntentServiceClass(MyPushIntentService.class); }}
9、测试
①消息推送服务后台( http://push.umeng.com )的“测试模式”中填写该设备的Device Token,将该设备添加为测试设备:
如需手动获取device token,可以调用mPushAgent.getRegistrationId()
方法
最后添加混淆代码:
-dontwarn com.taobao.**-dontwarn anet.channel.**-dontwarn anetwork.channel.**-dontwarn org.android.**-dontwarn org.apache.thrift.**-dontwarn com.xiaomi.**-dontwarn com.huawei.**-keepattributes *Annotation*-keep class com.taobao.** {*;}-keep class org.android.** {*;}-keep class anet.channel.** {*;}-keep class com.umeng.** {*;}-keep class com.xiaomi.** {*;}-keep class com.huawei.** {*;}-keep class org.apache.thrift.** {*;}-keep class com.alibaba.sdk.android.**{*;}-keep class com.ut.**{*;}-keep class com.ta.**{*;}-keep public class **.R$*{ public static final int *;}
更多相关文章
- Android自学笔记-10-Sqlite的简单使用
- EditText限制字符输入
- Android的路径信息
- Android面试题整理(1)
- android图形图像
- 安卓WebView与JavaScript交互
- Android(安卓)消息数字提示,类似微信,BadgeView
- 【Android】Service 的生命周期
- contentProvider操作