简体   繁体   中英

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. 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)

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

Starting with Android 8.0 (API level 26), notifications require a notification channel .

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:

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. It is OK if you make it a static final int . Once you created the channel, you can specify the same CHANNEL_ID when creating notifications:

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

Source

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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