通知与服务——消息通知——通知推送Notification
手机通知栏存放的是App主动推给用户的提醒消息,每条消息包括消息图标、消息标题、消息内容等,这些消息元素由通知建造器Notification.Builder设定。
常用方法如下:
setSmallIcon:设置应用名称左边的小图标。setLargeIcon:设置通知栏右边的大图标。setContentTitle:设置通知栏的标题文本。setContentText:设置通知栏的内容文本。setSubText:设置通知栏的附加文本,它位于应用名称右边。setProgress:设置进度条并显示当前进度。setUsesChronometer:设置是否显示计时器setContentIntent:设置通知内容的延迟意图,点击通知时触发该意图。build:构建通知。
获得Notification消息对象之后,还要由通知管理器NotificationManager推送消息。
通知管理器的常用方法如下。
notify:把指定消息推送到通知栏。cancel:取消指定的消息通知。cancelAll:取消所有的消息通知。createNotificationChannel:创建指定的通知渠道。getNotificationChannel:获取指定编号的通知渠道。
==============================================================================================
布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:padding="5dp" ><LinearLayoutandroid:layout_width="match_parent"android:layout_height="50dp"android:orientation="horizontal"><TextViewandroid:layout_width="wrap_content"android:layout_height="match_parent"android:gravity="center"android:text="消息标题:"android:textColor="@color/black"android:textSize="17sp" /><EditTextandroid:id="@+id/et_title"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:layout_margin="5dp"android:background="@drawable/editext_selector"android:hint="请填写消息标题"android:textColor="@color/black"android:textSize="17sp" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="70dp"android:orientation="horizontal"><TextViewandroid:layout_width="wrap_content"android:layout_height="match_parent"android:gravity="center"android:text="消息内容:"android:textColor="@color/black"android:textSize="17sp" /><EditTextandroid:id="@+id/et_message"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:gravity="top"android:layout_margin="5dp"android:background="@drawable/editext_selector"android:hint="请填写消息内容"android:textColor="@color/black"android:textSize="17sp" /></LinearLayout><Buttonandroid:id="@+id/btn_send_simple"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center"android:text="发送简单消息"android:textColor="@color/black"android:textSize="17sp" /></LinearLayout>
代码;
package com.example.myapplication;import androidx.appcompat.app.AppCompatActivity;import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;public class MainActivity3 extends AppCompatActivity implements View.OnClickListener
{private EditText et_title;private EditText et_message;@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main3);et_title = findViewById(R.id.et_title);et_message = findViewById(R.id.et_message);findViewById(R.id.btn_send_simple).setOnClickListener(this);}@Overridepublic void onClick(View v){if (v.getId() == R.id.btn_send_simple){ViewUtil.hideOneInputMethod(this, et_message); // 隐藏输入法软键盘if (TextUtils.isEmpty(et_title.getText())){Toast.makeText(this, "请填写消息标题", Toast.LENGTH_SHORT).show();return;}if (TextUtils.isEmpty(et_message.getText())){Toast.makeText(this, "请填写消息内容", Toast.LENGTH_SHORT).show();return;}String title = et_title.getText().toString();String message = et_message.getText().toString();sendSimpleNotify(title, message); // 发送简单的通知消息}}// 发送简单的通知消息(包括消息标题和消息内容)private void sendSimpleNotify(String title, String message){// 发送消息之前要先创建通知渠道,创建代码见MainApplication.java// 创建一个跳转到活动页面的意图Intent clickIntent = new Intent(this, MainActivity2.class);// 创建一个用于页面跳转的延迟意图PendingIntent contentIntent = PendingIntent.getActivity(this, R.string.app_name, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);// 创建一个通知消息的建造器Notification.Builder builder = new Notification.Builder(this);if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){// Android 8.0开始必须给每个通知分配对应的渠道builder = new Notification.Builder(this, getString(R.string.app_name));}builder.setContentIntent(contentIntent) // 设置内容的点击意图.setAutoCancel(true) // 点击通知栏后是否自动清除该通知.setSmallIcon(R.mipmap.ic_launcher) // 设置应用名称左边的小图标.setSubText("这里是副本") // 设置通知栏里面的附加说明文本.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_app)) // 设置通知栏右边的大图标.setContentTitle(title) // 设置通知栏里面的标题文本.setContentText(message); // 设置通知栏里面的内容文本Notification notify = builder.build(); // 根据通知建造器构建一个通知对象// 从系统服务中获取通知管理器NotificationManager notifyMgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);// 使用通知管理器推送通知,然后在手机的通知栏就会看到该消息notifyMgr.notify(R.string.app_name, notify);}}
============================================================================================================
布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:padding="5dp" ><LinearLayoutandroid:layout_width="match_parent"android:layout_height="50dp"android:orientation="horizontal"><TextViewandroid:layout_width="wrap_content"android:layout_height="match_parent"android:gravity="center"android:text="消息标题:"android:textColor="@color/black"android:textSize="17sp" /><EditTextandroid:id="@+id/et_title"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:layout_margin="5dp"android:background="@drawable/editext_selector"android:hint="请填写消息标题"android:textColor="@color/black"android:textSize="17sp" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="70dp"android:orientation="horizontal"><TextViewandroid:layout_width="wrap_content"android:layout_height="match_parent"android:gravity="center"android:text="消息内容:"android:textColor="@color/black"android:textSize="17sp" /><EditTextandroid:id="@+id/et_message"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:gravity="top"android:layout_margin="5dp"android:background="@drawable/editext_selector"android:hint="请填写消息内容"android:textColor="@color/black"android:textSize="17sp" /></LinearLayout><Buttonandroid:id="@+id/btn_send_counter"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center"android:text="发送计时消息"android:textColor="@color/black"android:textSize="17sp" /></LinearLayout>
代码:
package com.example.myapplication;import androidx.appcompat.app.AppCompatActivity;import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;public class MainActivity4 extends AppCompatActivity implements View.OnClickListener
{private EditText et_title;private EditText et_message;@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main4);et_title = findViewById(R.id.et_title);et_message = findViewById(R.id.et_message);findViewById(R.id.btn_send_counter).setOnClickListener(this);}@Overridepublic void onClick(View v){if (v.getId() == R.id.btn_send_counter){ViewUtil.hideOneInputMethod(this, et_message); // 隐藏输入法软键盘if (TextUtils.isEmpty(et_title.getText())){Toast.makeText(this, "请填写消息标题", Toast.LENGTH_SHORT).show();return;}if (TextUtils.isEmpty(et_message.getText())){Toast.makeText(this, "请填写消息内容", Toast.LENGTH_SHORT).show();return;}String title = et_title.getText().toString();String message = et_message.getText().toString();sendCounterNotify(title, message); // 发送计时的通知消息}}// 发送计时的通知消息private void sendCounterNotify(String title, String message){// 发送消息之前要先创建通知渠道,创建代码见MainApplication.java// 创建一个跳转到活动页面的意图Intent cancelIntent = new Intent(this, MainActivity2.class);// 创建一个用于页面跳转的延迟意图PendingIntent deleteIntent = PendingIntent.getActivity(this, R.string.app_name, cancelIntent, PendingIntent.FLAG_UPDATE_CURRENT);// 创建一个通知消息的建造器Notification.Builder builder = new Notification.Builder(this);if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){// Android 8.0开始必须给每个通知分配对应的渠道builder = new Notification.Builder(this, getString(R.string.app_name));}builder.setDeleteIntent(deleteIntent) // 设置内容的清除意图.setSmallIcon(R.mipmap.ic_launcher) // 设置应用名称左边的小图标.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_app)) // 设置通知栏右边的大图标.setProgress(100, 60, false) // 设置进度条及其具体进度.setUsesChronometer(true) // 设置是否显示计时器.setContentTitle(title) // 设置通知栏里面的标题文本.setContentText(message); // 设置通知栏里面的内容文本Notification notify = builder.build(); // 根据通知建造器构建一个通知对象// 从系统服务中获取通知管理器NotificationManager notifyMgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);// 使用通知管理器推送通知,然后在手机的通知栏就会看到该消息notifyMgr.notify(R.string.app_name, notify);}}
===================================================================================================
通知与服务——消息通知——通知推送Notification相关推荐
- Android开发笔记(五十二)通知推送Notification
PendingIntent 准备工作复习一下PendingIntent,前面的博文< Android开发笔记(五十)定时器AlarmManager>已经提到了它.PendingIntent ...
- .net平台借助第三方推送服务在推送Android消息(极光推送) 转
分类: .net外部工具接口(3) .net知识精华(29) 版权声明:本文为博主原创文章,未经博主允许不得转载. 最近做的.net项目(Windows Service)需要向Android手机发送推 ...
- 视频直播源码中关于服务端直播开播推送实现
在视频直播源码中直播app开播时需向客户推送开播消息通知用户,实现方式如下: 1.申请相应的推送服务三方,如下使用极光推送,获取相应的配置资料,并做好相应的配置 2.推送代码如下: /* 极光推送 * ...
- sse服务器推送性能,SSE 服务端向客户端推送
传统的ajax都是由客户端主动去请求,服务端才可以返回数据 而sse是建立一个通道,并且在断线后自动重连,由服务端去推送,不需要客户端去主动请求,只需要建立通道 websocket是双向通信 客户端可 ...
- Python Web实时消息后台服务器推送技术---GoEasy
越来越多的项目需要用到实时消息的推送与接收,怎样实现最方便呢?我这里推荐大家使用GoEasy,它是一款第三方推送服务平台,使用它的API可以轻松搞定实时推送! 浏览器兼容性:GoEasy推送 支持we ...
- C# Web实时消息后台服务器推送技术---GoEasy
越来越多的项目需要用到实时消息的推送与接收,怎样实现最方便呢?我这里推荐大家使用GoEasy, 它是一款第三方推送服务平台,使用它的API可以轻松搞定实时推送! 浏览器兼容性:GoEasy推送 支持w ...
- ASP.NET Web实时消息后台服务器推送技术---GoEasy
越来越多的项目需要用到实时消息的推送与接收,怎样用ASP.NET实现最方便呢?我这里推荐大家使用GoEasy, 它是一款第三方推送服务平台,使用它的API可以轻松搞定实时推送! 浏览器兼容性:GoEa ...
- C# Web实时消息后台服务器推送技术-GoEasy
越来越多的项目需要用到实时消息的推送与接收,怎样用C#实现最方便呢?我这里推荐大家使用GoEasy, 它是一款第三方推送服务平台,使用它的API可以轻松搞定实时推送! 浏览器兼容性:GoEasy推送 ...
- Ruby Web实时消息后台服务器推送技术---GoEasy
越来越多的项目需要用到实时消息的推送与接收,怎样实现最方便呢?我这里推荐大家使用GoEasy,它是一款第三方推送服务平台,使用它的API可以轻松搞定实时推送! 浏览器兼容性:GoEasy推送 支持we ...
- .NET Web实时消息后台服务器推送技术-GoEasy
越来越多的项目需要用到实时消息的推送与接收,怎样用.NET实现最方便呢?我这里推荐大家使用GoEasy, 它是一款第三方推送服务平台,使用它的API可以轻松搞定实时推送! 浏览器兼容性:GoEasy推 ...
最新文章
- SERU最佳需求分析方法
- 存储过程分页,按多条件排序(原创)
- 什么是MySQL,以及它的特性
- 小余学调度:学习记录(2022.4)
- 第3章:Kubernetes监控与日志管理
- python 字典和列表的遍历
- 系统学习深度学习(四十)--基于模拟的搜索与蒙特卡罗树搜索(MCTS)
- 随机数------选双色球
- RHEL6.4 NFS文件共享服务搭建
- Android 消息通知栏用法详解(一)
- 30行代码实现蚂蚁森林自动偷能量
- 具体怎么使用视频播放屏幕的感重力切换横竖屏
- 电商系统,剖析商品模块中商品表(spu)、规格表(sku)的数据库是如何设计的
- 导师喜欢什么样的“真”研究生?(转科学网)
- [NameError]: name ‘F’ is not defined
- mysql查询学生表的总人数_mysql数据库查询练习
- 【今日CV 计算机视觉论文速览 第128期】Mon, 10 Jun 2019
- 前端多页面的代码压缩和混淆
- 在uniapp里面使用阿里矢量图标(iconfont)
- 4.单片机之静态数码管和动态数码管