通知未显示在屏幕上且不存在错误
The notification is not displayed on the screen and not exist errors
请帮帮我!!!
我正在为明天的学校执行一个项目,我找不到解决这个问题的方法,因为 logcat
没有给出错误。
我正在创建一个可以创建提醒的应用程序。提醒已创建并保存在数据库中,但通知不会显示在屏幕上。
下面是我们创建的 ReminderAlarmService
Class 的代码,用于设置提醒和通知,通知部分在最后。
我想知道为什么没有显示通知,即使我检查了数据库并且提醒存储在那里。
以下代码对应我创建通知的地方:
package com.example.projetolinguagensprogramacao1.Lembretes;
import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.provider.Settings;
import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import androidx.core.app.TaskStackBuilder;
import com.example.projetolinguagensprogramacao1.R;
public class ReminderAlarmService extends IntentService {
private static final String TAG = ReminderAlarmService.class.getSimpleName();
private static final int NOTIFICATION_ID = 42;
//This is a deep link intent, and needs the task stack
public static PendingIntent getReminderPendingIntent(Context context, Uri uri) {
Intent action = new Intent(context, ReminderAlarmService.class);
action.setData(uri);
return PendingIntent.getService(context, 0, action, PendingIntent.FLAG_UPDATE_CURRENT);
}
public ReminderAlarmService() {
super(TAG);
}
@Override
protected void onHandleIntent(@Nullable Intent intent) {
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Uri uri = intent.getData();
//Display a notification to view the task details
Intent action = new Intent(this, AddReminderActivity.class);
action.setData(uri);
PendingIntent operation = TaskStackBuilder.create(this)
.addNextIntentWithParentStack(action)
.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
//Grab the task description
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
String description = "";
try {
if (cursor != null && cursor.moveToFirst()) {
description = AlarmReminderContract.getColumnString(cursor, AlarmReminderContract.AlarmReminderEntry.KEY_TITLE);
}
} finally {
if (cursor != null) {
cursor.close();
}
}
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setContentTitle(getString(R.string.reminder_title))
.setContentText(description)
.setSmallIcon(R.drawable.ic_add_alert_black_24dp)
.setContentIntent(operation)
.setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 })
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
.setAutoCancel(true)
.setContentIntent(operation);
manager.notify(1, notificationBuilder.build());
}
}
从 Android 8.0(API 级别 26)开始,通知将不会显示,除非您提供 NotificationChannel
作为 NotificationCompat.Builder
构造函数的第二个参数。
所以,要解决这个问题,请创建一个 NotificationChannel
;我将ID设置为"PRIMARY_CHANNEL_ID"如下
// Notification channels are only available in OREO and higher.
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel
("PRIMARY_CHANNEL_ID",
"Service",
NotificationManager.IMPORTANCE_HIGH);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.enableVibration(true);
notificationChannel.setDescription("Description");
manager.createNotificationChannel(notificationChannel);
}
使用频道 ID "PRIMARY_CHANNEL_ID" 作为 NotificationCompat.Builder
构造函数的第二个参数。
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "PRIMARY_CHANNEL_ID")
.setContentTitle(getString(R.string.reminder_title))
.setContentText(description)
.setSmallIcon(R.drawable.ic_add_alert_black_24dp)
.setContentIntent(operation)
.setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 })
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
.setAutoCancel(true)
.setContentIntent(operation);
请帮帮我!!!
我正在为明天的学校执行一个项目,我找不到解决这个问题的方法,因为 logcat
没有给出错误。
我正在创建一个可以创建提醒的应用程序。提醒已创建并保存在数据库中,但通知不会显示在屏幕上。
下面是我们创建的 ReminderAlarmService
Class 的代码,用于设置提醒和通知,通知部分在最后。
我想知道为什么没有显示通知,即使我检查了数据库并且提醒存储在那里。
以下代码对应我创建通知的地方:
package com.example.projetolinguagensprogramacao1.Lembretes;
import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.provider.Settings;
import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import androidx.core.app.TaskStackBuilder;
import com.example.projetolinguagensprogramacao1.R;
public class ReminderAlarmService extends IntentService {
private static final String TAG = ReminderAlarmService.class.getSimpleName();
private static final int NOTIFICATION_ID = 42;
//This is a deep link intent, and needs the task stack
public static PendingIntent getReminderPendingIntent(Context context, Uri uri) {
Intent action = new Intent(context, ReminderAlarmService.class);
action.setData(uri);
return PendingIntent.getService(context, 0, action, PendingIntent.FLAG_UPDATE_CURRENT);
}
public ReminderAlarmService() {
super(TAG);
}
@Override
protected void onHandleIntent(@Nullable Intent intent) {
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Uri uri = intent.getData();
//Display a notification to view the task details
Intent action = new Intent(this, AddReminderActivity.class);
action.setData(uri);
PendingIntent operation = TaskStackBuilder.create(this)
.addNextIntentWithParentStack(action)
.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
//Grab the task description
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
String description = "";
try {
if (cursor != null && cursor.moveToFirst()) {
description = AlarmReminderContract.getColumnString(cursor, AlarmReminderContract.AlarmReminderEntry.KEY_TITLE);
}
} finally {
if (cursor != null) {
cursor.close();
}
}
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setContentTitle(getString(R.string.reminder_title))
.setContentText(description)
.setSmallIcon(R.drawable.ic_add_alert_black_24dp)
.setContentIntent(operation)
.setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 })
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
.setAutoCancel(true)
.setContentIntent(operation);
manager.notify(1, notificationBuilder.build());
}
}
从 Android 8.0(API 级别 26)开始,通知将不会显示,除非您提供 NotificationChannel
作为 NotificationCompat.Builder
构造函数的第二个参数。
所以,要解决这个问题,请创建一个 NotificationChannel
;我将ID设置为"PRIMARY_CHANNEL_ID"如下
// Notification channels are only available in OREO and higher.
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel
("PRIMARY_CHANNEL_ID",
"Service",
NotificationManager.IMPORTANCE_HIGH);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.enableVibration(true);
notificationChannel.setDescription("Description");
manager.createNotificationChannel(notificationChannel);
}
使用频道 ID "PRIMARY_CHANNEL_ID" 作为 NotificationCompat.Builder
构造函数的第二个参数。
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "PRIMARY_CHANNEL_ID")
.setContentTitle(getString(R.string.reminder_title))
.setContentText(description)
.setSmallIcon(R.drawable.ic_add_alert_black_24dp)
.setContentIntent(operation)
.setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 })
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
.setAutoCancel(true)
.setContentIntent(operation);