Android Studio App开发之通知推送Notification的讲解及实战(给用户推送信息实战)

这篇具有很好参考价值的文章主要介绍了Android Studio App开发之通知推送Notification的讲解及实战(给用户推送信息实战)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

运行有问题或需要全部资源请点赞关注收藏后评论区留言~~~

一、通知推送Ntification

在APP的运行过程中,为了让用户及时收到某些消息,有必要由App主动向用户推送消息通知,以免错过有价值的信息。

在手机屏幕的顶端下拉会弹出通知栏,里面存放的便是App主动推给用户的提醒消息,消息通知的组成内容由Notification类所描述,每条消息通知都有消息图标,消息标题,消息内容等基本元素,时不时还有附加文本,进度条,计时器等额外元素。

并且Notification仅仅描述了消息通知的组成内容,实际推送工作还需要由通知管理器NotificationManager执行。

下面以发送简单消息为例 效果如下,如果你连接了真机则手机上会弹出所输入信息的弹框

Android Studio App开发之通知推送Notification的讲解及实战(给用户推送信息实战)

 Android Studio App开发之通知推送Notification的讲解及实战(给用户推送信息实战)

代码如下

Java类 

package com.example.chapter11;

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;

import androidx.appcompat.app.AppCompatActivity;

import com.example.chapter11.util.ViewUtil;

public class NotifySimpleActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText et_title;
    private EditText et_message;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notify_simple);
        et_title = findViewById(R.id.et_title);
        et_message = findViewById(R.id.et_message);
        findViewById(R.id.btn_send_simple).setOnClickListener(this);
    }

    @Override
    public 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, MainActivity.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);
    }

}

XML文件

<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" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="消息标题:"
            android:textColor="@color/black"
            android:textSize="17sp" />

        <EditText
            android: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>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="消息内容:"
            android:textColor="@color/black"
            android:textSize="17sp" />

        <EditText
            android: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>

    <Button
        android: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>

二、包含计时器与进度条

如果消息通知包含计时器与进度条,则需要调用消息建造器的setUsesChronometer与setProgress方法,效果如下

包含进度条不仅更加美观而且让用户能实时看见App的进展情况

Android Studio App开发之通知推送Notification的讲解及实战(给用户推送信息实战)

 代码如下

Java类

package com.example.chapter11;

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;

import androidx.appcompat.app.AppCompatActivity;

import com.example.chapter11.util.ViewUtil;

public class NotifyCounterActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText et_title;
    private EditText et_message;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notify_counter);
        et_title = findViewById(R.id.et_title);
        et_message = findViewById(R.id.et_message);
        findViewById(R.id.btn_send_counter).setOnClickListener(this);
    }

    @Override
    public 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, MainActivity.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);
    }

}

XML文件

<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" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="消息标题:"
            android:textColor="@color/black"
            android:textSize="17sp" />

        <EditText
            android: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>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="消息内容:"
            android:textColor="@color/black"
            android:textSize="17sp" />

        <EditText
            android: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>

    <Button
        android: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>

创作不易 觉得有帮助请点赞关注收藏~~~文章来源地址https://www.toymoban.com/news/detail-403501.html

