通知在 android 版 Oreo 中不起作用
Notification not working in android version Oreo
我正在尝试生成一个 'onclick' 通知,仅供在 android studio、sdk 版本 Oreo 中学习。但我就是无法让它工作。我还创建了一个通知渠道,但它不起作用。有人可以告诉我 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代码
<?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>
应用程序显示一条错误消息:
“包的开发人员警告.. 无法在频道 'personal_notifications' 上 post 通知”
Android虚拟设备版本为11
将频道 ID 添加到构建器:
NotificationCompat.Builder mbuilder = (NotificationCompat.Builder)
new NotificationCompat.Builder(getApplicationContext(),
CHANNEL_ID)// add channel id
我正在尝试生成一个 'onclick' 通知,仅供在 android studio、sdk 版本 Oreo 中学习。但我就是无法让它工作。我还创建了一个通知渠道,但它不起作用。有人可以告诉我 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代码
<?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>
应用程序显示一条错误消息: “包的开发人员警告.. 无法在频道 'personal_notifications' 上 post 通知”
Android虚拟设备版本为11
将频道 ID 添加到构建器:
NotificationCompat.Builder mbuilder = (NotificationCompat.Builder)
new NotificationCompat.Builder(getApplicationContext(),
CHANNEL_ID)// add channel id