Android开发之Notification(实现消息弹窗、提示音以及点击事件) 您所在的位置:网站首页 如何设置手机短信自动弹出 Android开发之Notification(实现消息弹窗、提示音以及点击事件)

Android开发之Notification(实现消息弹窗、提示音以及点击事件)

2024-06-30 11:49| 来源: 网络整理| 查看: 265

文章目录 通知管理器通知渠道通知发送通知更多效果添加点击事件取消消息

通知管理器

通知管理器(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(); 发送通知

先设置两个按钮,用于通知发送和取消。

先在java代码里写个简单的发送,使用notify(int id,Notification notification)来发送一条通知,id可以随便填,第二个参数就是我们要发送的通知:

public void send(View view) { manager.notify(1,note); }

看看效果: 在这里插入图片描述在这里插入图片描述

更多效果

添加大图标,注意图片要先转换成Bitmap:

note=new NotificationCompat.Builder(this,"ShadyPi") .setContentTitle("帅气的宝藏博主") .setContentText("ShadyPi") .setSmallIcon(R.drawable.ic_baseline_all_inclusive_24) .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.image)) .build();

在这里插入图片描述

设置小图标颜色,注意先转成int:

.setColor(Color.parseColor("#ff0000"))//设置小图标颜色

设置点击后取消通知:

.setAutoCancel(true)

设置通知被创建的时间,一般通知都显示弹出的时间(即当前时间),这是默认的。除非有特殊需求,否则不用专门设置。

添加点击事件

在MainActivity同一文件夹下创建点击动作NotificationActivity.java:

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("ShadyPi","点击通知"); } }

编写完以后鼠标点中类名NotificationActivity,用快捷键Alt+Enter选择弹出的第一个选项注册add to manifest。

写好该动作后,在MainActivity中声明点击动作要求的PendingIntent变量,然后就可以设置了:

Intent intent=new Intent(this,NotificationActivity.class); PendingIntent pending=PendingIntent.getActivity(this,0,intent,0); note=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();

在这里插入图片描述在这里插入图片描述

点击后可以在调试日志中看到输出,说明点击动作被执行了,同时点击后消息通知自动消失了。 在这里插入图片描述

取消消息

除了设置点击自动取消,还可以手动取消消息,只需要用cancel(int id)就可以取消掉相应id的消息。

public void cancel(View view) { manager.cancel(1); }

MainActivity完整代码:

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; @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("ShadyPi","ShadyPi的博客针不戳", NotificationManager.IMPORTANCE_HIGH); manager.createNotificationChannel(channel); } Intent intent=new Intent(this,NotificationActivity.class); PendingIntent pending=PendingIntent.getActivity(this,0,intent,0); note=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); } }


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有