zoukankan      html  css  js  c++  java
  • android Notification总结

    在发送一个Notification前,我们需要准备好一个NotificationManager

    NotificationManager manager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);

    notification = new Notification();

    1、定义一个Notification具备的三个基本参数icon,tickerText,以及添加打开关闭方式

    notification.icon = R.drawable.ic_launcher;
    notification.tickerText = "注意了,我被扔到状态栏了";
     
    notification.flags = Notification.FLAG_AUTO_CANCEL;// 点击通知之后自动消失
    notification.defaults=notification.DEFAULT_SOUND|notification.DEFAULT_VIBRATE; //notification被notify的时候,触发默认声音和默认震动
     
    2、为notification添加一个意图(Intent):
    Intent notificationIntent = new Intent(this, MainActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_ONE_SHOT);
    notification.contentIntent = contentIntent;
     
    由于Notification的包装内容为Intent,我们就可以方便地为通知被点击的触发的事件传值:
    openintent.putExtra("isRead", true);
    openintent.putExtra("name", "CaiYu");
     
    4、发送通知
    manager.notify(notifacation_id, notification);//notifacation_id由自己指定,为每一个Notification对应的唯一标志
     
    5、自定义通知栏
    RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notify_view);//自定义的layout
    contentView.setImageViewResource(R.id.notify_imageLog, R.drawable.ic_launcher);//layout中的控件
    contentView.setTextViewText(R.id.notify_name, "1");
    notification.contentView = contentView;
     
    6、删除、更改某些通知
    manager.cancel(notifacation_id);
    manager.cancelAll();
    manager.notify(notifaction_id,newNotification);
     
    7、用notification.builder实现
    NotificationCompat.Builder builder = new Builder(context);
    builder.setContent(remoteViews).setSmallIcon(R.drawable.icon).setLargeIcon(icon).setOngoing(true).setTicker("music is playing");
    manager.notify(notifaction_id, builder.build());      
     
  • 相关阅读:
    leetcode——448.找到所有数组中消失的数字
    leetcode——268.缺失数字
    leetcode——680.验证回文字符串2
    leetcode——125.验证回文串
    leetcode——217.存在重复元素
    leetcode——189.旋转数组
    02-04 线性回归
    02-03 感知机对偶形式(鸢尾花分类)
    02-02 感知机原始形式(鸢尾花分类)
    02-01 感知机
  • 原文地址:https://www.cnblogs.com/hzwcoming/p/3873162.html
Copyright © 2011-2022 走看看