伙计们,我使用此代码来显示通知,但是今天我尝试创建一个新的应用程序,并且不显示通知,我不知道发生了什么。我从firebase发送通知,但不显示通知。有什么想法吗?
在模拟器上它显示通知,而不是在我的三星A8(2018)中,我在其他应用程序中具有相同的代码,效果很好。
private void showSmallNotification( int icon, String title, String message, String timeStamp, PendingIntent resultPendingIntent, Uri alarmSound) {
final NotificationCompat.Builder builder = new NotificationCompat.Builder(this, Constants.TOPIC_GLOBAL);
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
inboxStyle.addLine(message);
Notification notification;
notification = builder
.setChannelId(Constants.TOPIC_GLOBAL)
.setAutoCancel(true)
.setContentTitle(title)
.setContentIntent(resultPendingIntent)
.setSound(alarmSound)
.setStyle(inboxStyle)
.setWhen(getTimeMilliSec(timeStamp))
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(this.getResources(), icon))
.setContentText(message)
.build();
NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(100, notification);
}
答案 0 :(得分:1)
在Android 8.0及更高版本上传递通知之前,必须通过将NotificationChannel实例传递给createNotificationChannel()在系统中注册应用程序的通知通道。因此,以下代码被SDK_INT版本上的条件阻止:
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);
}
}
阅读this文章。
答案 1 :(得分:1)
String CHANNEL_ID = "1";
String CHANNEL_NAME = "Notification";
Notification.Builder notification;
if (Build.VERSION.SDK_INT >= 26) {
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
channel.enableVibration(true);
channel.setLightColor(Color.BLUE);
channel.enableLights(true);
channel.setSound(Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + getApplicationContext().getPackageName() + "/" + R.raw.trial),
new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build());
channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET);
notificationManager.createNotificationChannel(channel);
notification = new Notification.Builder(getApplicationContext(), CHANNEL_ID);
} else {
notification = new Notification.Builder(getApplicationContext());
notification.setSound(Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + getPackageName() + "/" + R.raw.trial));
}
notification.setContentTitle(Title)
.setVibrate(new long[]{0, 100})
.setPriority(Notification.PRIORITY_MAX)
.setContentText(ContentText)
.setColor(ContextCompat.getColor(getApplicationContext(), R.color.green))
.setLights(Color.RED, 3000, 3000)
.setSmallIcon(R.drawable.ic_notifications_active_purple_24dp)
.setContentIntent(pendingIntent)
.setWhen(System.currentTimeMillis())
.setAutoCancel(true);
notificationManager.notify(CHANNEL_ID, 1, notification.build());