Android友盟推送接入

本文详细介绍了如何在Android项目中集成友盟推送SDK,包括在友盟官网创建应用、下载及配置SDK、注册推送服务、自定义通知样式等步骤,并提供了测试方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

        <meta-data
            android:name="UMENG_APPKEY"
            android:value="xxxxxxxxxxxxxxxxxxxxxxxxxxxx"/>
        <meta-data
            android:name="UMENG_MESSAGE_SECRET"
            android:value="xxxxxxxxxxxxxxxxxxxxxxxxxxxx"/>
        <meta-data
            android:name="UMENG_CHANNEL"
            android:value="Channel ID" />
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 *;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值