我正在尝试使用这段代码从服务创建通知:
NotificationCompat.Builder notif=new NotificationCompat.Builder(this,"ID1").setContentTitle("HI").setContentText("THERE");
NotificationManagerCompat man=(NotificationManagerCompat) getSystemService(NOTIFICATION_SERVICE);
man.notify("tag",notif);
但是我收到了这个错误:
对于
,可疑人员NotificationManagerCompat
:NOTIFICATION_SERVICE
预测NotificationManager
NotificationManagerCompat
我也尝试将NotificationManager
更改为NotificationCompat.Builder
,这给了我这个错误:
'android.app.NotificationManager'中的'notify(int,android.app.Notification)'无法应用于'(java.lang.String,android.support.v4.app.NotificationCompat.Builder)'
同时将Notification.Builder
更改为plt.draw()
需要将API版本更改为26,我不打算这样做。
非常感谢任何帮助。
答案 0 :(得分:2)
使用此示例通过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());
}
答案 1 :(得分:0)
引用the documentation for NotificationManagerCompat
:
要使用此类,请调用静态函数
from(Context)
以获取NotificationManagerCompat
对象,然后调用其中一种方法发布或取消通知。
所以,替换:
NotificationManagerCompat man=(NotificationManagerCompat) getSystemService(NOTIFICATION_SERVICE);
使用:
NotificationManagerCompat man=NotificationManagerCompat.from(this);