Android通知Notification学习 及 无法通知解决方案

今天在学习Android通知(Notification),根据书上的demo发现真机上并不能弹出,于是:

 

package com.example.notificationtest;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import java.io.File;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

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

        Button sendNotice = findViewById(R.id.send_notice);
        sendNotice.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.send_notice:
                // 构建意图
                Intent intent = new Intent(this, NotificationActivity.class);
                PendingIntent pi = PendingIntent.getActivities(this, 0, new Intent[]{intent}, 0);
                // 获取系统通知管理服务
                NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                // 构建 Notification
                Notification.Builder notification = new Notification.Builder(this)
                        .setContentTitle("这里是通知标题")
                        .setContentText("震惊!男人看了会沉默,女人看了会流泪")
                        .setWhen(System.currentTimeMillis())
                        .setSmallIcon(R.mipmap.play)
                        .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.play))
                        .setContentIntent(pi) // 跳转
                        .setAutoCancel(true) // 取消通知
                        .setSound(Uri.fromFile(new File("/system/media/audio/notifications/Simple.ogg"))) // 通知铃声
                        .setVibrate(new long[] {0, 1000, 1000, 1000}) // 通知时震动
                        .setLights(Color.GREEN, 1000, 1000) // LED灯
                        ;
                // 兼容  API 26,Android 8.0
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    // 第三个参数表示通知的重要程度,默认则只在通知栏闪烁一下
                    NotificationChannel notificationChannel = new NotificationChannel("AppTestNotificationId", "AppTestNotificationName", NotificationManager.IMPORTANCE_DEFAULT);
                    // 注册通道,注册后除非卸载再安装否则不改变
                    manager.createNotificationChannel(notificationChannel);
                    notification.setChannelId("AppTestNotificationId");
                }
                // 发出通知
                manager.notify(1, notification.build());
                break;
            default:
                break;
        }
    }
}

排查发现是因为我使用的真机是Android9.0系统,其中需要对API 26以上进行代码兼容 

// 兼容  API 26,Android 8.0
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    // 第三个参数表示通知的重要程度,默认则只在通知栏闪烁一下
                    NotificationChannel notificationChannel = new NotificationChannel("AppTestNotificationId", "AppTestNotificationName", NotificationManager.IMPORTANCE_DEFAULT);
                    // 注册通道,注册后除非卸载再安装否则不改变
                    manager.createNotificationChannel(notificationChannel);
                    notification.setChannelId("AppTestNotificationId");
                }

运行效果图: 

 

Android通知Notification学习 及 无法通知解决方案_第1张图片

你可能感兴趣的:(Android)