我正在尝试启动前台服务。我收到服务确实启动的通知,但通知总是被抑制。我仔细检查了该应用程序是否允许在我设备上的应用程序信息中显示通知。这是我的代码:
private void showNotification() {
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.setAction(Constants.ACTION.MAIN_ACTION);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);
Bitmap icon = BitmapFactory.decodeResource(getResources(),
R.mipmap.ic_launcher);
Notification notification = new NotificationCompat.Builder(getApplicationContext())
.setContentTitle("Revel Is Running")
.setTicker("Revel Is Running")
.setContentText("Click to stop")
.setSmallIcon(R.mipmap.ic_launcher)
//.setLargeIcon(Bitmap.createScaledBitmap(icon, 128, 128, false))
.setContentIntent(pendingIntent)
.setOngoing(true).build();
startForeground(Constants.FOREGROUND_SERVICE,
notification);
Log.e(TAG,"notification shown");
}
这是我在相关中看到的唯一错误: 06-20 12:26:43.635 895-930/? E/NotificationService: Suppressing notification from the package by user request.
原文由 Dreamers Org 发布,翻译遵循 CC BY-SA 4.0 许可协议
问题是我使用的是 Android O,它需要更多信息。这是android O的成功代码。