Solution 1 :
Starting from Android 8.0 (API level 26), notifications won’t show unless you provide NotificationChannel
as a second argument to the NotificationCompat.Builder
constructor.
So, to solve that, create a NotificationChannel
; I will set the ID to “PRIMARY_CHANNEL_ID” as below
// 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);
}
use the channel ID “PRIMARY_CHANNEL_ID” as a second argument to the NotificationCompat.Builder
constructor.
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);
Problem :
Please help me!!!
I’m carrying out a project for the school for tomorrow and I can’t find how to solve this because the logcat
does not give an error.
I’m creating an app where I can create reminders. The reminder is created and saved in the database but notification is not displayed on the screen.
Below is the code for the ReminderAlarmService
Class we created in order to set up the reminder but also the notification, the notification part is at the very end.
I want to know, why the notifications aren’t being displayed, even though I checked the database and the reminders are stored there.
The following code corresponds to where I created the notification:
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());
}
}
Comments
Comment posted by Eduardo Neves
Thank you, you managed to solve my mistake, I didn’t really know that because I started working a little with android studio.