我一直在网上寻找答案,但我自己找不到它,尽管阅读官方Android文档网站上的开发者指南和来自互联网的代码片段。
我一直在练习在一个新的干净项目上创建通知,尽管创建了一个通知通道和通知本身,我仍然无法让它运行并收到一条消息:“无法在空通道上发布通知” 。但我正在创建频道,并将其分配给我的通知,就像它说到处都是。我完全没有想法,老实说我现在已经浏览堆栈至少几个小时,每个答案看起来都一样,但它根本没有帮助我,所以也许如果有人比一个初学者更好看我的代码可以告诉我我的代码有什么问题?这很烦人,因为通知是我正在进行的项目的一个重要部分,它阻止了我的发展。
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开发Android最低级别5.1,我一直想知道是否真的有必要在我的应用程序中放置频道,但我还是做了它,因为我试图在没有它们的情况下做到这一点我仍然得到完全相同的错误。
编辑:我仍然在努力解决这个问题。我是否必须在gradle文件中执行某些操作才能将两个参数传递到NotificationCompat.Builder构造函数中?不管我做什么,它都不会让我这样做,我认为这是主要的问题。NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
不能为我做,它仍然不想把channel_id作为参数。
答案 0 :(得分:6)
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());
答案 1 :(得分:1)
我发现了什么遗失了。我使用android studio 2.3.3而不是最新的3.0.1,所以我无法通过Gradle导入一些新功能。
之后我只需将我的依赖类路径更改为'com.android.tools.build:grad:3.0.1',问题就消失了。
感谢您的帮助。
答案 2 :(得分:1)
好吧,也许我来晚了,但是...
尝试缩短您的channelId字符串。
现在
private static final String NOTIFICATION_CHANNEL_ID = "my_notification_channel";
尝试
private static final String NOTIFICATION_CHANNEL_ID = "my_channel";
对我来说,这是一个非常奇怪的错误。当channelId太长时,它不会在我的设备上显示通知。
编辑:
它不是太长或太短,只是系统中禁止了某些字符串。另外,它也不会显示在Android通知设置屏幕中。