我正在尝试使用以下代码从服务创建通知:

NotificationCompat.Builder notif=new NotificationCompat.Builder(this,"ID1").setContentTitle("HI").setContentText("THERE");
NotificationManagerCompat man=(NotificationManagerCompat) getSystemService(NOTIFICATION_SERVICE);
man.notify("tag",notif);


但是我得到这个错误:


  可疑转换为NotificationManagerCompatNOTIFICATION_SERVICE:预期NotificationManager


我还尝试将NotificationManagerCompat更改为NotificationManager,这给了我这个错误:


  无法将'android.app.NotificationManager'中的'notify(int,android.app.Notification)'应用于'(java.lang.String,android.support.v4.app.NotificationCompat.Builder)'


同样将NotificationCompat.Builder更改为Notification.Builder需要将API版本更改为26,我不打算这样做。
任何帮助是极大的赞赏。

最佳答案

使用此示例通过notificationmanager发送通知

private void sendNotification() {
    // Send notifications to watch
    NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(getApplicationContext())
                    .setContentTitle(getResources().getString(R.string.app_name))
                    .setContentText(Integer.toString(mStepsCount) + " " +
                            getResources().getString(R.string.steps))
                    .setSmallIcon(R.mipmap.ic_notification_fitwatch);

    // Get an instance of the NotificationManager service
    NotificationManagerCompat notificationManager =
            NotificationManagerCompat.from(getApplicationContext());

    // Build the notification and issues it with notification manager.
    notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
}

10-08 17:22