【发布时间】:2021-06-05 09:30:24
【问题描述】:
我已经设置了接收通知的代码,但它不起作用:
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager notificationManager = (NotificationManager) getSystemService(ns);
int icon = R.drawable.messages_received;
CharSequence contentTitle = "TITLE";
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
Context context = getApplicationContext();
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
Notification notification = builder
.setContentIntent(contentIntent)
.setSmallIcon(icon)
.setWhen( System.currentTimeMillis())
.setContentTitle(contentTitle)
.setContentText(message.getMessage())
.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.defaults |= Notification.DEFAULT_LIGHTS;
final int NOTIFY = 1;
notificationManager.notify(NOTIFY, notification);
我在我的 MainActivity 上使用它,并在电话处于待机状态时尝试了它。 我该如何解决?它需要一些其他代码吗? 我遵循了这个指南:http://www.dre.vanderbilt.edu/~schmidt/android/android-4.0/out/target/common/docs/doc-comment-check/guide/topics/ui/notifiers/notifications.html
谢谢
【问题讨论】:
-
您测试的操作系统版本是什么?
-
Android 10,但我也想要旧操作系统的解决方案,谢谢
标签: java android notifications