繁体   English   中英

通知不起作用 - Android Studio

[英]Notification doesn't work - Android Studio

所以我是一个初学者,我正在关注一个关于如何在 Android Studio 中发出通知的教程。 当您按下按钮时,此应用应发出通知,但不会发出通知,而是显示开发人员警告

错误:

没有找到 pkg=com.example.myapplication、channelId=我的通知、id=1、tag=null、opPkg=com.example.myapplication、callingUid=10153、userId=0、incomingUserId=0、notificationUid=10153、通知的频道=通知(频道=我的通知快捷方式=null contentView=null vibrate=null sound=null defaults=0x0 flags=0x10 color=0x00000000 vis=PRIVATE)

代码(Java):

        public void onClick(View v) {
           

            NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this,"My notification");
            builder.setContentTitle("My Title");
            builder.setContentText("Test");
            builder.setSmallIcon(R.drawable.ic_launcher_background);
            builder.setAutoCancel(true);

            NotificationManagerCompat managerCompat = NotificationManagerCompat.from(MainActivity.this);
            managerCompat.notify(null,0, builder.build());
        }
    });

你知道如何解决这个问题吗?

private Notification getNotification() {
    Intent notificationIntent = new Intent(this, VideoExportActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
    NotificationChannel notificationChannel = null;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        notificationChannel = new NotificationChannel("CHANNEL_ID", "Alarm Time....", NotificationManager.IMPORTANCE_DEFAULT);
    }
    NotificationManager notificationManager = null;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        notificationManager = getSystemService(NotificationManager.class);
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        notificationManager.createNotificationChannel(notificationChannel);
    }
    return new NotificationCompat.Builder(this, "CHANNEL_ID")
            .setContentTitle("Title")
            .setContentText("Your Message")
            .setSmallIcon(R.drawable.ic_launcher_foreground)
            .setContentIntent(pendingIntent)
            .build();
}

通知通知 = getNotification();

从 Android 8.0(API 级别 26)开始,通知需要通知通道

这是您创建并分配给通知的类别。 每个应用程序有多个通知渠道,允许用户更好地管理他们希望从应用程序接收哪种通知。

为此,您需要创建一个通知通道,如 Android 文档中的示例所述:

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可以是任何 integer 编号,在您的应用程序的生命周期内不会更改。 如果你把它变成static final int就可以了。 创建频道后,您可以在创建通知时指定相同的CHANNEL_ID

NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
        .setSmallIcon(R.drawable.notification_icon)
        .setContentTitle(textTitle)
        .setContentText(textContent)
        .setPriority(NotificationCompat.PRIORITY_DEFAULT);

资源

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM