作者:五环4_183 | 来源:互联网 | 2024-09-27 19:48
文章目录 通知管理器 通知渠道 通知 发送通知 更多效果 添加点击事件 取消消息
通知管理器 通知管理器(NotificationManager)类是一个通知管理器,这个对象是由系统维护的服务,是以单例模式的方式获得,所以一般并不直接实例化这个对象。在 Activity中,可以使用 Activity.getSystemService(String)
方法获取通知管理器对象,Activity.getSystemService(String)
方法可以通过 Android系统级服务的句柄,返回对应的对象。在这里需要返回NotificationManager,所以直接传递Context.NOTIFICATION_SERVICE
即可。
manager= ( NotificationManager) getSystemService ( NOTIFICATION_SERVICE) ;
通知渠道 Android 8.0引入了通知渠道(NotificationChannel),不同的渠道通知的消息机器有不同的反应,用户可自定义渠道。
新建通知渠道对象时,有三个参数,id
是渠道的id,name
是,importance
就是重要程度。 下面列出通知管理器类中通知重要程度设置: IMPORTANT_NONE
关闭通知 IMPORTANT_MIN
开启通知,不弹出,无提示音,状态栏不显示 IMPORTANT_LOW
开启通知,不弹出,无提示音,状态栏显示 IMPORTANT_DEFAULT
开启通知,不会弹出,发出提示音,状态栏显示 IMPORTANT_HIGH
开启通知,会弹出,发出提示音,状态栏显示
if ( Build. VERSION. SDK_INT>= Build. VERSION_CODES. O) { NotificationChannel channel= new NotificationChannel ( "ShadyPi" , "ShadyPi的博客针不戳" , NotificationManager. IMPORTANCE_HIGH) ; manager. createNotificationChannel ( channel) ; }
通知 使用NotificationCompat类的Builder构造器来创建Notification对象,可以保证程序在所有的版本上都能正常工作。 Android8.0新増了通知渠道这个概念,如果没有设置,则通知无法在 Android8.0的机器上显示。注意这里的渠道id一定要是之前声明过的。
通知常见属性: setContentTitle(String string)
设置标题 setContentText(String string)
设置文本内容 setSmallIcon(int icon)
设置小图标 setLargeIcon(Bitmap icon)
设置大图标 setColor(int argb)
设置小图标颜色 setContentIntent(PendingIntent intent)
设置点击通知后的跳转意图 setAutoCancel(boolean boolean)
设置点击通知后自动清除通知 setWhen(long when)
设置通知被创建的时间
注意:前三个属性必须设置,且从Android 5.0开始,通知栏的图标应该只使用alpha图层,不应该包括RGB图层(简单说就是不能有颜色)。
note= new NotificationCompat. Builder ( this , "ShadyPi" ) . setContentTitle ( "帅气的宝藏博主" ) . setContentText ( "ShadyPi" ) . setSmallIcon ( R. drawable. ic_baseline_all_inclusive_24) . build ( ) ;
发送通知 先设置两个按钮,用于通知发送和取消。
< LinearLayout xmlns: android&#61; " http://schemas.android.com/apk/res/android" android: layout_width&#61; " match_parent" android: layout_height&#61; " match_parent" android: orientation&#61; " vertical" > < Buttonandroid: onClick&#61; " send" android: text&#61; " 发送通知" android: layout_width&#61; " wrap_content" android: layout_height&#61; " wrap_content" /> < Buttonandroid: onClick&#61; " cancel" android: text&#61; " 取消通知" android: layout_width&#61; " wrap_content" android: layout_height&#61; " wrap_content" /> LinearLayout>
先在java代码里写个简单的发送&#xff0c;使用notify(int id,Notification notification)
来发送一条通知&#xff0c;id可以随便填&#xff0c;第二个参数就是我们要发送的通知&#xff1a;
public void send ( View view) { manager. notify ( 1 , note) ; }
看看效果&#xff1a;
更多效果 添加大图标&#xff0c;注意图片要先转换成Bitmap&#xff1a;
note&#61; new NotificationCompat. Builder ( this , "ShadyPi" ) . setContentTitle ( "帅气的宝藏博主" ) . setContentText ( "ShadyPi" ) . setSmallIcon ( R. drawable. ic_baseline_all_inclusive_24) . setLargeIcon ( BitmapFactory. decodeResource ( getResources ( ) , R. drawable. image) ) . build ( ) ;
设置小图标颜色&#xff0c;注意先转成int&#xff1a;
. setColor ( Color. parseColor ( "#ff0000" ) )
设置点击后取消通知&#xff1a;
. setAutoCancel ( true )
设置通知被创建的时间&#xff0c;一般通知都显示弹出的时间&#xff08;即当前时间&#xff09;&#xff0c;这是默认的。除非有特殊需求&#xff0c;否则不用专门设置。
添加点击事件 在MainActivity
同一文件夹下创建点击动作NotificationActivity.java
&#xff1a;
package com. example. mynotification; import android. app. Activity; import android. os. Bundle; import android. util. Log; import androidx. annotation. Nullable; public class NotificationActivity extends Activity { &#64;Override protected void onCreate ( &#64;Nullable Bundle savedInstanceState) { super . onCreate ( savedInstanceState) ; Log. e ( "ShadyPi" , "点击通知" ) ; } }
编写完以后鼠标点中类名NotificationActivity
&#xff0c;用快捷键Alt&#43;Enter
选择弹出的第一个选项注册add to manifest。
写好该动作后&#xff0c;在MainActivity
中声明点击动作要求的PendingIntent变量&#xff0c;然后就可以设置了&#xff1a;
Intent intent&#61; new Intent ( this , NotificationActivity. class ) ; PendingIntent pending&#61; PendingIntent. getActivity ( this , 0 , intent, 0 ) ; note&#61; new NotificationCompat. Builder ( this , "ShadyPi" ) . setContentTitle ( "帅气的宝藏博主" ) . setContentText ( "ShadyPi" ) . setSmallIcon ( R. drawable. ic_baseline_all_inclusive_24) . setLargeIcon ( BitmapFactory. decodeResource ( getResources ( ) , R. drawable. image) ) . setColor ( Color. parseColor ( "#ff0000" ) ) . setContentIntent ( pending) . setAutoCancel ( true ) . build ( ) ;
点击后可以在调试日志中看到输出&#xff0c;说明点击动作被执行了&#xff0c;同时点击后消息通知自动消失了。
取消消息 除了设置点击自动取消&#xff0c;还可以手动取消消息&#xff0c;只需要用cancel(int id)
就可以取消掉相应id的消息。
public void cancel ( View view) { manager. cancel ( 1 ) ; }
MainActivity
完整代码&#xff1a;
package com. example. mynotification; import androidx. appcompat. app. AppCompatActivity; import androidx. core. app. NotificationCompat; import android. app. Notification; import android. app. NotificationChannel; import android. app. NotificationManager; import android. app. PendingIntent; import android. content. Intent; import android. graphics. BitmapFactory; import android. graphics. Color; import android. os. Build; import android. os. Bundle; import android. view. View; public class MainActivity extends AppCompatActivity { private NotificationManager manager; private Notification note; &#64;Override protected void onCreate ( Bundle savedInstanceState) { super . onCreate ( savedInstanceState) ; setContentView ( R. layout. activity_main) ; manager&#61; ( NotificationManager) getSystemService ( NOTIFICATION_SERVICE) ; if ( Build. VERSION. SDK_INT>&#61; Build. VERSION_CODES. O) { NotificationChannel channel&#61; new NotificationChannel ( "ShadyPi" , "ShadyPi的博客针不戳" , NotificationManager. IMPORTANCE_HIGH) ; manager. createNotificationChannel ( channel) ; } Intent intent&#61; new Intent ( this , NotificationActivity. class ) ; PendingIntent pending&#61; PendingIntent. getActivity ( this , 0 , intent, 0 ) ; note&#61; new NotificationCompat. Builder ( this , "ShadyPi" ) . setContentTitle ( "帅气的宝藏博主" ) . setContentText ( "ShadyPi" ) . setSmallIcon ( R. drawable. ic_baseline_all_inclusive_24) . setLargeIcon ( BitmapFactory. decodeResource ( getResources ( ) , R. drawable. image) ) . setColor ( Color. parseColor ( "#ff0000" ) ) . setContentIntent ( pending) . setAutoCancel ( true ) . build ( ) ; } public void send ( View view) { manager. notify ( 1 , note) ; } public void cancel ( View view) { manager. cancel ( 1 ) ; } }