android - 警报未显示通知
问题描述
它仅在低于 26 的 API 上显示通知。它不显示高于 API 26 的通知。
我是新来的,我是否需要添加频道才能获得通知,如果需要如何添加频道。
我是编程新手,如果用完整的现有代码发布答案会有所帮助。这是我的 RingtonePlayingService.java,提前谢谢。
public class RingtonePlayingService extends Service {
private boolean isRunning;
private Context context;
MediaPlayer mMediaPlayer;
@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.e("MyActivity", "In service");
return null;
}
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
final NotificationManager mNM = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
Intent intent1 = new Intent(this.getApplicationContext(),
MainActivity2.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent1,
0);
Notification mNotify = new Notification.Builder(this)
.setContentTitle("WAKE UP" + "!")
.setContentText("No Alarm Needed My Passion Wakes Me")
.setSmallIcon(R.drawable.icon)
.setContentIntent(pIntent)
.setAutoCancel(true)
.build();
String state =
Objects.requireNonNull(intent.getExtras()).getString("extra");
Log.e("what is going on here ", state);
assert state != null;
switch (state) {
case "no":
startId = 0;
break;
case "yes":
startId = 1;
break;
default:
startId = 0;
break;
}
int startId1;
if(!this.isRunning && startId == 1) {
Log.e("if there was not sound ", " and you want start");
int min = 1;
int max = 5;
Random r = new Random();
int random_number = r.nextInt(max - min + 1) + min;
Log.e("random number is ", String.valueOf(random_number));
if (random_number == 1) {
mMediaPlayer = MediaPlayer.create(this,
R.raw.rins_3);
}
else if (random_number == 2) {
mMediaPlayer = MediaPlayer.create(this,
R.raw.rics_2);
}
else if (random_number == 3) {
mMediaPlayer = MediaPlayer.create(this,
R.raw.rics_1);
}
else if (random_number == 4) {
mMediaPlayer = MediaPlayer.create(this,
R.raw.rs_4);
}
else if (random_number == 5) {
mMediaPlayer = MediaPlayer.create(this,
R.raw.rics_5);
}
else {
mMediaPlayer = MediaPlayer.create(this,
R.raw.rs_1);
}
//mMediaPlayer = MediaPlayer.create(this,
R.raw.r_1);
mMediaPlayer.start();
Objects.requireNonNull(mNM).notify(0, mNotify);
this.isRunning = true;
startId1 = 0;
}
else if (!this.isRunning){
Log.e("if there was not sound ", " and you want end");
this.isRunning = false;
startId1 = 0;
}
else if (startId == 1){
Log.e("if there is sound ", " and you want start");
this.isRunning = true;
startId1 = 0;
}
else {
Log.e("if there is sound ", " and you want end");
mMediaPlayer.stop();
mMediaPlayer.reset();
this.isRunning = false;
startId1 = 0;
}
Log.e("MyActivity", "In the service");
return START_NOT_STICKY;
}
@Override
public void onDestroy() {
Log.e("JSLog", "on destroy called");
super.onDestroy();
this.isRunning = false;
}
}
解决方案
Android O 你需要创建通道 ID 并在创建通知构建器时传递通道 ID。
您需要创建类似这样的通知通道。
public void createNotificationChannel(NotificationManager manager, String channelID, String description) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (manager.getNotificationChannel(channelID) == null) {
NotificationChannel channel = new NotificationChannel(channelID, description,
NotificationManager.IMPORTANCE_LOW);
channel.setDescription(description);
manager.createNotificationChannel(channel);
}
}
}
在创建通知构建器时,您需要传递通道 ID
Notification mNotify = new Notification.Builder(this,<ChannelID>)
.setContentTitle("WAKE UP" + "!")
.setContentText("No Alarm Needed My Passion Wakes Me")
.setSmallIcon(R.drawable.icon)
.setContentIntent(pIntent)
.setAutoCancel(true)
.build();
mNM.notify(notificationId, mNotify);
推荐阅读
- java - 为什么 RandomAccessFile 无法打开,因为该文件正在被另一个进程使用?
- c++ - 不同操作系统/编译器之间的 C 风格字符串输出不一致
- javascript - 如何覆盖 React Native 组件?
- java - 有没有办法在 Java 中仅将扫描仪推进一个或 n 个字符?
- ruby-on-rails - stripe api 订阅取消退款金额大于费用
- date - 如何在 swiftUI 中显示 WheelPicker 上的所有旋转值,而不仅仅是选定的值?
- typescript - 在类型字符串上使用 += 时的简写形式 | 无效的?
- firebase - Firebase quata 垃圾邮件攻击
- javascript - 使用 JavaScript 将深度嵌套对象中的值打印为数组
- javascript - 如果用户离线,则提供本地文件