Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 通知不显示在屏幕上,并且不存在错误_Java_Android_Sqlite_Notifications - Fatal编程技术网

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

Java 通知不显示在屏幕上,并且不存在错误,java,android,sqlite,notifications,Java,Android,Sqlite,Notifications,请帮帮我 我正在为学校执行一个明天的项目,我找不到如何解决这个问题,因为logcat没有给出错误 我正在创建一个可以创建提醒的应用程序。提醒已创建并保存在数据库中,但通知不会显示在屏幕上。 下面是我们为设置提醒和通知而创建的rementeralmservice类的代码,通知部分在最后 我想知道,尽管我检查了数据库并将提醒存储在那里,但为什么没有显示通知 以下代码对应于我创建通知的位置: package com.example.projetolinguagensprogramacao1.Lembr

请帮帮我
我正在为学校执行一个明天的项目,我找不到如何解决这个问题,因为
logcat
没有给出错误

我正在创建一个可以创建提醒的应用程序。提醒已创建并保存在数据库中,但通知不会显示在屏幕上。 下面是我们为设置提醒和通知而创建的
rementeralmservice
类的代码,通知部分在最后

我想知道,尽管我检查了数据库并将提醒存储在那里,但为什么没有显示通知

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

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)开始,除非您向
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);
因此,要解决这个问题,创建一个
通知频道
;我将把ID设置为“主通道ID”,如下所示

使用通道ID“主通道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);

谢谢你,你设法解决了我的错误,我真的不知道,因为我开始在安卓工作室工作。