Android开发笔记——Android 9发送通知

发送通知

布局文件

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/send_notify"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="send_notify" />
</LinearLayout>

程序代码

package cn.fanchencloud.studyapplication;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    /**
     * 通知<p>id</p>
     */
    private static final int NOTIFY_ID = 0x123;

    private Button sendNoticeButton;
    // 通知相关
    // 渠道id
    private String channelId = "channelId1";
    private NotificationManager notificationManager;

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //新建通知管理器
        notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        addNotificationChannel();
        sendNoticeButton = findViewById(R.id.send_notify);
        sendNoticeButton.setOnClickListener(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.send_notify:
                Log.d("MainActivity", "点击按钮!");
                sendNotification("还不赶紧关注公众号", "点击查看详情!");
                break;
            default:
                break;
        }
    }

    private void sendNotification(String title, String content) {
        Notification notification;
        if (Build.VERSION.SDK_INT >= 26) {
            //当sdk版本大于26
            notification = new Notification.Builder(MainActivity.this, channelId)
                    .setCategory(Notification.CATEGORY_MESSAGE)
                    .setSmallIcon(R.mipmap.ic_launcher_copy)
                    .setContentTitle(title)
                    .setContentText(content)
                    .setAutoCancel(true)
                    .build();
        } else {
            //当sdk版本小于26
            notification = new NotificationCompat.Builder(MainActivity.this, channelId)
                    .setContentTitle("This is content title")
                    .setContentText("This is content text")
                    .setSmallIcon(R.mipmap.ic_launcher_copy)
                    .build();
        }
        notificationManager.notify(NOTIFY_ID, notification);
    }

    public void addNotificationChannel() {
        //当sdk版本大于26
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            //创建通知渠道
            CharSequence name = "fanchen_notify";
            String description = "fanchen的通知渠道";
            //重要性级别 这里用默认的
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel mChannel = null;
            mChannel = new NotificationChannel(channelId, name, importance);
            //渠道描述
            mChannel.setDescription(description);
            //是否显示通知指示灯
            mChannel.enableLights(true);
            //是否振动
            mChannel.enableVibration(true);
            //创建通知渠道
            notificationManager.createNotificationChannel(mChannel);
        }
    }
}

运行截图

运行截图

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值