炒现饭之友盟推送的简单实现

版权声明:转载请注明出处 https://blog.csdn.net/dl10210950/article/details/73117319

万能的第三方第一步:注册账号并创建应用拿到app_key和app_secret
这里写图片描述

这里写图片描述

第二步:下载SDK

文档地址和sdk下载地址

第三步:将下载的文件解压,里面有一个PushSDK,将其作为module导入项目中

这里写图片描述

这里写图片描述

第四步:配置manifast

<meta-data
            android:name="UMENG_APPKEY"
            android:value="593e2fd6310c937413000f70" />
        <meta-data
            android:name="UMENG_MESSAGE_SECRET"
            android:value="222b7fbb8e9d2cf3a07c4dabd3a76274" />

里面的APPKEY和UMENG_MESSAGE_SECRET都是前面创建应用时拿到的

第五步:配置app下的build.gradle

在Application Module的build.gradle文件的dependencies下添加compile project(‘:PushSDK’)。

第六步:配置自定义的Application

新建一个MyApplication继承自Application,在manifast里面配置上

然后在MyApplication的onCreate()里注册推送

 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) {

            }
        });

第七步:在所有的Activity 的onCreate 方法或在应用的BaseActivity的onCreate方法中添加:

 PushAgent.getInstance(this).onAppStart();

这样就可以推送简单的消息了,推送的步骤:

进入:http://push.umeng.com/选择自己的应用

这里写图片描述

点击自己的应用进入–>点击消息列表–>新建消息

这里写图片描述

然后就是自己写消息的相关内容

这里写图片描述

最后点击提交,手机上就能够收到消息了

以上是简单的集成,接下来说说自定义消息样式:

  • 1 : 在MyAPPlication里面添加自定义的
UmengMessageHandler messageHandler = new UmengMessageHandler() {
            @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);

注意:这里的case 1就是通知样式编号:

这里写图片描述

只有当你样式编号写1的时候才会显示你自定义的布局

还有一点要注意的是自定义的图标要配置,并且在你的drawable文件夹下的图片文件

这里写图片描述

自定义的xml 的notification_view.xml文件代码为:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="64dp" >

    <ImageView
        android:id="@+id/notification_large_icon"
        android:layout_width="64dp"
        android:layout_height="64dp"
        android:scaleType="fitXY"
        android:src="@drawable/umeng_addalias" />

    <TextView
        android:id="@+id/notification_title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="5dp"
        android:layout_toRightOf="@+id/notification_large_icon"
        android:text="Title"
        android:textColor="#000000"/>

    <TextView
        android:id="@+id/notification_text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/notification_title"
        android:layout_marginLeft="10dp"
        android:layout_toRightOf="@+id/notification_large_icon"
        android:text="Message"
        android:textColor="#000000"/>

    <ImageView
        android:id="@+id/notification_small_icon"
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:scaleType="fitXY"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginRight="5dp"
        android:layout_marginBottom="5dp"
        android:src="@drawable/umeng_addalias" />

</RelativeLayout>

这样就可以发送自定义的样式通知消息了

这里写图片描述

猜你喜欢

转载自blog.csdn.net/dl10210950/article/details/73117319
今日推荐