android 为什么代码不显示通知

mkshixfv  于 2022-12-31  发布在  Android
关注(0)|答案(3)|浏览(447)

所以我试着在手机顶部创建一个通知,但是当我运行我的代码时,它什么也不做。甚至没有一个错误。我在这里做错了什么?

public void createNotification(Context ctx) {

        SharedPreferences settings = ctx.getApplicationContext().getSharedPreferences(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, 0);
        String contactName = settings.getString("curName", String.valueOf(0));
        String contactEmail = settings.getString("contactEmail", String.valueOf(0));
        String contactNumber = settings.getString("curPhone", String.valueOf(0));
        String dueAmount = String.valueOf(settings.getInt("amountDue", 0));

        NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx);
        builder.setSmallIcon(R.drawable.myanlogo);
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.journaldev.com/"));
        PendingIntent pendingIntent = PendingIntent.getActivity(ctx, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
        builder.setContentIntent(pendingIntent);
        builder.setLargeIcon(BitmapFactory.decodeResource(ctx.getResources(), R.mipmap.ic_launcher));
        builder.setContentTitle("LETTING YOU KNOW");
        builder.setContentText("Your notification content here.");

        NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(NOTIFICATION_SERVICE);

        // Will display the notification in the notification bar
        notificationManager.notify(1, builder.build());

    }

我把它放在一个类里,就像这样:

class ContactRVAdapter extends RecyclerView.Adapter<ContactRVAdapter.ViewHolder>

我在这里调用这个函数:

holder.textDueTomorrow.setVisibility(View.GONE);
        if (Integer.valueOf(getFirstDateNumber) == Integer.valueOf(getSecondDateNumber) - 1) {
            holder.textDueTomorrow.setVisibility(View.VISIBLE);

            createNotification(context);

        }

一些建议会很好,因为我已经在这方面工作了一段时间了。

2ekbmq32

2ekbmq321#

看起来您缺少Build.VERSION_CODE.O和更高版本https://developer.android.com/develop/ui/views/notifications/channels的通知通道

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            int importance = NotificationManager.IMPORTANCE_LOW;
            NotificationChannel notificationChannel = new NotificationChannel(channelId, channelName, importance);
            notificationManager.createNotificationChannel(notificationChannel);

            // Make notification show big text.
            NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
            bigTextStyle.setBigContentTitle(title);
            bigTextStyle.bigText(notificationText);
            // Set big text style.
            builder.setStyle(bigTextStyle);
        } else {
            builder.setContentTitle(title);
            builder.setContentText(notificationText);
        }
anauzrmj

anauzrmj2#

下面的香奈儿链接和下面提供的例子沿着把我弄晕了。

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            int importance = NotificationManager.IMPORTANCE_LOW;
            NotificationChannel notificationChannel = new NotificationChannel(channelId, channelName, importance);
            notificationManager.createNotificationChannel(notificationChannel);

            // Make notification show big text.
            NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
            bigTextStyle.setBigContentTitle(title);
            bigTextStyle.bigText(notificationText);
            // Set big text style.
            builder.setStyle(bigTextStyle);
        } else {
            builder.setContentTitle(title);
            builder.setContentText(notificationText);
        }

这个链接使它更清楚。notification channel is not sending notifications它基本上100%的罚款,他只是忘了调用函数createNotificationChannel()之前创建通知。

jgwigjjp

jgwigjjp3#

对于Android 8及更高版本,您需要创建通知通道,在通知构建器之前写入,并将相同的通道ID传递给NotificationCompat.Builder
复制/粘贴:

public void createNotification(Context ctx) {
    createNotificationChannel();

    SharedPreferences settings = ctx.getApplicationContext().getSharedPreferences(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, 0);
    String contactName = settings.getString("curName", String.valueOf(0));
    String contactEmail = settings.getString("contactEmail", String.valueOf(0));
    String contactNumber = settings.getString("curPhone", String.valueOf(0));
    String dueAmount = String.valueOf(settings.getInt("amountDue", 0));

    NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx, "CHANNEL_ID");
    builder.setSmallIcon(R.drawable.myanlogo);
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.journaldev.com/"));
    PendingIntent pendingIntent = PendingIntent.getActivity(ctx, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
    builder.setContentIntent(pendingIntent);
    builder.setLargeIcon(BitmapFactory.decodeResource(ctx.getResources(), R.mipmap.ic_launcher));
    builder.setContentTitle("LETTING YOU KNOW");
    builder.setContentText("Your notification content here.");

    NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(NOTIFICATION_SERVICE);
    notificationManager.notify(1, builder.build());
}

我已更新此行NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx, "CHANNEL_ID");

JAVA语言

private void createNotificationChannel() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel notificationChannel = new NotificationChannel("CHANNEL_ID", "Updates", NotificationManager.IMPORTANCE_DEFAULT);
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notificationManager.createNotificationChannel(notificationChannel);
    }
}

Kotlin

private fun createNotificationChannel() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        val importance = NotificationManager.IMPORTANCE_DEFAULT
        val notificationChannel = NotificationChannel("CHANNEL_ID", "Updates", NotificationManager.IMPORTANCE_DEFAULT)
        val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
        notificationManager.createNotificationChannel(notificationChannel)
    }
}

相关问题