调用notificationManager.publish发布通知失败
很抱歉,您没有提供具体的错误信息或代码示例,所以我无法为您提供一个准确的答案。
如果您在调用 notificationManager.publish()
方法时遇到问题,可能有几个原因:
<uses-permission android:name="android.permission.SEND_SMS" />
NotificationManager
。例如:NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.publish()
方法被正确调用。例如:Notification notification = new NotificationCompat.Builder(this, channelId)
.setContentTitle("My notification")
.setContentText("Hello World!")
.setSmallIcon(R.drawable.notification_icon)
.build();
notificationManager.notify(notificationId, notification);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = getString(R.string.channel_name);
String description = getString(R.string.channel_description);
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(channelId, name, importance);
channel.setDescription(description);
notificationManager.createNotificationChannel(channel);
}
notificationManager.publish()
失败的更多线索。
发布通知时,需要在真机端系统设置中,开启对应应用的通知开关,然后才能在通知栏中看到发布的通知。
手动开启步骤:设置 > 通知和状态栏 > 应用名称 > 允许通知。
也可通过接口
notificationManager.requestEnableNotification()
来弹窗让用户授权(仅弹一次)。