startForegroundService-无法使用setContentIntent + addAction?

问题描述

不幸的是,有关我的startForegroundService通知的另一个问题...我确实搜索了:

我的前台服务运行良好。我想在此通知添加一些操作。首先,当用户单击通知时将其发送到MainActivity并添加“ Quit” addAction。

这是我用来创建通知代码段:

    Intent intent = new Intent(this,MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this,intent,0);

    notificationmanager notificationmanager = (notificationmanager) getSystemService(NOTIFICATION_SERVICE);
    String channelId = getNotificationChannel(notificationmanager);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this,channelId);
    Notification notification = notificationBuilder.setongoing(true)
            .setCategory(NotificationCompat.CATEGORY_SERVICE)
            .setSmallIcon(R.drawable.ic_notif_icon)
            .setContentTitle("My app")
            .setContentText("Background service is running...")
            .setContentIntent(pendingIntent)
            .build();

    startForeground(13365,notification);

使用上面的通知显示很好,但是单击它不会有任何结果。我也尝试使用addAction,也没有。我知道添加addAction时的语法有些不同(.... Action.Builder)。

我正在前台服务的onCreate处理程序中创建通知。在SDK 26上运行。

startForeground通知可以附加setContentIntent / addAction吗?

谢谢!

解决方法

已解决:我已在应用程序的其他位置替换了通知,并且未在其中添加意图。

Do!