Android中使用Notification在通知栏中显示通知

生活中,最使人疲惫的往往不是道路的遥远,而是心中的郁闷;最使人痛苦的往往不是生活的不幸,而是希望的破灭;最使人颓废的往往不是前途的坎坷,而是自信的丧失;最使人绝望的往往不是挫折的打击,而是心灵的死亡。所以我们要有自己的梦想,让梦想的星光指引着我们走出落漠,走出惆怅,带着我们走进自己的理想。

导读:本篇文章讲解 Android中使用Notification在通知栏中显示通知,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

场景

App在接收到后台推送的消息后,需要在系统通知栏中显示通知消息,并且点击通知消息跳转到新的页面,并将消息内容传递过去。

效果如下

 

Android中使用Notification在通知栏中显示通知

注:

博客:
https://blog.csdn.net/badao_liumang_qizhi
关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。

实现

首先在主页面添加一个按钮

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/showNotice"
        android:text="显示通知"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</RelativeLayout>

然后在activity中设置按钮的点击事件

    private Button showNoticeButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        showNoticeButton = findViewById(R.id.showNotice);
        showNoticeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showNotification(MainActivity.this, "通知内容:关注公众号,编程资料与技术共享学习");
            }
        });
    }

在点击事件中调用了方法showNotification并且传递了消息的内容,在方法showNotification中

    public void showNotification(Context context, String content) {
        //1.创建通知管理器
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        NotificationCompat.Builder builder;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {//Android 8.0版本适配
            NotificationChannel channel = new NotificationChannel("default", "default", NotificationManager.IMPORTANCE_HIGH);
            notificationManager.createNotificationChannel(channel);
            builder = new NotificationCompat.Builder(context, "default");
        } else {
            builder = new NotificationCompat.Builder(context);
        }
        //用来进行跳转并传递消息的intent
        Intent intent = new Intent(this, NoticeActivity.class);
        intent.putExtra("content",content);
        //2.创建通知实例
        Notification notification = builder
                .setContentTitle("通知标题:公众号-霸道的程序猿")
                .setContentText(content)
                .setWhen(System.currentTimeMillis())
                //smallIcon 通知栏显示小图标
                //android5.0 之后通知栏图标都修改了,小图标不能含有RGB图层,也就是说图片不能带颜色,否则显示的就成白色方格了
                //解决方法一:为图片带颜色,targetSdkVersion改为21以下
                //解决方法二:只能用白色透明底的图片
                .setSmallIcon(R.mipmap.ic_launcher)
                //LargeIcon 下拉后显示的图标
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
                //收到通知时的效果,这里是默认声音
                .setDefaults(Notification.DEFAULT_SOUND)
                .setAutoCancel(true)
                .setContentIntent(PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT))
                .build();
        //3.notify
        //notifyId每次要不一致,不然下一次的通知会覆盖上一次
        int notifyId = new Random().nextInt();
        notificationManager.notify(notifyId, notification);
    }

首先创建通知管理器,然后创建通知实例,在创建通知实例时传递了一个Intent对象用来进行点击通知消息的跳转,通过

        //用来进行跳转并传递消息的intent
        Intent intent = new Intent(this, NoticeActivity.class);
        intent.putExtra("content",content);

传递要显示的消息内容

然后新建一个Activity叫NoticeActivity用来实现点击通知后跳转显示消息内容。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".NoticeActivity">

    <TextView
        android:id="@+id/content_TextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</RelativeLayout>

添加一个TextView用来显示消息内容

然后在Activity中

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notice);
        String content = getIntent().getStringExtra("content");
        TextView textView = findViewById(R.id.content_TextView);
        textView.setText("消息内容:\n"+content);
    }

获取传递的参数并显示消息内容。

效果

点击显示通知按钮

 

Android中使用Notification在通知栏中显示通知

点击通知内容

 

Android中使用Notification在通知栏中显示通知

 

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/136427.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!