伙计们我用这段代码来显示通知,但今天我尝试创建一个新的应用程序,并没有显示通知,我不知道发生了什么。从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);
}
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());
在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文章。