服务中未显示通知
Notification not Showing from Service
我搜索了许多与此主题相关的其他问题,但没有找到令人满意的答案,其中 none 对我有用。
我想显示一个只能由应用程序终止的连续通知。但是我写的代码几天前可以用,现在不行了。
private void GenNotification(String title, String body)
{
try
{
Log.i(Config.TAGWorker, "Generating Notification . . .");
Intent myIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(
this,
0,
myIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle(title)
.setContentText(body)
.setChannelId("myID")
.setTicker("Notification!")
.setWhen(System.currentTimeMillis())
.setContentIntent(pendingIntent)
.setDefaults(Notification.DEFAULT_SOUND)
.setAutoCancel(false)
.setSmallIcon(R.drawable.floppy)
.setOngoing(true)
.build();
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Service.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, notification);
}
catch (Exception e)
{
Log.e(Config.TAGWorker, e.getMessage());
}
}
Logcat中没有关于此的异常记录。该代码在服务的 onCreate 中调用。该服务正在正确启动我可以在 Log cat 中看到也没有异常但未显示通知。我的 OS 是 Android ONE for nokia (PI)
您是否检查过您的字符串(标题和 body)是否为空,如果它为空通知将不会显示
如果您的 android 高于 7.0,还要检查您每次启动服务时是否调用通知渠道
当你回忆起它时清除通知在你的情况下相同的 id 是 1.
您正在使用已弃用的 NotificationCompat.Builder
构造函数,该构造函数采用单个参数(上下文);从 Android 8.0(API 级别 26)开始,这将不起作用。
所以,要解决这个问题:
第 1 步: 使用 NotificationManager
创建一个 Notification channel
NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// 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");
mNotificationManager.createNotificationChannel(notificationChannel);
}
注意:根据需要更改参数值
步骤 2::使用未弃用的 Notification.Builder
class 及其双参数构造函数,该构造函数采用第二个参数作为频道 ID你在第一步分配的,我把它设置为 "PRIMARY_CHANNEL_ID"
Notification notification = new NotificationCompat.Builder
(this, "PRIMARY_CHANNEL_ID")
.setContentTitle("title")
.setContentText("body")
.setTicker("Notification!")
.setWhen(System.currentTimeMillis())
.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.ic_launcher_background)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setOngoing(true)
.setDefaults(NotificationCompat.DEFAULT_ALL)
.setAutoCancel(true)
.build();
mNotificationManager.notify(0, notification);
我搜索了许多与此主题相关的其他问题,但没有找到令人满意的答案,其中 none 对我有用。 我想显示一个只能由应用程序终止的连续通知。但是我写的代码几天前可以用,现在不行了。
private void GenNotification(String title, String body)
{
try
{
Log.i(Config.TAGWorker, "Generating Notification . . .");
Intent myIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(
this,
0,
myIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle(title)
.setContentText(body)
.setChannelId("myID")
.setTicker("Notification!")
.setWhen(System.currentTimeMillis())
.setContentIntent(pendingIntent)
.setDefaults(Notification.DEFAULT_SOUND)
.setAutoCancel(false)
.setSmallIcon(R.drawable.floppy)
.setOngoing(true)
.build();
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Service.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, notification);
}
catch (Exception e)
{
Log.e(Config.TAGWorker, e.getMessage());
}
}
Logcat中没有关于此的异常记录。该代码在服务的 onCreate 中调用。该服务正在正确启动我可以在 Log cat 中看到也没有异常但未显示通知。我的 OS 是 Android ONE for nokia (PI)
您是否检查过您的字符串(标题和 body)是否为空,如果它为空通知将不会显示 如果您的 android 高于 7.0,还要检查您每次启动服务时是否调用通知渠道 当你回忆起它时清除通知在你的情况下相同的 id 是 1.
您正在使用已弃用的 NotificationCompat.Builder
构造函数,该构造函数采用单个参数(上下文);从 Android 8.0(API 级别 26)开始,这将不起作用。
所以,要解决这个问题:
第 1 步: 使用 NotificationManager
NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// 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");
mNotificationManager.createNotificationChannel(notificationChannel);
}
注意:根据需要更改参数值
步骤 2::使用未弃用的 Notification.Builder
class 及其双参数构造函数,该构造函数采用第二个参数作为频道 ID你在第一步分配的,我把它设置为 "PRIMARY_CHANNEL_ID"
Notification notification = new NotificationCompat.Builder
(this, "PRIMARY_CHANNEL_ID")
.setContentTitle("title")
.setContentText("body")
.setTicker("Notification!")
.setWhen(System.currentTimeMillis())
.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.ic_launcher_background)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setOngoing(true)
.setDefaults(NotificationCompat.DEFAULT_ALL)
.setAutoCancel(true)
.build();
mNotificationManager.notify(0, notification);