inoi
inoi

Reputation: 129

Flutter with OneSignal PushNotifications in background

I am trying to allow my flutter app to process notifications on background. I am using push notifications with onesignal and when I swipe my app and close it, it does not call onReceiveHandler anymore. So I read about NotificationExtenderService: https://documentation.onesignal.com/docs/service-extensions#section-notification-extender-service

So firstly I created a class in android>app>main>java>..>NotificationsService.java, next to MainActivity.java

And also added this to AndroidMainfest.xml which is in the android>app>main folder:

<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>

So without doing any other work I tried running my application just to see if it doesn't show any errors, after doing flutter clean and all.

But it seems it doesn't recognize the class methods and stuff:

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

The following error is showing:

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

So where can the problem be, or is this the wrong way of doing it, and also do I have to create a method channel for flutter and the native code to communicate for this case.

Upvotes: 5

Views: 3933

Answers (1)

Ermindo Lopes
Ermindo Lopes

Reputation: 109

in AndroidMainfest.xml, inside :

<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>

inside android>app>main>java>..> i create OneSignalNotificationExtender.java next to MainActivity.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;
   }

}

Upvotes: 1

Related Questions