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

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

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

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

  • 改变 Notification 里的铃声、震动、显示与消失行为
  • 自定义通知栏显示样式
  • 不同的 Push 通知,Notification 样式不同
    关于如何自定义我们参考Android SDK 进阶教程

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


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讲解

  • a.首先我们对这条json数据进行处理
private void loadJson(Context context, String msg) {
        IndexAd indexAd = GsonUtils.jsonToObj(msg, IndexAd.class);
        String url = indexAd.getImg();//图片
        Disposable disposable = Observable.create(new ObservableOnSubscribe() {
            @Override
            public void subscribe(ObservableEmitter 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() {
                    @Override
                    public void accept(Bitmap bitmap) throws Exception {
                        showNotice(context, bitmap, indexAd);
                    }
                });
    }
  • b.然后我们用RemoteViews的方式来显示
    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);
    }
  • c.因为RemoteViews支持的控件类型有限,我们就简单创建一个图片+文字的布局notification_custom



    

    
    
    



最后的效果:


自定义布局

你可能感兴趣的:(极光推送第三篇:消息跳转和自定义)