Android工具集推送

极光推送第三篇:消息跳转和自定义

2019-07-04  本文已影响2人  Small_Cake

极光推送第一篇:配置
极光推送第二篇:消息处理

上一篇极光推送第二篇:消息接收中我们接收并打印了消息,这一篇我们看看消息的交互。

参考极光API通知栏样式定制,我们知道一般来说采用定义好的就行了,但是我们需要做的更好或多样化,就需要自定义通知栏了。可以做到:

这些都是极光官方推送通知的自定义,不是重点,我们主要是接收自定义消息然后进行处理。


1.拿到自定义消息msg,显示通知,点击通知跳主页
    private void showNotice(Context context,String msg){
        NotificationManager manager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
        NotificationCompat.Builder builder;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel("渠道ID", "优惠券商品", NotificationManager.IMPORTANCE_LOW);
            manager.createNotificationChannel(channel);
            builder = new NotificationCompat.Builder(context, "渠道ID");
        }else {
            builder = new NotificationCompat.Builder(context);
        }
        Intent intent = new Intent(context, MainActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0,  intent, PendingIntent.FLAG_CANCEL_CURRENT);

        Notification notification = builder
                .setSmallIcon(R.mipmap.logo)
                .setContentTitle("我是一个标题")
                .setContentText(msg)
                .setContentIntent(contentIntent)
                 .setAutoCancel(true)
                .build();
        manager.notify(0,notification);
    }
2.msg一般自定义为json,我们就需要处理json消息然后展示

例如我们的json消息为:

 {
        "adId": "15",
        "type": "1",
        "id": "589681283609",
        "title": "2019跨境新品跳舞电动机器人六爪鱼炫酷灯光音乐男女孩儿童玩具礼",
        "img": "http://img.alicdn.com/imgextra/i1/2608512658/O1CN01zxXnKu1VVPFROxG3T_!!2608512658.jpg",
        "endPrice": "29.90",
        "frequency": "1",
        "time": "1558434160"
  }
  

我们把这个消息转换为对象,方便操作:

   IndexAd indexAd = GsonUtils.jsonToObj(msg, IndexAd.class);

如果我们需要把推送的商品图片展示到通知栏,那么就要用到RemoteViews
参考:RemoteViews讲解

private void loadJson(Context context, String msg) {
        IndexAd indexAd = GsonUtils.jsonToObj(msg, IndexAd.class);
        String url = indexAd.getImg();//图片
        Disposable disposable = Observable.create(new ObservableOnSubscribe<Bitmap>() {
            @Override
            public void subscribe(ObservableEmitter<Bitmap> emitter) throws Exception {
                File file = Glide.with(context).download(url).submit().get();
                Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
                emitter.onNext(bitmap);
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Bitmap>() {
                    @Override
                    public void accept(Bitmap bitmap) throws Exception {
                        showNotice(context, bitmap, indexAd);
                    }
                });
    }
    private void showNotice(Context context, Bitmap bitmap, IndexAd indexAd) {
        String title = indexAd.getTitle();
        // 使用remoteViews去加载自定义布局
        RemoteViews remoteViews = new RemoteViews(AppUtils.getAppPackageName(), R.layout.notification_custom);
        remoteViews.setTextViewText(R.id.tv_title, title);
        remoteViews.setTextViewText(R.id.tv_price, "¥"+indexAd.getEndPrice()+"元");
        remoteViews.setTextViewText(R.id.tv_time, TimeUtils.tsToMs(indexAd.getTime()));
        remoteViews.setImageViewBitmap(R.id.iv_pic, BitmapUtils.setRoundedCorner(bitmap,9));
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);


        NotificationManager manager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
        NotificationCompat.Builder builder;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel("渠道ID", "优惠券商品", NotificationManager.IMPORTANCE_LOW);
            manager.createNotificationChannel(channel);
            builder = new NotificationCompat.Builder(context, "渠道ID");
        } else {
            builder = new NotificationCompat.Builder(context);
        }
        Notification notification = builder
                .setSmallIcon(R.mipmap.logo)
                .setContentTitle(title)
                .setContentText(title)
                .setContentIntent(contentIntent)
                .setContent(remoteViews)
                .setAutoCancel(true)
                .build();
        manager.notify(0, notification);
    }
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:orientation="horizontal"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/iv_pic"
        android:layout_width="80dp"
        android:layout_height="wrap_content"
        android:src="@mipmap/logo"/>

    <TextView
        android:ellipsize="end"
        android:maxLines="2"
        android:textSize="14sp"
        android:textColor="@color/black"
        android:layout_marginRight="8dp"
        android:layout_marginLeft="8dp"
        android:layout_toRightOf="@+id/iv_pic"
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="标题"/>
    <TextView
        android:textSize="18sp"
        android:layout_alignParentBottom="true"
        android:textColor="@color/red"
        android:layout_marginRight="8dp"
        android:layout_marginLeft="8dp"
        android:layout_toRightOf="@+id/iv_pic"
        android:id="@+id/tv_price"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="19.8元"/>
    <TextView
        android:textSize="12sp"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:layout_marginRight="8dp"
        android:layout_marginLeft="8dp"
        android:id="@+id/tv_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="19.8元"/>

</RelativeLayout>

最后的效果:


自定义布局
上一篇下一篇

猜你喜欢

热点阅读