[英]Notification not working in android version Oreo
I am trying to generate an 'onclick' notification just for learning purposes in android studio, sdk version Oreo.我正在尝试在 android studio sdk 版本 Oreo 中生成仅用于学习目的的“onclick”通知。 But I just cant get it to work.
但我就是无法让它工作。 I have also created a notification channel but it isn't working.
我还创建了一个通知渠道,但它不起作用。 Can someone please tell what is the issue in the java code.
有人可以告诉java代码中的问题是什么。
package com.example.notificationexample;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
public class MainActivity extends AppCompatActivity {
Button button;
private final String CHANNEL_ID = "perosnal_notifications";
private final int NOTIFICATION_ID = 001;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.click);
final long [] vibe = {0,500};
final Uri notificationsound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
createNotificationChannel();
NotificationCompat.Builder mbuilder = (NotificationCompat.Builder)
new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.drawable.yo,10)
.setSound(notificationsound)
.setVibrate(vibe)
.setContentTitle("Notification")
.setContentText("This is a notification for you");
NotificationManager notificationManager = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID,mbuilder.build());
}
});
}
private void createNotificationChannel()
{
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O)
{
CharSequence name = "Personal not";
String description = "Include all personal nots";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, name, importance);
notificationChannel.setDescription(description);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(notificationChannel);
}
}
}
XML code XML 代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="30sp"
android:text="Notofication"/>
</RelativeLayout>
The application displays an error saying: "developer warning for package.. Failed to post notification on channel 'personal_notifications'"应用程序显示一条错误消息:“开发人员警告包.. 无法在频道‘personal_notifications’上发布通知”
Android version of virtual device is 11安卓版虚拟设备是11
Add channel id to builder:将频道 ID 添加到构建器:
NotificationCompat.Builder mbuilder = (NotificationCompat.Builder)
new NotificationCompat.Builder(getApplicationContext(),
CHANNEL_ID)// add channel id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.