Android 通知方法中的通知崩溃应用程序
我一直在尝试显示通知,但它要么不显示,要么在notify方法中导致致命错误。此通知实际上应该是一个祝酒词,它将一直保存在通知抽屉中,直到点击为止 我试过几种不同的方法,但没有一种。我还复制了一个仍然不起作用的完整示例 我不知道是什么原因导致了这个错误,我已经尝试在应用程序中添加一个logcat,但是我无法得到任何信息 我使用的是Pixel2运行的Stock8.1;我的编程在手机上,因此无法使用adb/root选项Android 通知方法中的通知崩溃应用程序,android,android-notifications,Android,Android Notifications,我一直在尝试显示通知,但它要么不显示,要么在notify方法中导致致命错误。此通知实际上应该是一个祝酒词,它将一直保存在通知抽屉中,直到点击为止 我试过几种不同的方法,但没有一种。我还复制了一个仍然不起作用的完整示例 我不知道是什么原因导致了这个错误,我已经尝试在应用程序中添加一个logcat,但是我无法得到任何信息 我使用的是Pixel2运行的Stock8.1;我的编程在手机上,因此无法使用adb/root选项 public int noti(String title, String bo
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;
}
我知道参数在toast中是有效的,我还知道没有触发任何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:从包vzy.html.tester发布的错误通知:无法创建图标:StatusBarIcon(图标=图标(类型=URI=content://com.android.externalstorage.documents/document/primary%3Ayp_sodapop.png)可见用户=0)
我猜您没有为通知创建通知频道。在所有运行8.0+的Android设备上,每个通知都需要关联到一个通知通道。您正在将通道ID“83”传递给通知生成器,但可能您事先没有实际创建通道
有关如何创建频道的详细信息,请查看以下内容:
请张贴日志。它有助于定位错误。我无法从我的logcat尝试中获得任何信息,我需要从应用程序内部调用它,这样我的选项会更有限。请尝试在catch块中写入
e.printstacktrace()
,然后再次检查异常。我为输出文件添加了一个printstacktrace和一个printstacktrace,捕获没有被触发…为什么日志中没有错误日志?哦,我忘了在我的问题中添加它,让我把它移入。