所以我不过是一个Button,并为其设置了一个侦听器,因此执行了下面的sendNotification()方法。但是,一旦单击按钮,即使catch语句也没有任何反应 请注意,代码是用片段编写的,因此我创建了变量上下文和活动,然后将其初始化
activity= getActivity();
context= getActivity().getBaseContext();
public void sendNotification() {
try{
NotificationCompat.Builder builder;
builder = new NotificationCompat.Builder(context,"id");
builder.setSmallIcon(R.drawable.ic_launcher_background)
.setContentTitle("Notifications Example")
.setContentText("This is a test notification");
Intent notificationIntent = new Intent(context, activity.getClass());
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
// Add as notification
NotificationManager manager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
manager.notify(0, builder.build());
}
catch (Exception e){MainActivity.prefConfig.displayToast("something is wrong with the method.. ");}
}
答案 0 :(得分:0)
如Create and Manage Notification Channels指南中所述:
如果您以Android 8.0(API级别26)为目标并且在未指定通知渠道的情况下发布通知,则该通知不会出现,并且系统会记录错误。
指南中提供了完整的说明,但是您基本上需要首先创建一个通知通道,如下所示:
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) {
CharSequence name = getString(R.string.channel_name);
String description = getString(R.string.channel_description);
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);
// 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);
}
}
在创建CHANNEL_ID
对象时,必须将相同的NotificationCompat.Builder
作为第二个参数传递。 (您已通过“ id”的地方)
答案 1 :(得分:0)
这可能会帮助您
NotificationCompat.Builder构造函数要求您提供一个通道ID。为了与Android 8.0(API级别26)及更高版本兼容,这是必需的,但较早版本会忽略。
在Android 8.0及更高版本上传递通知之前,必须通过将NotificationChannel实例传递给createNotificationChannel()在系统中注册应用程序的通知通道。因此,以下代码被SDK_INT版本上的条件阻止:
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) {
CharSequence name = getString(R.string.channel_name);
String description = getString(R.string.channel_description);
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);
// 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);
}}
每个通知都应响应点击,通常会在您的应用程序中打开一个与通知相对应的活动。为此,您必须指定使用PendingIntent对象定义的内容意图,并将其传递给setContentIntent()。
以下代码片段显示了如何在用户点击通知时创建打开活动的基本意图:
// Create an explicit intent for an Activity in your app
Intent intent = new Intent(this, AlertDetails.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Hello World!")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
// Set the intent that will fire when the user taps the notification
.setContentIntent(pendingIntent)
.setAutoCancel(true);
显示通知 要显示通知,请调用NotificationManagerCompat.notify(),并向其传递通知的唯一ID和NotificationCompat.Builder.build()的结果。
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
// notificationId is a unique int for each notification that you must define
notificationManager.notify(notificationId, mBuilder.build());
记住要保存传递给NotificationManagerCompat.notify()的通知ID,因为以后要更新或删除通知时将需要它。