通知未显示在屏幕上,并且不存在错误

时间:2020-06-07 22:33:36

标签: java android sqlite notifications

请帮助我!!!
我正在为学校的明天进行一个项目,但找不到解决方法,因为logcat不会出错。

我正在创建一个可以创建提醒的应用。提醒已创建并保存在数据库中,但通知未显示在屏幕上。 下面是我们创建的ReminderAlarmService类的代码,用于设置提醒和通知,通知部分位于末尾。

我想知道,即使我检查了数据库并将提醒存储在其中,为什么也没有显示通知。

以下代码与我创建通知的位置相对应:

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());
    }
}

1 个答案:

答案 0 :(得分:0)

从Android 8.0(API级别26)开始,除非向NotificationCompat.Builder构造函数提供NotificationChannel作为第二个参数,否则不会显示通知。

因此,要解决此问题,请创建一个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);