自定义通知在Android Oreo中不起作用

时间:2018-10-17 07:27:48

标签: android android-studio

我正在尝试在Oreo设备上创建通知。我已经添加了频道和所有内容,但仍然无法正常运行。我正在使用Samsung Galaxy J7进行测试,并在活动的onCreate()中调用此函数。

  private void sendNotification() {
    Intent intent = new Intent(this, DailyReportActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    Notification.Builder builder;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        String id = "w01", name = "Some Name";
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        String desc = "Description";

        NotificationChannel channel = new NotificationChannel(id, name, importance);
        channel.setDescription(desc);
        notificationManager.createNotificationChannel(channel);
        builder = new Notification.Builder(this, id);
    } else
        builder = new Notification.Builder(this);

    builder.setAutoCancel(false);
    builder.setContentTitle("Notification Title");
    builder.setContentText("Content Text");
    builder.setStyle(new Notification.BigTextStyle().bigText("Some Data"));
    builder.setSmallIcon(R.drawable.app_icon);
    builder.setContentIntent(pendingIntent);
    Notification notification = builder.build();

    notificationManager.notify(8, notification);
}

我不知道我在这里缺少什么,它正在我的棉花糖设备上工作。

0 个答案:

没有答案