我一直试图显示一个通知,但是它没有显示,或者在notify方法内部导致致命错误。该通知应该是有效的吐司,将一直留在通知抽屉中,直到被点击为止。
我尝试了几种不同的意图,包括没有。在某些情况下,我还复制了整个示例,但仍然无法正常工作。
我不知道是什么导致了错误,并且我尝试将logcat附加到应用程序中,但是我什么都没得到。
我使用的是Pixel 2自带库存8.1;我的编程是在手机上进行的,所以我不能使用adb / root选项
public int noti(String title, String body, String ico){
//Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();
showToast(title+">"+body+">"+ico);
try{
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.google.com/"));
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);
Notification.Builder mBuilder =
new Notification.Builder(getApplicationContext(), "83")
.setSmallIcon(Icon.createWithContentUri(ico))
.setContentTitle(title)
.setContentText(body)
.setOngoing(true)
.setContentIntent(pendingIntent)
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(getApplicationContext());
Notification noti = mBuilder.build();
if(noti==null){
logBack("noti.builder == null.");
return -2;
}
int notificationId = notiIDz.getAndIncrement();
// notificationId is a unique int for each notification that you must define
notificationManager.notify(notificationId, noti);
return notificationId;
} catch(Exception e){
logBack(e.toString());
}
return -1;
}
我知道参数在吐司中是有效的,我也知道没有logBack()被触发。
创建通知通道,在onCreate期间调用:
private void createNotificationChannel() {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("83", "vzyNotiChan", NotificationManager.IMPORTANCE_DEFAULT);
channel.setDescription("The notification channel for vzy apps.");
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
编辑,最后进入日志目录:07-24 10:48:10.879 27133 27133 E AndroidRuntime: android.app.RemoteServiceException: Bad notification posted from package vzy.html.tester: Couldn't create icon: StatusBarIcon(icon=Icon(typ=URI uri=content://com.android.externalstorage.documents/document/primary%3Ayp_sodapop.png) visible user=0 )
答案 0 :(得分:1)
我的猜测是您没有为通知创建通知通道。在所有运行8.0+版本的Android设备上,每个通知都需要与通知渠道相关联。您正在将通道ID“ 83”传递给通知生成器,但是也许您实际上尚未预先创建通道。
有关如何创建频道的更多信息,请查看以下内容: