Unable To Cancel Foreground Notification

时间:2015-10-06 09:05:12

标签: android notifications foreground-service

I have the following code for a notification to a foreground service.

private Notification buildNotification(String title, String artist, Bitmap art, boolean isPlaying) {
        notificationBuilder = new NotificationCompat.Builder(getApplicationContext());
        notificationBuilder.setOngoing(true);
        notificationBuilder.setAutoCancel(true);  //TODO revert this to false depending on effect
        notificationBuilder.setSmallIcon(R.drawable.ic_stat_playing);

        PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0,
                new Intent(getApplicationContext(), Home.class), PendingIntent.FLAG_UPDATE_CURRENT);
        notificationBuilder.setContentIntent(pendingIntent);

        RemoteViews compactNotification = new RemoteViews(getApplicationContext().getPackageName(), R.layout.notfication_custom_layout);
        RemoteViews expandedNotification = new RemoteViews(getApplicationContext().getPackageName(), R.layout.notification_expanded_layout);

        Intent previousTrackIntent = new Intent(getApplicationContext(), MusicService.class);
        previousTrackIntent.setAction(MusicService.ACTION_REWIND);
        PendingIntent previousTrackPendingIntent = PendingIntent.getService(getApplicationContext(), 0, previousTrackIntent, 0);

        Intent playPauseTrackIntent = new Intent(getApplicationContext(), MusicService.class);
        playPauseTrackIntent.setAction(MusicService.ACTION_TOGGLE_PLAYBACK);
        PendingIntent playPauseTrackPendingIntent = PendingIntent.getService(getApplicationContext(), 0, playPauseTrackIntent, 0);

        Intent nextTrackIntent = new Intent(getApplicationContext(), MusicService.class);
        nextTrackIntent.setAction(MusicService.ACTION_SKIP);
        PendingIntent nextTrackPendingIntent = PendingIntent.getService(getApplicationContext(), 0, nextTrackIntent, 0);

        Intent stopServiceIntent = new Intent(getApplicationContext(), MusicService.class);
        stopServiceIntent.setAction(MusicService.ACTION_STOP);
        PendingIntent stopServicePendingIntent = PendingIntent.getService(getApplicationContext(), 0, stopServiceIntent, 0);

        if (isPlaying) {
            compactNotification.setImageViewResource(R.id.notification_base_play, R.drawable.ic_action_pause_dark);
            expandedNotification.setImageViewResource(R.id.notification_expanded_base_play, R.drawable.ic_action_pause_dark);
        } else {
            compactNotification.setImageViewResource(R.id.notification_base_play, R.drawable.ic_action_play_dark);
            expandedNotification.setImageViewResource(R.id.notification_expanded_base_play, R.drawable.ic_action_play_dark);
        }

        compactNotification.setTextViewText(R.id.notification_base_line_one, title);
        compactNotification.setTextViewText(R.id.notification_base_line_two, artist);

        expandedNotification.setTextViewText(R.id.notification_expanded_base_line_one, title);
        expandedNotification.setTextViewText(R.id.notification_expanded_base_line_two, artist);

        compactNotification.setOnClickPendingIntent(R.id.notification_base_play, playPauseTrackPendingIntent);
        expandedNotification.setOnClickPendingIntent(R.id.notification_expanded_base_play, playPauseTrackPendingIntent);

        compactNotification.setOnClickPendingIntent(R.id.notification_base_next, nextTrackPendingIntent);
        expandedNotification.setOnClickPendingIntent(R.id.notification_expanded_base_next, nextTrackPendingIntent);

        compactNotification.setOnClickPendingIntent(R.id.notification_base_previous, previousTrackPendingIntent);
        expandedNotification.setOnClickPendingIntent(R.id.notification_expanded_base_previous, previousTrackPendingIntent);

        compactNotification.setOnClickPendingIntent(R.id.notification_base_collapse, stopServicePendingIntent);
        expandedNotification.setOnClickPendingIntent(R.id.notification_expanded_base_collapse, stopServicePendingIntent);

        expandedNotification.setImageViewBitmap(R.id.notification_expanded_base_image, art);
        compactNotification.setImageViewBitmap(R.id.notification_base_image, art);

        notificationBuilder.setContent(compactNotification);

        Notification notification = notificationBuilder.build();

        if (Build.VERSION.SDK_INT >= 16)
            notification.bigContentView = expandedNotification;

        notification.flags = Notification.FLAG_FOREGROUND_SERVICE | Notification.FLAG_ONGOING_EVENT | Notification.FLAG_AUTO_CANCEL;

        return notification;
    }

The notification builds and updates as it should, however, when i try to call the stopForeground(true) or stopSelf() methods on the service, the notification persists.

How can i remove this notification when i stop the service?

1 个答案:

答案 0 :(得分:0)

您服务的onStartCommand的返回值是多少?在我的情况下,我将值从START_STICKY更改为START_NOT_STICKY,然后它可以正常工作。