Notification与NotificationManager
NotificationManager类是一个通知管理器类,这个对象是由系统维护的服务,是以单例模式的方式获得,所以一般并不直接实例化这个对象,在Activity中,可以使用Activity.getSystemService(String)方法获取
NotificationManager对象,Activity.getSystemService(String)方法可以通过Android系统级服务的句柄,返回对应的对象,在这里需要返回NotificationManager,所以直接传递Context.NOTIFICATION_SERVICE即可
使用Builder构造器来创建Notification对象
好处:可以保证程序在所有版本上都能正常工作
NotificationChannel
通知渠道,Android 8.0引入了通知渠道,其允许您为要显示的每种通知类型创建用户可自定义的渠道
重要程度设置的属性
在NotificationManager类中
- IMPORTANCE_NONE 关闭通知
- IMPORTANCE_MIN 开启通知,不会弹出,但没有提示音,状态栏中无显示
- IMPORTANCE_LOW 开启通知,不会弹出,不发出提示音,状态栏中显示
- IMPORTANCE_DEFAULT 开启通知,不会弹出,发出提示音,状态栏中显示
- IMPORTANCE_HIGH 开启通知,会弹出,发出提示音,状态栏中显示
常用方法
方法名 | 解析 |
---|---|
setContentTitle(String string) | 设置标题 |
setContentText(String string) | 设置内容 |
setSmallIcon(int icon) | 设置小图标 |
setLargeIcon(Bitmap icon) | 设置通知的大图标 |
setColor(int argb) | 设置小图标颜色 |
setContentIntent(PendingIntent intent) | 设置点击通知后跳转的意图 |
setAutoCancel(boolean boolean) | 设置点击通知后自动清除通知 |
setWhen(long time) | 设置通知被创建的时间 |
实例
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button android:layout_width="wrap_content" android:layout_height="wrap_content"
android:onClick="sendNotification"
android:text="send"
/>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content"
android:onClick="cacelNotification"
android:text="cacel"
/>
</LinearLayout>
MainActivity.java
package com.example.mynotification;
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.view.View;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import androidx.core.app.NotificationCompat;
public class MainActivity extends AppCompatActivity {
private NotificationManager manager;
private Notification notification;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
manager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
NotificationChannel channel=new NotificationChannel("leo","测试通知",NotificationManager.IMPORTANCE_HIGH);
manager.createNotificationChannel(channel);
}
Intent intent=new Intent(this,NotificationActivity.class);
PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,0);
notification=new NotificationCompat.Builder(this,"leo")
.setContentTitle("官方通知")//标题
.setContentText("CSDN博客碰磕邀你一起学习~")//内容
.setSmallIcon(R.drawable.ic_person_outline_black_24dp)//小图标
.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.chunyu02))//大图标
.setColor(Color.parseColor("#ff0000"))//图标颜色
.setContentIntent(pendingIntent) //点击跳转后的意图
.setAutoCancel(true)//点击通知后会取消通知
.build();
}
public void sendNotification(View view){
manager.notify(1,notification);
}
public void cacelNotification(View view){
manager.cancel(1);
}
}
创建一个acticity用于点击通知跳转的地址
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 {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e("leo","进入NotificationActivity");
}
}
注意:一定要将这个类add成acticity,否则无效
注意点:Android从5.0开始对于通知栏图标设计进行了修改,现在Google要求所有的通知栏图标,只使用alpha图层来绘制,而不应该包括rgb图层
效果图
点击通知后会进行跳转
页面时空白的
后台收到通知
测试完成…