Flutter with OneSignal PushNotifications 在后台

Flutter with OneSignal PushNotifications in background

我正在尝试让我的 flutter 应用程序在后台处理通知。我正在使用带有 onesignal 的推送通知,当我滑动我的应用程序并关闭它时,它不再调用 onReceiveHandler。所以我读到了 NotificationExtenderService: https://documentation.onesignal.com/docs/service-extensions#section-notification-extender-service

所以我首先在 android>app>main>java>..>NotificationsService.java 中创建了一个 class,在 MainActivity.java[= 旁边14=]

并将其添加到 AndroidMainfest.xml 中,它位于 android>app>main 文件夹中:

<service
   android:name=".YOUR_CLASS_NAME"
   android:permission="android.permission.BIND_JOB_SERVICE"
   android:exported="false">
   <intent-filter>
      <action android:name="com.onesignal.NotificationExtender" />
   </intent-filter>
</service>

因此,在没有做任何其他工作的情况下,我尝试 运行 我的应用程序只是为了看看它是否没有显示任何错误,在完成 flutter clean 等所有操作之后。

但它似乎无法识别 class 方法和内容:

import com.onesignal.OSNotificationPayload;
import com.onesignal.NotificationExtenderService;

public class NotificationExtenderBareBonesExample extends NotificationExtenderService {
   @Override
   protected boolean onNotificationProcessing(OSNotificationReceivedResult receivedResult) {
      // Read properties from result.

      // Return true to stop the notification from displaying.
      return false;
   }
}

显示以下错误:

error: cannot find symbol
 protected boolean onNotificationProcessing(OSNotificationReceivedResult receivedResult) {
                                            ^
  symbol:   class OSNotificationReceivedResult
  location: class OneSignalSilentNotificationHandler
1 error

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 8s
Finished with error: Gradle task assembleDebug failed with exit code 1

那么问题出在哪里,或者这是错误的做法,我还必须为 flutter 创建一个方法通道和本机代码来为这种情况进行通信。

在AndroidMainfest.xml,里面:

<service
    android:name=".OneSignalNotificationExtender"
    android:permission="android.permission.BIND_JOB_SERVICE"
    android:exported="false">
    <intent-filter>
        <action android:name="com.onesignal.NotificationExtender" />
    </intent-filter>
</service>

在 android>app>main>java>..> 我在 MainActivity.java

旁边创建 OneSignalNotificationExtender.java

OneSignalNotificationExtender.java:

import com.onesignal.OSNotificationPayload;
import com.onesignal.OSNotificationReceivedResult;
import com.onesignal.NotificationExtenderService;

public class OneSignalNotificationExtender extends NotificationExtenderService  {
   @Override
   protected boolean onNotificationProcessing(OSNotificationReceivedResult receivedResult) {
      if (receivedResult != null) {




      } 

      // Returning true tells the OneSignal SDK you have processed the notification and not to display it's own.
      return false;
   }

}