Notification通知

notification就是在我们手机通知栏里面显示的,一般是推送。很多app都有推送功能,那么他们的推送是怎么来的呢,首先他们得有一个后台服务在,并且在你连上互联网的时候,他给他的服务器发送一个请求,然后收到一些数据,以notification显示通知,你点开后在请求全部数据。这里我只说说notification。notification你可以使用系统自带的,也可以使用自定义的。对于notification,系统自带的就可以解决很多问题了。但是有时候还是需要自定义。用法很简单,部分说明代码里面有,如果需要更详细的了解他,可以查看api。下面是代码
public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    public Button button1;
    public Button button2;
    public NotificationManager nm=null;
    public Notification n;

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

        nm= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        button1= (Button) findViewById(R.id.button1);
        button2= (Button) findViewById(R.id.button2);
        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
    }

    public void show(){
        //系统自带的通知
       Notification.Builder builder=new Notification.Builder(this);
        builder.setContentText("这是标题");
        builder.setContentText("这是内容");
        builder.setSmallIcon(R.mipmap.ic_launcher);//设置图标
        builder.setWhen(System.currentTimeMillis());//设置显示通知的时间
        //设置大标题,参数真是Bitmap所以将res里的图片转化成Bitmap
        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher));
        builder.setAutoCancel(true);//设置可以被取消
        builder.setDefaults(Notification.DEFAULT_SOUND);
//        Notification.DEFAULT_VIBRATE(添加默认震动提醒);
//        Notification.DEFAULT_SOUND(添加默认声音提醒);
//        Notification.DEFAULT_LIGHTS(添加默认三色灯提醒)
//        Notification.DEFAULT_ALL(添加默认以上3种全部提醒)
        //部分需要权限。
        PendingIntent pendingIntent=PendingIntent.getActivity(this,0,new Intent(this,MainActivity.class),0);
        builder.setContentIntent(pendingIntent);//设置响应

        n=builder.build();
        nm.notify(1,n);

    }

    public void myshow(){
        //自定义通知
        Notification.Builder builder=new Notification.Builder(this);
        builder.setSmallIcon(R.mipmap.ic_launcher);
        RemoteViews remoteViews=new RemoteViews(getPackageName(),R.layout.view_notification);
        builder.setContent(remoteViews);
        remoteViews.setTextViewText(R.id.te,"这是一个自定义标题");

        //事件类型只能为PendingIntent
        PendingIntent pendingIntent=PendingIntent.getActivity(this,0,new Intent(this,MainActivity.class),0);
        remoteViews.setOnClickPendingIntent(R.id.bu,pendingIntent);

        n=builder.build();
        nm.notify(2,n);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button1:
                show();
                break;
            case R.id.button2:
                myshow();
                break;
        }
    }
}

自定义布局。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/te"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/bu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮"/>

</LinearLayout>


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值