package com.example.notification;import android.app.NotificationChannel;import android.app.NotificationManager;import android.os.Build;import android.support.v4.app.NotificationCompat;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;public class MainActivity extends AppCompatActivity {    Button button1,button2;    NotificationManager manager;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        button1=findViewById(R.id.button1);        button2=findViewById(R.id.button2);        manager= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);        button1.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                //高版本需要渠道                if(Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O){                    //只在Android O之上需要渠道                    NotificationChannel notificationChannel = new NotificationChannel("channelid1","channelname",NotificationManager.IMPORTANCE_HIGH);                    //如果这里用IMPORTANCE_NOENE就需要在系统的设置里面开启渠道,通知才能正常弹出                    manager.createNotificationChannel(notificationChannel);                }                NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this,"channelid1");                builder.setSmallIcon(R.drawable.ico)                        .setContentTitle("通知标题")                        .setContentText("通知内容")                        .setAutoCancel(true);                manager.notify(0x12,builder.build());            }        });    }}

 

转载于:https://www.cnblogs.com/chunshu/p/10317960.html

更多相关文章

  1. android Notification动态更新
  2. 如何在Root的手机上开启ViewServer,使得HierachyViewer能够连接
  3. android 8.0 报错StartForeground Bad Notification Error 解决
  4. Android中多USB摄像头解决方案——UVCCamera
  5. Android(安卓)4.4 KitKat NotificationManagerService使用详解与
  6. Android(安卓)中状态栏(屏幕顶部)消息的显示 Notification
  7. Android中switch控件默认是开启/关闭以及edittext进行点击时禁止
  8. 是否应该开启WebView硬件加速?
  9. android之Notification通知

随机推荐

  1. Android(安卓)-- 基础
  2. Android代码混淆-添加了Gson遇到的问题
  3. 关于Android的Run as 不出现Android Appl
  4. 【Android】沉浸式状态栏完美解决方案
  5. Android开发包下载(包括开发所需所有安装
  6. Android DrawerLayout 侧滑菜单
  7. Android 网络编程之---HttpClient 与 Htt
  8. Android(安卓)UI--动画 Animation
  9. Android(安卓)Layout Binder(在线将XML中V
  10. android中Activity与service之间相互通信