我正在尝试使用这段代码从服务创建通知:
NotificationCompat.Builder notif=new NotificationCompat.Builder(this,"ID1").setContentTitle("HI").setContentText("THERE");
NotificationManagerCompat man=(NotificationManagerCompat) getSystemService(NOTIFICATION_SERVICE);
man.notify("tag",notif);
但是我收到此错误:
Suspicious cast to
NotificationManagerCompat
for aNOTIFICATION_SERVICE
: expectedNotificationManager
我还尝试将 NotificationManagerCompat
更改为 NotificationManager
这给了我这个错误:
'notify(int, android.app.Notification)' in 'android.app.NotificationManager' cannot be applied to '(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());
}
关于java - 可疑的NotificationManagerCompat转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48846026/