我试图发送通知但它没有显示,也没有异常被抛出。我在Android 8.0上测试了这个。我听说你需要使用NotificationChannel或类似的东西,但我已经尝试过使用它但它仍然没有用,或者我必须使用它。
private void sendNotifications() {
final Random random = new Random();
try {
for (QuoteData data : quoteData) {
Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Quote quote = data.getQuotes().get(random.nextInt(data.getQuotes().size()));
String quoteMessage;
if (quote.getSayer().isEmpty()) {
quoteMessage = quote.getMessage();
} else {
quoteMessage = quote.getMessage() + "-" + quote.getSayer();
}
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification n = new Notification.Builder(context)
.setContentTitle(data.getTitle())
.setContentText(quoteMessage)
.setSmallIcon(R.drawable.ic_arrow_back_black_24dp)
.setContentIntent(pendingIntent)
.setAutoCancel(true).build();
notificationManager.notify(AlarmReceiver.getId(), n);
}
} catch (Exception e) {
e.printStackTrace();
}
}
答案 0 :(得分:1)
Android O必须在Notification Builder中使用频道
尝试使用此代码
NotificationManager notificationManager = (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);
// Configure the notification channel.
notificationChannel.setDescription("Channel description");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
notificationChannel.enableVibration(true);
notificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID) .setVibrate(new long[]{0, 100, 100, 100, 100, 100})
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setSmallIcon(R.mipmap.ic_launcher) .setContentTitle("Content Title")
.setContentText("Content Text");
notificationManager.notify(NOTIFICATION_ID, builder.build());
答案 1 :(得分:0)
像这样初始化你的频道
public void initChannels(Context context) {
if (Build.VERSION.SDK_INT < 26) {
return;
}
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel channel = new NotificationChannel("default",
"YOUR_CHANNEL_NAME",
NotificationManager.IMPORTANCE_DEFAULT);
channel.setDescription("Channel description");
notificationManager.createNotificationChannel(channel);
}
像这样传递给您的通知构建器
notificationBuilder = new NotificationCompat.Builder(this,"default")
答案 2 :(得分:0)
String CHANNEL_NAME="some_name"
String CHANNEL_ID="some_id"
private void sendNotifications() {
final Random random = new Random();
try {
for (QuoteData data : quoteData) {
Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Quote quote = data.getQuotes().get(random.nextInt(data.getQuotes().size()));
String quoteMessage;
if (quote.getSayer().isEmpty()) {
quoteMessage = quote.getMessage();
} else {
quoteMessage = quote.getMessage() + "-" + quote.getSayer();
}
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
// Using AppCompat library
NotificationCompat.Builder n = new NotificationCompat.Builder(context,CHANNEL_ID)
.setContentTitle(data.getTitle())
.setContentText(quoteMessage)
.setSmallIcon(R.drawable.ic_arrow_back_black_24dp)
.setContentIntent(pendingIntent)
.setAutoCancel(true).build();
// To support Android O devices
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationManager.createNotificationChannel(new NotificationChannelGroup(CHANNEL_ID,
CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT))
}
notificationManager.notify(AlarmReceiver.getId(), n);
}
} catch (Exception e) {
e.printStackTrace();
}
}