Notification集合(一)

这两天公司项目用到消息推送机制,想将后台推送的消息以notification的形式展示个用户,自己也就试着这了几个小demo(包含兼容API11、API16及以后版本和自定义view),废话不多说,直接上代码,具体解释都写在注释中了


    public class MainActivity extends AppCompatActivity {

        public static TextView tvPayload ;
        public static final int NOTIFICATION_API11 = 1;//notificationId
        public static final int NOTIFICATION_API16 = 2;
        public static final int NOTIFICATION_CUSTOM = 3;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }

        /**
        * 在布局文件中为每个按钮设置了onclick="click"
        */
        public void click(View v){
            NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            /**
            * 设置意图,指向另一个activity,当点击notification的时候,会跳转过去
            * action = "com.xiaohua.notification.MSG"
            * category = "android.intent.category.DEFAULT"
            */
            Intent intent = new Intent();
            intent.setAction("com.xiaohua.notification.MSG");
            intent.addCategory("android.intent.category.DEFAULT");
            // 告诉activity使用自己软件中的任务站管理activity,如果没有这个任务栈,系统会帮它创建一个(此设置可有可无)
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            switch (v.getId()){
                case R.id.btn_notify11://编译版本大于11,小于16使用这种方法
                    /**
                     * 创建一个PendingIntent ,和intent类似,由于不是马上调用,需要下拉状态发出的activity,所以采用的是PendingIntent,即点击Notification跳转到哪个acitivity
                     * context 上下文
                     * requestCode 请求码
                     * intent 打开其他界面的意图
                     * flags 用来标识延迟意图是否只能使用一次:FLAG_ONE_SHOT只能使用一次
                     */
                    intent.putExtra("msg","XXX,我想你了");
                    PendingIntent pendingIntent11 = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
                    Notification notify11 = new Notification.Builder(this)
                            .setSmallIcon(R.drawable.notify_icon)//设置状态栏的小图片,也是在下拉状态栏中所显示,如果需要更换更大的图片,可以使用setlargeIcon(Bitmap icon)
                            .setContentTitle("新消息title")//textview中显示的标题
                            .setContentText("XXX,我想你了")//Textview中显示的详细内容
                            .setContentIntent(pendingIntent11)//关联PendingIntent
                            .setNumber(3)//在Textview的右方显示的数字
                            .getNotification();//builder是在API level 16及以后增加的,在API11-API15可以使用getNotification()来代替
                    notify11.flags = Notification.FLAG_AUTO_CANCEL;//通知显示后自动消失
                    notify11.defaults = Notification.DEFAULT_ALL;//设置通知其他选项:使用系统默认值:声音、震动、亮度
                    manager.notify(NOTIFICATION_API11,notify11);
                    break;
                case R.id.btn_notify16:
    //                intent = new Intent(this,Notify_16_Activity.class);
                    intent.putExtra("msg","这是在API 16下创建的notify跳转的界面");
                    PendingIntent pendingIntent16 = PendingIntent.getActivity(this, 1, intent, PendingIntent.FLAG_ONE_SHOT);
                    Notification notify16 = new NotificationCompat.Builder(this)
                            .setSmallIcon(R.drawable.notify_16_icon)
                            .setTicker("TickerText:你有新消息")//状态栏提示消息时显示的内容
                            .setContentTitle("消息title")//设置下拉通知栏中的标题
                            .setContentText("消息content")//设置下拉通知栏中消息体
                            .setContentIntent(pendingIntent16)//消息的延迟意图
                            .setNumber(2)
                            .build();
                    notify16.flags = Notification.FLAG_AUTO_CANCEL;
                    manager.notify(NOTIFICATION_API16,notify16);
                    break;
                case R.id.btn_mynotify:
                    intent.putExtra("msg","我是从自定义的notity跳转过来的");
                    Notification myNotify = new Notification();
                    myNotify.icon = R.drawable.notify_custom;//状态栏弹出通知时显示的图片
                    myNotify.tickerText = "我的自定义消息";//状态栏弹出通知时显示的文字
                    myNotify.when = System.currentTimeMillis();//设置notification显示的时间
                    myNotify.flags = Notification.FLAG_NO_CLEAR;//不自动清除
                    RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notify_custom);
                    remoteViews.setTextViewText(R.id.tv_title,"自定义notify来了");//根据自定义布局中的id设置下拉通知栏中相应的内容
                    remoteViews.setImageViewResource(R.id.iv_icon,R.drawable.notify_custom);//设置通知栏中的内容
    //                remoteViews.setImageViewBitmap(int viewId,Bitmap bitmap);
    //                remoteViews.setImageViewUri(int viewId, Uri uri);
                    myNotify.contentView = remoteViews;//给notification设置自定义的布局
                    PendingIntent pendingIntent = PendingIntent.getActivity(this, 2, intent, PendingIntent.FLAG_ONE_SHOT);
                    myNotify.contentIntent = pendingIntent;
                    manager.notify(NOTIFICATION_CUSTOM,myNotify);
                    break;
                case R.id.btn_clear11:
                    manager.cancel(NOTIFICATION_API11);//根据notification的id号清除消息
                    break;
                case R.id.btn_clear16:
                    manager.cancel(NOTIFICATION_API16);
                    break;
                case R.id.btn_clear_custom:
                    manager.cancel(NOTIFICATION_CUSTOM);
                    break;
                case R.id.btn_clear:
                    manager.cancel(NOTIFICATION_API11);
                    manager.cancel(NOTIFICATION_API16);
                    manager.cancel(NOTIFICATION_CUSTOM);
                    break;
                default:
                    break;
            }
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值