[英]Notification in android system tray, does not work
i have set up the code for receiving notifications but it is not working:我已经设置了接收通知的代码,但它不起作用:
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);
I am using it on my MainActivity, and i tryied it as the telephone is in standby.我在我的 MainActivity 上使用它,并在电话处于待机状态时尝试了它。 How can i solve?
我该如何解决? Does it need some other code?
它需要一些其他代码吗? I followed this guide: http://www.dre.vanderbilt.edu/~schmidt/android/android-4.0/out/target/common/docs/doc-comment-check/guide/topics/ui/notifiers/notifications.html
我遵循了本指南: http://www.dre.vanderbilt.edu/~schmidt/android/android-4.0/out/target/common/docs/doc-comment-check/guide/topics/ui/notifiers/notifications。 html
Thanks谢谢
public static final String CHANNEL_ID = "com.example.test.services";
public static final String CHANNEL_NAME = "UploadBackgroundService";
public static final int FOREGROUND_ID = 1339;
private void createAndShowForegroundNotification() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel chan = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_NONE);
chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
assert manager != null;
manager.createNotificationChannel(chan);
notificationBuilder = new NotificationCompat.Builder(this, CHANNEL_ID);
Notification notification = notificationBuilder
.setContentTitle("Image upload service")
.setContentText("Uploading Images...")
.setPriority(NotificationManager.IMPORTANCE_HIGH)
.setCategory(Notification.CATEGORY_SERVICE)
.setOngoing(true)
.setSmallIcon(R.drawable.logoo)
.setAutoCancel(false)
.build();
manager.notify(FOREGROUND_ID, notification);
} else {
manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationBuilder = new NotificationCompat.Builder(this,CHANNEL_ID )
.setDefaults(Notification.DEFAULT_ALL)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setContentTitle("Image upload service")
.setContentText("Uploading Images...")
.setOngoing(true)
.setSmallIcon(R.drawable.logoo)
.setAutoCancel(false);
Notification notification = notificationBuilder.build();
manager.notify(FOREGROUND_ID, notification);
}
}
try this code.replace the required parameters with your own.
I solved it with this code:我用这段代码解决了它:
public static final String CHANNEL_ID = "messages-id";
public static final String CHANNEL_NAME = "messages";
public static final int FOREGROUND_ID = 1339;
private void notifyForeground(String message) {
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel chan = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
assert manager != null;
manager.createNotificationChannel(chan);
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, CHANNEL_ID);
Notification notification = notificationBuilder
.setContentIntent(contentIntent)
.setDefaults(Notification.DEFAULT_ALL)
.setContentTitle("New message")
.setContentText(message)
.setPriority(NotificationManager.IMPORTANCE_HIGH)
.setCategory(Notification.CATEGORY_MESSAGE)
.setSmallIcon(R.drawable.messages_received)
.setAutoCancel(true)
.build();
manager.notify(FOREGROUND_ID, notification);
} else {
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this,CHANNEL_ID )
.setDefaults(Notification.DEFAULT_ALL)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setContentTitle("New message")
.setContentText(message)
.setSmallIcon(R.drawable.messages_received)
.setAutoCancel(true);
Notification notification = notificationBuilder.build();
manager.notify(FOREGROUND_ID, notification);
}
}
Thanks谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.