【问题标题】:How to start Flutter application from foreground service如何从前台服务启动 Flutter 应用程序
【发布时间】:2020-06-07 18:35:17
【问题描述】:

我有一个 Flutter 应用,它运行一个永无止境的前台服务,它的定义如下:

    private void startForeground() {
    Intent notificationIntent = new Intent(this, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
            notificationIntent, 0);
    startForeground(NOTIF_ID, new NotificationCompat.Builder(this,
            NOTIF_CHANNEL_ID)
            .setOngoing(true)
            .setContentTitle("service")
            .setContentText("Service is running background")
            .setContentIntent(pendingIntent)
            .build());
}

然后我开始这样的服务:

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
    Notification notification = notificationBuilder.setOngoing(true)
            .setSmallIcon(R.drawable.smile)
            .setContentTitle("App is running in background")
            .setPriority(NotificationManager.IMPORTANCE_MIN)
            .setCategory(Notification.CATEGORY_SERVICE)
            .build();
    startForeground(2, notification);

我的前台服务一直在显示通知。

是否可以在用户按下前台服务显示的通知时启动 Flutter 应用程序,即使 Flutter 应用程序没有在后台运行?谢谢。

【问题讨论】:

    标签: java android flutter service


    【解决方案1】:

    要在单击通知时启动Activity,您可以使用PendingIntent。您已经在第一个示例中实现了此功能,但这里有一些链接。 Android docsStackoverflow question

    Intent notificationIntent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
                notificationIntent, 0);
    builder.setContentIntent(pendingIntent);
    

    【讨论】:

    • 谢谢。但是 Flutter 应用呢,我该如何启动呢?
    • 对不起,我想我理解你。我需要启动 MainActivity,它扩展了 FlutterActivity..
    猜你喜欢
    • 2021-06-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-30
    • 1970-01-01
    • 2020-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多