Oreo-闪烁通知LED

时间:2019-01-08 10:02:42

标签: java android

在奥利奥中,如何启用通知指示灯闪烁而没有通知声音,如果我禁用通知声音指示灯不闪烁

  Notification notification = new NotificationCompat.Builder(this)
            .setLights(0xff00ff00, 3000, 100)
          //  .setContentTitle("title")
            .setSmallIcon(R.drawable.cubs)
            .setOngoing(false)
            .setSound(null)
            .setPriority(Notification.PRIORITY_MAX).build();
    NotificationManager notificationManager =
            (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    notificationManager.notify(notification_id, notification);

2 个答案:

答案 0 :(得分:1)

从奥利奥(Oreo)开始,引入了NotificationChannel机制。您必须设置频道并启用这样的灯光,

NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    CharSequence name = "Notification";
    String description = "Notification";
    int importance = NotificationManager.IMPORTANCE_DEFAULT;
    NotificationChannel mChannel = new NotificationChannel("someChannelID", name, importance);
    mChannel.setDescription(description);
    mChannel.setShowBadge(true);
    mChannel.enableLights(true);
    mChannel.setLightColor(Color.RED);
    mChannel.setSound(uri);
    if (notificationManager != null)  notificationManager.createNotificationChannel(mChannel);
    Notification notification = new Notification.Builder(context, "someChannelID")
            .setContentTitle(contentTitleText)
            .setContentText(contentContentText)
            .setNumber(1)
            .setSmallIcon(whiteLogo)
            .setBadgeIconType(whiteLogo)
            .setAutoCancel(true)
            .setContentIntent(pendingIntent)
            .build();
    if (notificationManager != null) {
        notificationManager.notify(notification_id, notification);
    }
}

else {
    NotificationCompat.Builder notification = new NotificationCompat.Builder(this)
            .setSmallIcon(whiteLogo)
            .setLargeIcon(largeIcon)
            .setContentTitle(contentTitleText)
            .setContentText(contentContentText)
            .setOngoing(false)
            .setNumber(1)
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .setAutoCancel(true)
            .setLights(colorPPDOrange, 1000, 2000);
            .setSound(uri);
            .setContentIntent(pendingIntent)
            .build();
    if (notificationManager != null) {
        notificationManager.notify(notification_id, notification);
    }
}

答案 1 :(得分:0)

这是我的代码
      `import android.app.Notification;        导入android.app.NotificationChannel;        导入android.app.NotificationManager;        导入android.app.PendingIntent;        导入android.content.Context;        导入android.content.Intent;        导入android.media.RingtoneManager;        导入android.net.Uri;        导入android.os.Build;        导入android.support.v4.app.NotificationCompat;        导入android.app.Activity;        导入android.os.Bundle;        导入android.util.Log;

   public abstract class MainActivity extends      Activity {

   @Override
   protected void onCreate(Bundle        savedInstanceState) {
      super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    NotificationManager notificationManager    =
            (NotificationManager)   getSystemService(Context.NOTIFICATION_SERVICE);

    String channelId = "test-channel";

    if (Build.VERSION.SDK_INT >=    Build.VERSION_CODES.O) {
        NotificationChannel newIncidentChannel = new NotificationChannel(channelId,
                "Test Channel",
                NotificationManager.IMPORTANCE_HIGH);
        notificationManager.createNotificationChannel(newIncidentChannel);
     }

    NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(this, channelId)
                    .setSmallIcon(R.drawable.notification_icon)
                    .setContentTitle("Test")
                    .setContentText("Text")
                    .setSound(RingtoneManager
                            .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                    .setDefaults(Notification.DEFAULT_ALL)
                    .setAutoCancel(true);

    int NOTIFICATION_ID = (int) (System.currentTimeMillis()%10000);
    notificationManager.notify("test", NOTIFICATION_ID, notificationBuilder.build());
}

} `