我正在拼命尝试向我的应用程序添加通知功能,使通知LED闪烁。在我所有的尝试之后没有任何作用......
这是我的代码:
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
NotificationCompat.Builder notification = new NotificationCompat.Builder(this)
.setLights(Color.BLUE, 200, 200)
.setContentTitle(remoteMessage.getData().get("title"))
.setContentText(remoteMessage.getData().get("body"))
.setColor(getColor(R.color.buttonBlueInactive))
.setSmallIcon(R.mipmap.ic_launcher);
NotificationManagerCompat manager = NotificationManagerCompat.from(this);
manager.notify(1, notification.build());
}
如果有人能提供帮助,我们将非常感激。
解
对于那些遇到同样问题的人,有解决方案:
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
NotificationCompat.Builder notification = new NotificationCompat.Builder(this)
.setLights(Color.BLUE, 200, 200)
// Add the line bellow
.setDefaults(Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND | Notification.FLAG_SHOW_LIGHTS)
.setContentTitle(remoteMessage.getData().get("title"))
.setContentText(remoteMessage.getData().get("body"))
.setColor(getColor(R.color.buttonBlueInactive))
.setSmallIcon(R.mipmap.ic_launcher);
NotificationManagerCompat manager = NotificationManagerCompat.from(this);
manager.notify(1, notification.build());
}
答案 0 :(得分:1)
选项1
如果我没错,你就错过了setDefaults()
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
NotificationCompat.Builder notification = new NotificationCompat.Builder(this)
.setLights(Color.BLUE, 200, 200)
.setDefaults(Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND | Notification.FLAG_SHOW_LIGHTS) // Add this line
.setContentTitle(remoteMessage.getData().get("title"))
.setContentText(remoteMessage.getData().get("body"))
.setColor(getColor(R.color.buttonBlueInactive))
.setSmallIcon(R.mipmap.ic_launcher);
NotificationManagerCompat manager = NotificationManagerCompat.from(this);
manager.notify(1, notification.build());
}
在上面的示例中,我添加了以下选项:
Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND | Notification.FLAG_SHOW_LIGHTS
您可能希望根据需要进行更改。更多信息:
选项2
如果上述示例不起作用,请尝试以下代码:
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
NotificationCompat.Builder notification = new NotificationCompat.Builder(this)
.setLights(Color.BLUE, 200, 200)
.setContentTitle(remoteMessage.getData().get("title"))
.setContentText(remoteMessage.getData().get("body"))
.setColor(getColor(R.color.buttonBlueInactive))
.setSmallIcon(R.mipmap.ic_launcher);
Notification builtNotification = notification.build();
builtNotification.flags |= Notification.FLAG_SHOW_LIGHTS;
NotificationManagerCompat manager = NotificationManagerCompat.from(this);
manager.notify(1, builtNotification);
}