在后台使用 OneSignal PushNotifications 进行 Flutter

ino*_*noi 5 onesignal flutter

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

所以首先我在 android>app>main>java>..>NotificationsService.java 中创建了一个类,位于 MainActivity.java 旁边

并将其添加到 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>
Run Code Online (Sandbox Code Playgroud)

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

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

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;
   }
}
Run Code Online (Sandbox Code Playgroud)

显示以下错误:

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
Run Code Online (Sandbox Code Playgroud)

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

小智 1

在 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>
Run Code Online (Sandbox Code Playgroud)

在 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;
   }

}
Run Code Online (Sandbox Code Playgroud)