【问题标题】:Can't swipe-delete a notification from service无法从服务中滑动删除通知
【发布时间】:2017-01-30 11:11:44
【问题描述】:

我有一个服务,它下载数据,在一个单独的进程中运行(这样当应用程序关闭时它不会死/重新启动)并显示一个通知它的进度。如果用户滑动删除通知,我希望能够停止服务,但到目前为止还无法做到。相关代码如下:

DatabaseDownloadService.java

public class DatabaseDownloadService extends Service
{
    private final static int NOTIFICATION_ID = 1337;
    private final static String NOTIFICATION_DISMISSAL_TAG = "my_notification_dismissal_tag";
    private NotificationManager mNotificationManager;

    @Override
    public void onCreate()
    {
        super.onCreate();

        mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = getNotification("Downloading database...");
        startForeground(NOTIFICATION_ID, notification);

        startDownloadingStuff();
    }

    private Notification getNotification(String text)
    {
        NotificationDismissedReceiver receiver = new NotificationDismissedReceiver();
        registerReceiver(receiver, new IntentFilter(NOTIFICATION_DISMISSAL_TAG));

        Intent intent = new Intent(this, NotificationDismissedReceiver.class);
        PendingIntent deleteIntent = PendingIntent.getBroadcast(this, NOTIFICATION_ID, intent, 0);

        return new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("My Awesome App")
                .setContentText(text)
                .setDeleteIntent(deleteIntent)
                .build();
    }

    public class NotificationDismissedReceiver extends BroadcastReceiver
    {
        @Override
        public void onReceive(Context context, Intent intent)
        {
            int notificationId = intent.getExtras().getInt(NOTIFICATION_DISMISSAL_TAG);
            Toast.makeText(context, "Download cancelled", Toast.LENGTH_SHORT).show();

            // Do more logic stuff here once this works...
        }
    }
}

AndroidManifest.xml

<application
    ... properties and activities go here...>

    <service
        android:name=".DatabaseDownloadService"
        android:process=":dds_process"
        android:enabled="true"/>

    <receiver
        android:name="com.myapp.DatabaseDownloadService$NotificationDismissedReceiver"
        android:exported="false"/>

</application>

据我所知,.setDeleteIntent() 应该使通知滑动删除,然后应该发送一个广播,然后应该由我的 NotificationDismissedReceiver 捕获。但是,就目前而言,我什至无法滑动删除通知,而且我从来没有看到“下载取消”Toast...

【问题讨论】:

  • 我认为您在使用startForeground时遇到了平台限制:stackoverflow.com/questions/26576872/…
  • 根据官方文档:Make this service run in the foreground, supplying the ongoing notification to be shown to the user while in this state. 但是您可以创建一个待处理的 Intent,一旦单击触发实际停止服务的待处理 Intent 就关闭该服务 - 从而删除通知

标签: android notifications broadcastreceiver


【解决方案1】:

您可以在前台调用停止服务,传递false,表示不删除通知。对于 Android N 及以上版本,您也可以传递 STOP_FOREGROUND_DETACH。

stopForeground(false);

之后您也可以自行停止服务。

stopSelf();

【讨论】:

    【解决方案2】:

    不要使用 startForeground(),而是使用:

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify("tag", NOTIFICATION_ID, notification);
    

    【讨论】:

    • 我使用 startForeground() 的原因是,即使用户关闭应用程序,我也希望服务继续执行其操作。使用 notify() 会使服务在应用关闭时重新启动,这不是我想要的行为。
    • 您是否尝试在构建通知时明确设置 setOngoing(false) ?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多