如何从前台服务启动Flutter应用程序

时间:2020-06-07 18:35:17

标签: java android flutter service

我有一个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应用?谢谢。

1 个答案:

答案 0 :(得分:1)

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

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