简体   繁体   English

通知不起作用 - Android Studio

[英]Notification doesn't work - Android Studio

So I am a beginner and I was following one tutorial on how to make notification in Android Studio.所以我是一个初学者,我正在关注一个关于如何在 Android Studio 中发出通知的教程。 This app should make a notification when you press a button, but instead of making notification it shows up a Developer Warning当您按下按钮时,此应用应发出通知,但不会发出通知,而是显示开发人员警告

Error:错误:

No Channel found for pkg=com.example.myapplication, channelId=My notification, id=1, tag=null, opPkg=com.example.myapplication, callingUid=10153, userId=0, incomingUserId=0, notificationUid=10153, notification=Notification(channel=My notification shortcut=null contentView=null vibrate=null sound=null defaults=0x0 flags=0x10 color=0x00000000 vis=PRIVATE)没有找到 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)

Code(Java):代码(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());
        }
    });

Do you know how to fix this?你知道如何解决这个问题吗?

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();
}

Notification notification = getNotification();通知通知 = getNotification();

Starting with Android 8.0 (API level 26), notifications require a notification channel .从 Android 8.0(API 级别 26)开始,通知需要通知通道

This is a category which you create and assign to your notifications.这是您创建并分配给通知的类别。 Having multiple notification channels per app, allows users to better manage which kind of notifications they want to receive from an app.每个应用程序有多个通知渠道,允许用户更好地管理他们希望从应用程序接收哪种通知。

To achieve this, you need to create a notification channel as stated in the example in the Android docs:为此,您需要创建一个通知通道,如 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);
    }
}

Source 资源

CHANNEL_ID can be any integer number which doesn't change during the lifetime of your app. CHANNEL_ID可以是任何 integer 编号,在您的应用程序的生命周期内不会更改。 It is OK if you make it a static final int .如果你把它变成static final int就可以了。 Once you created the channel, you can specify the same CHANNEL_ID when creating notifications:创建频道后,您可以在创建通知时指定相同的CHANNEL_ID

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

Source 资源

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

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