Android前台服务通知未显示

时间:2017-06-20 16:52:11

标签: android notifications

我正在尝试启动前台服务。我收到通知,该服务确实启动但通知始终被抑制。我仔细检查了应用是否允许在我的设备上的应用信息中显示通知。这是我的代码:

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.

6 个答案:

答案 0 :(得分:19)

这是因为Android O bg服务限制。

现在,您只需要为startForeground()启动的服务调用startForegroundService(),并在服务启动后的前5秒内调用它。

以下是指南 - https://developer.android.com/about/versions/oreo/background#services

像这样:

//Start service:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  startForegroundService(new Intent(this, YourService.class));
} else {
  startService(new Intent(this, YourService.class));
}

然后创建并显示通知(使用前面假设的频道):

private void createAndShowForegroundNotification(Service yourService, int notificationId) {

    final NotificationCompat.Builder builder = getNotificationBuilder(yourService,
         "com.example.your_app.notification.CHANNEL_ID_FOREGROUND", // Channel id
    NotificationManagerCompat.IMPORTANCE_LOW); //Low importance prevent visual appearance for this notification channel on top 
    builder.setOngoing(true)
    .setSmallIcon(R.drawable.small_icon)
    .setContentTitle(yourService.getString(R.string.title))
    .setContentText(yourService.getString(R.string.content));

    Notification notification = builder.build();

    yourService.startForeground(notificationId, notification);

    if (notificationId != lastShownNotificationId) {
          // Cancel previous notification
          final NotificationManager nm = (NotificationManager) yourService.getSystemService(Activity.NOTIFICATION_SERVICE);
          nm.cancel(lastShownNotificationId);
    }
    lastShownNotificationId = notificationId;
}

public static NotificationCompat.Builder getNotificationBuilder(Context context, String channelId, int importance) {
    NotificationCompat.Builder builder;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        prepareChannel(context, channelId, importance);
        builder = new NotificationCompat.Builder(context, channelId);
    } else {
        builder = new NotificationCompat.Builder(context);
    }
    return builder;
}

@TargetApi(26)
private static void prepareChannel(Context context, String id, int importance) {
    final String appName = context.getString(R.string.app_name);
    String description = context.getString(R.string.notifications_channel_description);
    final NotificationManager nm = (NotificationManager) context.getSystemService(Activity.NOTIFICATION_SERVICE);

    if(nm != null) {
        NotificationChannel nChannel = nm.getNotificationChannel(id);

        if (nChannel == null) {
            nChannel = new NotificationChannel(id, appName, importance);
            nChannel.setDescription(description);
            nm.createNotificationChannel(nChannel);
        }
    }
}

请记住,即使您使用不同的渠道ID,您的前台通知也会与其他通知具有相同的状态,因此它可能会与其他人一起隐藏。使用不同的组来避免它。

答案 1 :(得分:13)

问题是我使用的是Android O,它需要更多信息。这是android O的成功代码。

    mNotifyManager = (NotificationManager) mActivity.getSystemService(Context.NOTIFICATION_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) createChannel(mNotifyManager);
    mBuilder = new NotificationCompat.Builder(mActivity, "YOUR_TEXT_HERE").setSmallIcon(android.R.drawable.stat_sys_download).setColor
            (ContextCompat.getColor(mActivity, R.color.colorNotification)).setContentTitle(YOUR_TITLE_HERE).setContentText(YOUR_DESCRIPTION_HERE);
    mNotifyManager.notify(mFile.getId().hashCode(), mBuilder.build());

@TargetApi(26)
private void createChannel(NotificationManager notificationManager) {
    String name = "FileDownload";
    String description = "Notifications for download status";
    int importance = NotificationManager.IMPORTANCE_DEFAULT;

    NotificationChannel mChannel = new NotificationChannel(name, name, importance);
    mChannel.setDescription(description);
    mChannel.enableLights(true);
    mChannel.setLightColor(Color.BLUE);
    notificationManager.createNotificationChannel(mChannel);
}

答案 2 :(得分:2)

如果以上方法均无效,则应检查通知ID是否为0 ... 惊喜!不能为0。

非常感谢@Luka Kama发表了this帖子

startForeground(0, notification); // Doesn't work...

startForeground(1, notification); // Works!!!

答案 3 :(得分:1)

如果您定位的Android 9(Pie)api级别为28,并且高于清单文件中的FOREGROUND_SERVICE权限,请参见此链接:https://developer.android.com/about/versions/pie/android-9.0-migration#bfa

答案 4 :(得分:0)

对我来说,所有设置都正确(还添加了FOREGROUND_SERVICE权限以进行显示), 但是我只需要卸载该应用并重新安装。

答案 5 :(得分:-2)

就我而言,这是由我使用IntentService引起的。

简而言之,如果你想要一个前台服务,那么继承Service