Android 使用通知通道创建通知不会产生任何效果
我一直在网上寻找答案,但我自己找不到答案,尽管我阅读了android官方文档网站上的开发者指南和来自互联网上的代码片段 我一直在练习在一个新的clean项目上创建通知,尽管创建了一个通知通道和通知本身,但我仍然无法让它运行,并收到一条消息:“未能在空通道上发布通知”。但是我正在创建频道,并将其分配给我的通知,就像它在任何地方所说的那样。我完全没有想法,老实说,我已经浏览了至少几个小时的堆栈,每个答案看起来都一样,但它真的没有帮助我,所以如果有人谁比乞丐看我的代码更好可以告诉我我的代码出了什么问题?这很烦人,因为通知是我正在做的一个项目的一个重要部分,它阻止了我前进Android 使用通知通道创建通知不会产生任何效果,android,notifications,sdk,mobile-application,channels,Android,Notifications,Sdk,Mobile Application,Channels,我一直在网上寻找答案,但我自己找不到答案,尽管我阅读了android官方文档网站上的开发者指南和来自互联网上的代码片段 我一直在练习在一个新的clean项目上创建通知,尽管创建了一个通知通道和通知本身,但我仍然无法让它运行,并收到一条消息:“未能在空通道上发布通知”。但是我正在创建频道,并将其分配给我的通知,就像它在任何地方所说的那样。我完全没有想法,老实说,我已经浏览了至少几个小时的堆栈,每个答案看起来都一样,但它真的没有帮助我,所以如果有人谁比乞丐看我的代码更好可以告诉我我的代码出了什么问题
public class MainActivity extends AppCompatActivity {
NotificationCompat.Builder notification;
private static final int NOTIFICATION_ID = 1;
private static final String NOTIFICATION_CHANNEL_ID = "my_notification_channel";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
notification = new NotificationCompat.Builder(this);
notification.setAutoCancel(true);
findViewById(R.id.buckysButton).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
createNotification(view);
}
});
}
public void createNotification(View view){
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_DEFAULT);
notificationChannel.setDescription("Channel description");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
notificationChannel.enableVibration(true);
nm.createNotificationChannel(notificationChannel);
}
notification.setSmallIcon(R.drawable.ic_priority_high_black_24px);
notification.setTicker("Ticker");
notification.setWhen(System.currentTimeMillis());
notification.setContentTitle("Title");
notification.setContentText("Body Body Body Body Body Body Body Body Body ");
notification.setChannel(NOTIFICATION_CHANNEL_ID);
notification.build();
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setContentIntent(pendingIntent);
nm.notify(NOTIFICATION_ID, notification.build());
}}
顺便说一句,我正在为Android最低级别5.1开发,我一直在想,是否真的有必要在我的应用程序中设置频道,但我还是这样做了,因为当我尝试在没有频道的情况下设置频道时,我仍然会遇到完全相同的错误
编辑:我还在为这个问题挣扎。我必须在gradle文件中做些什么才能将两个参数传递到NotificationCompat.Builder构造函数中吗?不管我做什么,它都不会让我做,我认为这是这里的主要问题
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
不为我这么做,它仍然不想把频道id作为参数
new NotificationCompat.Builder(this);
如果已弃用,请尝试使用
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
下面是一个示例代码,创建一个NotificationHelper
类,如下所示
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.provider.Settings;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.ContextCompat;
public class NotificationHelper extends ContextWrapper {
private NotificationManager mNotificationManager;
private final String MY_CHANNEL = "my_channel";
private final long[] vibrationScheme = new long[]{200, 400};
/**
* Registers notification channels, which can be used later by individual notifications.
*
* @param context The application context
*/
public NotificationHelper(Context context) {
super(context);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
// Create the channel object with the unique ID MY_CHANNEL
NotificationChannel myChannel =
new NotificationChannel(
MY_CHANNEL,
getResources().getString(R.string.notification_channel_title),
NotificationManager.IMPORTANCE_DEFAULT);
// Configure the channel's initial settings
myChannel.setLightColor(Color.GREEN);
myChannel.setVibrationPattern(vibrationScheme);
// Submit the notification channel object to the notification manager
getNotificationManager().createNotificationChannel(myChannel);
}
}
/**
* Build you notification with desired configurations
*
*/
public NotificationCompat.Builder getNotificationBuilder(String title, String body, Intent pendingIntent) {
Bitmap notificationLargeIconBitmap = BitmapFactory.decodeResource(
getApplicationContext().getResources(),
R.mipmap.ic_launcher);
return new NotificationCompat.Builder(getApplicationContext(), MY_CHANNEL)
.setSmallIcon(R.drawable.ic_notification_icon)
.setLargeIcon(notificationLargeIconBitmap)
.setContentTitle(title)
.setContentText(body)
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
.setVibrate(vibrationScheme)
.setStyle(new NotificationCompat.BigTextStyle().bigText(body))
.setColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary))
.setContentIntent(pendingIntent)
.setAutoCancel(true);
}
public NotificationManager getNotificationManager() {
if (mNotificationManager == null) {
mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
}
return mNotificationManager;
}
}
NotificationHelper notificationHelper = new NotificationHelper(context);
NotificationCompat.Builder builder = notificationHelper.getNotificationBuilder(title, message);
notificationHelper.getNotificationManager().notify(notificationID, builder.build());
然后按如下方式使用它
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.provider.Settings;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.ContextCompat;
public class NotificationHelper extends ContextWrapper {
private NotificationManager mNotificationManager;
private final String MY_CHANNEL = "my_channel";
private final long[] vibrationScheme = new long[]{200, 400};
/**
* Registers notification channels, which can be used later by individual notifications.
*
* @param context The application context
*/
public NotificationHelper(Context context) {
super(context);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
// Create the channel object with the unique ID MY_CHANNEL
NotificationChannel myChannel =
new NotificationChannel(
MY_CHANNEL,
getResources().getString(R.string.notification_channel_title),
NotificationManager.IMPORTANCE_DEFAULT);
// Configure the channel's initial settings
myChannel.setLightColor(Color.GREEN);
myChannel.setVibrationPattern(vibrationScheme);
// Submit the notification channel object to the notification manager
getNotificationManager().createNotificationChannel(myChannel);
}
}
/**
* Build you notification with desired configurations
*
*/
public NotificationCompat.Builder getNotificationBuilder(String title, String body, Intent pendingIntent) {
Bitmap notificationLargeIconBitmap = BitmapFactory.decodeResource(
getApplicationContext().getResources(),
R.mipmap.ic_launcher);
return new NotificationCompat.Builder(getApplicationContext(), MY_CHANNEL)
.setSmallIcon(R.drawable.ic_notification_icon)
.setLargeIcon(notificationLargeIconBitmap)
.setContentTitle(title)
.setContentText(body)
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
.setVibrate(vibrationScheme)
.setStyle(new NotificationCompat.BigTextStyle().bigText(body))
.setColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary))
.setContentIntent(pendingIntent)
.setAutoCancel(true);
}
public NotificationManager getNotificationManager() {
if (mNotificationManager == null) {
mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
}
return mNotificationManager;
}
}
NotificationHelper notificationHelper = new NotificationHelper(context);
NotificationCompat.Builder builder = notificationHelper.getNotificationBuilder(title, message);
notificationHelper.getNotificationManager().notify(notificationID, builder.build());
我已经找到了丢失的东西。我使用的是android studio 2.3.3,而不是最新的3.0.1,因此我无法通过Gradle导入一些新功能 之后,我只需将依赖项类路径更改为'com.android.tools.build:gradle:3.0.1',问题就解决了
谢谢你的帮助。好吧,也许我有点晚了,但是 尝试缩短您的channelId字符串 现在它
private static final String NOTIFICATION_CHANNEL_ID = "my_notification_channel";
试一试
对我来说,这是一个非常奇怪的错误。当channelId太长时,它不会在我的设备上显示通知
编辑:
它不是太长或太短,只是一些字符串在系统中被禁止。它也没有显示在Android通知设置屏幕中。不知何故,我无法获取更新的构造函数,我的应用程序总是给我一个错误,告诉我NotificationCompat.Builder只将上下文作为参数,不能同时获取字符串。我仍在为此挣扎,情不自禁。也许是因为我在Oreo发布之前做了这个项目?@Piotroleh:检查你是否正在导入android.support.v4.app。NotificationCompat@PiotrOlech当前位置我添加了一个示例,请尝试