2017-09-06 91 views
0

我拼命向我的應用程序添加通知功能,使通知LED閃爍。之後我所有的嘗試都沒有什麼工作......Android - 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()); 
} 

如果有人可以幫助,這將非常感激。

SOLUTION

對於那些誰,我有同樣的問題,有解決辦法:

@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

儘量提高測試的定時器......從變化200到2000只是爲了測試是否改變一些東西。 – W0rmH0le

+0

剛剛嘗試過,但它沒有工作.. –

+0

好的。你至少得到了通知,對吧?只是不閃爍的LED? – W0rmH0le

回答

0

選項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 

您可能需要根據自己的需要改變。更多信息:

Android DOC

選項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); 
} 
+0

你願意嫁給我嗎?我花了整整一天的時間來修復它!非常感謝 ! –

+0

很高興知道..請讓我知道以上哪些代碼適合您..僅供將來參考... – W0rmH0le

+0

這是第一個 –