今天在学习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");                }

运行效果图: 

 

更多相关文章

  1. android 用户注册demo
  2. Android=》Nitification
  3. android Notification 工具类
  4. Android(安卓)各种机型兼容问题
  5. Android(安卓)Broadcast 用法简单讨论
  6. Android(安卓)如何破解兼容性困局
  7. 小编程(三):用户登录注册界面开发及用户信息管理案例代码详解
  8. Android(安卓)开机启动应用
  9. Android设计(起步篇)

随机推荐

  1. Android中用Handle做定时器,显示实时时间
  2. Android(安卓)获取View高度的4种方法
  3. Firefox mobile (android) and orientati
  4. android 利用pull解析xml数据
  5. TabLayout 遇到那些坑
  6. (Android) ContentProvider 实例
  7. Fastjson生成json时Null属性不显示
  8. android JB2连拍降速原理介绍
  9. Android(安卓)jni 编程(参数的传递,成员,方
  10. Android(安卓)保存图片到SQLite