Notification封装(没做从网络下载)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Notification封装(没做从网络下载)相关的知识,希望对你有一定的参考价值。

1.Notification作为消息通知,这里简单封装了使用

建立一个bean

package com.jcut.view;

/**
 * Author:JsonLu
 * DateTime:2016/2/26 14:25
 * Email:[email protected]
 * Desc:
 **/
public class NotificationModel {


    private String NTitle;
    private String NContent;
    private long NWhen;
    private String NTicker;
    private int NType;
    private String NIcon;

    public String getNTitle() {
        return NTitle;
    }

    public void setNTitle(String NTitle) {
        this.NTitle = NTitle;
    }

    public String getNContent() {
        return NContent;
    }

    public void setNContent(String NContent) {
        this.NContent = NContent;
    }

    public long getNWhen() {
        return NWhen;
    }

    public void setNWhen(long NWhen) {
        this.NWhen = NWhen;
    }

    public String getNTicker() {
        return NTicker;
    }

    public void setNTicker(String NTicker) {
        this.NTicker = NTicker;
    }

    public int getNType() {
        return NType;
    }

    public void setNType(int NType) {
        this.NType = NType;
    }

    public String getNIcon() {
        return NIcon;
    }

    public void setNIcon(String NIcon) {
        this.NIcon = NIcon;
    }
}

简单的Notification 的封装

package com.jcut.view;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.support.v7.app.NotificationCompat;

import com.example.jcut.R;

/**
 * Author:JsonLu
 * DateTime:2016/2/26 12:46
 * Email:[email protected]
 * Desc:
 **/
public class Notification extends NotificationCompat {

    private Builder mBuilder;

    public Notification(Context context) {
        mBuilder = new Builder(context);
    }

    public Notification(Context context, NotificationModel model) {
        mBuilder = new Builder(context);
        mBuilder.setContentTitle(model.getNTitle())
                .setContentText(model.getNContent())
                .setTicker(model.getNTicker())
                .setWhen(model.getNWhen())
                .setPriority(PRIORITY_DEFAULT)
                .setOngoing(false)
                .setDefaults(model.getNType())
                .setSmallIcon(R.drawable.ic_launcher);//此处要下载图片到本地
    }

    public Builder getmBuilder() {
        return mBuilder;
    }

    public void setContentIntent(CallBack call) {
        mBuilder.setContentIntent(call.getPendingIntent());
    }

    public void notify(int notifyId, NotificationManager manager) {
        android.app.Notification notify = mBuilder.build();
        notify.flags = Notification.FLAG_AUTO_CANCEL;
        manager.notify(notifyId, notify);
    }


    public interface CallBack {
        PendingIntent getPendingIntent();
    }
}

简单的Test

public void showButtonNotify() {
        Notification customNot = new Notification(this);
        NotificationCompat.Builder mBuilder = customNot.getmBuilder();
        mBuilder.setContentTitle("测试标题")//设置通知栏标题
                .setContentText("测试内容") //设置通知栏显示内容
                .setTicker("测试通知来啦") //通知首次出现在通知栏,带上升动画效果的
                .setWhen(System.currentTimeMillis())//通知产生的时间,会在通知信息里显示,一般是系统获取到的时间
                .setPriority(PRIORITY_DEFAULT) //设置该通知优先级
                .setOngoing(false)//ture,设置他为一个正在进行的通知。他们通常是用来表示一个后台任务,用户积极参与(如播放音乐)或以某种方式正在等待,因此占用设备(如一个文件下载,同步操作,主动网络连接)
                .setDefaults(DEFAULT_VIBRATE)//向通知添加声音、闪灯和振动效果的最简单、最一致的方式是使用当前的用户默认设置,使用defaults属性,可以组合
//                        Notification.DEFAULT_ALL  Notification.DEFAULT_SOUND 添加声音 // requires VIBRATE permission
                .setSmallIcon(R.mipmap.ic_launcher);//设置通知小ICON

        customNot.setContentIntent(new CallBackImpl());
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        customNot.notify(1, mNotificationManager);
    }

    public PendingIntent getDefalutIntent(int flags) {
        Intent notificationIntent = new Intent(this, TestActivity.class);
        notificationIntent.setAction(Intent.ACTION_MAIN);
        notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        return contentIntent;
    }

    class CallBackImpl implements Notification.CallBack {
        @Override
        public PendingIntent getPendingIntent() {
            return getDefalutIntent(1);
        }
    }

 

以上是关于Notification封装(没做从网络下载)的主要内容,如果未能解决你的问题,请参考以下文章

Android Notification 通知封装成工具类

使用xtrabackup不停服务做从库

原生ajax的简单封装(仅提供大概的思路,具体还有参数验证的都没做)

Notification通知栏的使用

在Push Notification Android上下载文件

Android Notification常见样式总结