到了这里,关于Android Studio App开发之通知推送Notification的讲解及实战(给用户推送信息实战)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【android开发-20】android中notification的用法讲解

    1, notification的基本用法 在Android中,通知(Notification)是一种在状态栏上显示的消息提示,用户点击通知后可以展开更多详细信息。以下是基本的通知用法: 1,创建通知 创建通知需要使用Notification类,可以通过以下代码创建一个简单的通知: 2,创建通知通道 从Android 8.0开

    2024年02月04日
    浏览(40)
  • Android Studio App开发之通知渠道NotificationChannel及给华为、小米手机桌面应用添加消息数量角标实战(包括消息重要级别的设置 附源码)

    需要全部源码或运行有问题请点赞关注收藏后评论区留言~~~ 为了分清消息通知的轻重缓急,Android8.0新增了通知渠道,并且必须指定通知渠道才能正常推送消息,一个应用允许拥有多个通知渠道,每个渠道的重要性各不相同,有的渠道消息在通知栏被折叠成小行,有的渠道消

    2024年02月16日
    浏览(48)
  • Android 之 Notification (状态栏通知)详解

    本节带来的是Android中用于在状态栏显示通知信息的控件:Notification,相信大部分 学Android都对他都很熟悉,而网上很多关于Notification的使用教程都是基于2.x的,而 现在普遍的Android设备基本都在4.x以上,甚至是5.0以上的都有;他们各自的Notification 都是不一样的!而本节给大家

    2024年02月15日
    浏览(35)
  • Android Studio App开发之网络通信中使用GET方式调用HTTP接口的讲解及实战(附源码 超详细必看)

    运行有问题或需要源码请点赞关注收藏后评论区留言~~~ Android开发采用Java作为编程语言,也就沿用了Java的HTTP连接工具HttpURLConnection,不管是访问HTTP接口还是上传或下载文件都是用它来实现。它有几个关键点 1:HttpURLConnection默认采取国际通行的UTF-8编码,中文用GBK编码 2:多数

    2024年02月05日
    浏览(103)
  • flutter开发实战-实现推送功能Push Notification

    flutter开发实战-实现推送功能Push Notification 推送服务现在可以说是所有 App 的标配了,最近在Flutter工程项目上实现推送功能。flutter上实现推送功能需要依赖原生的功能,需要插件实现,这里使用的是极光推送的服务。 效果图如下 在使用极光推送功能时,需要使用的是极光提

    2024年02月16日
    浏览(40)
  • 使用Android Studio 利用极光推送SDK 制作手机 APP 实现远程测试技术 (第一部)

    总参考文章:https://blog.csdn.net/qq_38436214/article/details/105073213 Android Studio 安装配置教程 - Windows(详细版) 1.JDK 安装与环境变量配置(Win10详细版) 《jdk-8u371-windows-i586.exe》 https://blog.csdn.net/qq_38436214/article/details/105071088 此时会让登录账号密码: https://login.oracle.com/mysso/signon.jsp 账号:

    2024年02月03日
    浏览(37)
  • uni-app Android studio 本地打包 【图文讲解】

    需要修改文件列表 appsrcmainresvaluesstrings.xml 修改app名称 appsrcmainresvaluesAndroidManifest.xml 修改 包名 以及 uni-app 开发者后台生成的 离线打包可key simpleDemo 目录下的 准备安卓开发环境 (这里忽略,之前有文章讲解) 安卓开发环境 https://nativesupport.dcloud.net.cn/AppDocs/download/an

    2024年02月03日
    浏览(64)
  • android.app.RemoteServiceException: Bad notification for startForeground

    异常如下: 解决方法是 在 Android 8.0上 创建一个 NotificationChannel,代码如下 2 Android 中的 Service Service 是一种可在后台执行长时间运行操作而不提供界面的应用组件。 服务可由其他应用组件启动,而且即使用户切换到其他应用,服务仍将在后台继续运行。 此外,组件可通过绑

    2024年02月12日
    浏览(47)
  • Android Studio App入门之列表视图ListView的讲解及实战(附源码 超详细必看)

    需要图片集或全部源码请点赞关注收藏后评论区留言~~ 如果想在页面上直接显示全部列表信息,就要引入新的列表视图ListView,列表视图允许在页面上分行展示相似的数据列表。 列表视图新增的属性与方法如下 divider 指定分割线的图形 dividerHeight 指定分割线的高度 listSelecto

    2023年04月22日
    浏览(38)
  • android studio开发app实例

    以下是一个简单的Android Studio开发App的实例: 1. 打开Android Studio,并创建一个新项目。 2. 选择一个适当的应用程序名称和包名称,然后选择目标API级别和默认Activity的模板。 3. 在MainActivity.java文件中,添加以下代码以配置MainActivity: ``` import android.os.Bundle; import androidx.appcompa

    2024年02月16日
    浏览(43)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包