[英]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 中創建了一個類,在 MainActivity.java 旁邊
並將其添加到 android>app>main 文件夾中的 AndroidMainfest.xml 中:
<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 和所有操作后它是否沒有顯示任何錯誤。
但它似乎無法識別類方法和東西:
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
那么問題出在哪里,或者這是錯誤的做法,我是否還必須為顫振創建一個方法通道和本機代碼以在這種情況下進行通信。
在 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;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.