java - 服务未显示通知

标签 java android service notifications

我搜索了与该主题相关的许多其他问题,但没有找到令人满意的答案,而且没有一个对我有用。 我想显示一个只能由应用程序终止的连续通知。但我写的代码几天前还可以工作,但现在不行了。

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 中没有记录任何异常。该代码是在service的onCreate中调用的。该服务正在正确启动,我可以在 Log cat 中看到也没有异常,但未显示通知。我的操作系统是 Android ONE for nokia (PI)

最佳答案

您正在使用已弃用的 NotificationCompat.Builder 构造函数,该构造函数采用单个参数(上下文);从 Android 8.0(API 级别 26)开始,这将不起作用。

所以,要解决这个问题:

第 1 步:创建 Notification channel使用 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类及其双参数构造函数,该构造函数将第二个参数作为您在第一步中分配的 channel 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);

关于java - 服务未显示通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60786552/

相关文章:

java - 扩展 SimpleJpaRepository

java - 正确地将 JSON 数据发布到 Restful Web 服务 NetBeans

堆内存超过阈值后的 Java 垃圾收集

android - 如何将多个 project_number/sender id 放入 google-services.json

Android 服务对位置更改执行操作

Android:用于后台处理的组件,必须手动启动

java - ArrayList<String> 使用改造 android 解析为 recyclerview

android - 不幸的是,我的应用程序停止了,我该怎么办?

android - 自定义命名空间支持库 ActionBar?

linux - 如何从文件重定向密码请求?