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

场景

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

效果如下

 

注:

博客:
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);
    }

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

效果

点击显示通知按钮

 

点击通知内容

 

 

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

霸道流氓气质

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值