Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Receive notification when app is closed/background. #618

Open
3 tasks done
davetebonfire0302 opened this issue Nov 16, 2022 · 9 comments
Open
3 tasks done

Comments

@davetebonfire0302
Copy link

davetebonfire0302 commented Nov 16, 2022

What happened?

Followed instructions here and created a .java file in com.package.name now I have 2 files there MainActivity.java And NotificationServiceExtension.java

  <meta-data android:name="com.onesignal.NotificationServiceExtension"
            android:value="com.bonfire.cdis.NotificationServiceExtension" />

image

Steps to reproduce?

Use flutter 3.4.2
Follow instructions here https://documentation.onesignal.com/docs/service-extensions#android-notification-service-extension
Run the app

What did you expect to happen?

Expected to successfully run the app and receive notification when app is in foreground or background.

OneSignal Flutter SDK version

3.4.2

Which platform(s) are affected?

  • iOS
  • Android

Relevant log output

Launching lib\main.dart on sdk gphone x86 64 in debug mode...
Running Gradle task 'assembleDebug'...
D:\caps\cdis\android\app\src\main\java\com\bonfire\cdis\NotificationServiceExtension.java:24: error: cannot find symbol
            builder.setColor(new BigInteger("FF00FF00", 16).intValue());
                                 ^
  symbol:   class BigInteger
  location: class NotificationServiceExtension
D:\caps\cdis\android\app\src\main\java\com\bonfire\cdis\NotificationServiceExtension.java:26: error: cannot find symbol
            Spannable spannableTitle = new SpannableString(notification.getTitle());
            ^
  symbol:   class Spannable
  location: class NotificationServiceExtension
D:\caps\cdis\android\app\src\main\java\com\bonfire\cdis\NotificationServiceExtension.java:26: error: cannot find symbol
            Spannable spannableTitle = new SpannableString(notification.getTitle());
                                           ^
  symbol:   class SpannableString
  location: class NotificationServiceExtension
D:\caps\cdis\android\app\src\main\java\com\bonfire\cdis\NotificationServiceExtension.java:27: error: cannot find symbol
            spannableTitle.setSpan(new ForegroundColorSpan(Color.RED),0,notification.getTitle().length(),0);
                                       ^
  symbol:   class ForegroundColorSpan
  location: class NotificationServiceExtension
D:\caps\cdis\android\app\src\main\java\com\bonfire\cdis\NotificationServiceExtension.java:27: error: cannot find symbol
            spannableTitle.setSpan(new ForegroundColorSpan(Color.RED),0,notification.getTitle().length(),0);
                                                           ^
  symbol:   variable Color
  location: class NotificationServiceExtension
D:\caps\cdis\android\app\src\main\java\com\bonfire\cdis\NotificationServiceExtension.java:30: error: cannot find symbol
            Spannable spannableBody = new SpannableString(notification.getBody());
            ^
  symbol:   class Spannable
  location: class NotificationServiceExtension
D:\caps\cdis\android\app\src\main\java\com\bonfire\cdis\NotificationServiceExtension.java:30: error: cannot find symbol
            Spannable spannableBody = new SpannableString(notification.getBody());
                                          ^
  symbol:   class SpannableString
  location: class NotificationServiceExtension
D:\caps\cdis\android\app\src\main\java\com\bonfire\cdis\NotificationServiceExtension.java:31: error: cannot find symbol
            spannableBody.setSpan(new ForegroundColorSpan(Color.BLUE),0,notification.getBody().length(),0);
                                      ^
  symbol:   class ForegroundColorSpan
  location: class NotificationServiceExtension
D:\caps\cdis\android\app\src\main\java\com\bonfire\cdis\NotificationServiceExtension.java:31: error: cannot find symbol
            spannableBody.setSpan(new ForegroundColorSpan(Color.BLUE),0,notification.getBody().length(),0);
                                                          ^
  symbol:   variable Color
  location: class NotificationServiceExtension
9 errors

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 7s
Exception: Gradle task assembleDebug failed with exit code 1

Code of Conduct

  • I agree to follow this project's Code of Conduct
@bugrevealingbme
Copy link

I have the same error. Why isn't there an update?

@Davete0302
Copy link

Switched to firebase messaging.

@tovidd
Copy link

tovidd commented Aug 2, 2023

After hours long, finally my app can received background notifications and enter the apps once clicking on the notifications bar !

Just don't copy all the code section here to your project https://documentation.onesignal.com/docs/service-extensions#android-notification-service-extension. You need only the code below to make your background notifications works in flutter.

package com.companyName.applicationName;

import android.content.Context;
import com.onesignal.OSNotification;
import com.onesignal.OSNotificationReceivedEvent;
import com.onesignal.OneSignal.OSRemoteNotificationReceivedHandler;

@SuppressWarnings("unused")
public class NotificationServiceExtension implements OSRemoteNotificationReceivedHandler {
    @Override
    public void remoteNotificationReceived(Context context, OSNotificationReceivedEvent notificationReceivedEvent) {
        OSNotification notification = notificationReceivedEvent.getNotification();
        notificationReceivedEvent.complete(notification);
    }
}

Then in Manifest.xml add below meta data in application not activity tag.

<application>
  <activity/>
  <meta-data
    android:name="com.onesignal.NotificationServiceExtension"
    android:value="com.companyName.applicationName.NotificationServiceExtension" />
</application>

On flutter 3.12.0 & onesignal_flutter: ^3.5.1

@caini1213
Copy link

After hours long, finally my app can received background notifications and enter the apps once clicking on the notifications bar !

Just don't copy all the code section here to your project https://documentation.onesignal.com/docs/service-extensions#android-notification-service-extension. You need only the code below to make your background notifications works in flutter.

package com.companyName.applicationName;

import android.content.Context;
import com.onesignal.OSNotification;
import com.onesignal.OSNotificationReceivedEvent;
import com.onesignal.OneSignal.OSRemoteNotificationReceivedHandler;

@SuppressWarnings("unused")
public class NotificationServiceExtension implements OSRemoteNotificationReceivedHandler {
    @Override
    public void remoteNotificationReceived(Context context, OSNotificationReceivedEvent notificationReceivedEvent) {
        OSNotification notification = notificationReceivedEvent.getNotification();
        notificationReceivedEvent.complete(notification);
    }
}

Then in Manifest.xml add below meta data in application not activity tag.

<application>
  <activity/>
  <meta-data
    android:name="com.onesignal.NotificationServiceExtension"
    android:value="com.companyName.applicationName.NotificationServiceExtension" />
</application>

On flutter 3.12.0 & onesignal_flutter: ^3.5.1

how about the latest version v5.0.4?

@yigtkaya
Copy link

yigtkaya commented Jan 4, 2024

After hours long, finally my app can received background notifications and enter the apps once clicking on the notifications bar !
Just don't copy all the code section here to your project https://documentation.onesignal.com/docs/service-extensions#android-notification-service-extension. You need only the code below to make your background notifications works in flutter.

package com.companyName.applicationName;

import android.content.Context;
import com.onesignal.OSNotification;
import com.onesignal.OSNotificationReceivedEvent;
import com.onesignal.OneSignal.OSRemoteNotificationReceivedHandler;

@SuppressWarnings("unused")
public class NotificationServiceExtension implements OSRemoteNotificationReceivedHandler {
    @Override
    public void remoteNotificationReceived(Context context, OSNotificationReceivedEvent notificationReceivedEvent) {
        OSNotification notification = notificationReceivedEvent.getNotification();
        notificationReceivedEvent.complete(notification);
    }
}

Then in Manifest.xml add below meta data in application not activity tag.

<application>
  <activity/>
  <meta-data
    android:name="com.onesignal.NotificationServiceExtension"
    android:value="com.companyName.applicationName.NotificationServiceExtension" />
</application>

On flutter 3.12.0 & onesignal_flutter: ^3.5.1

how about the latest version v5.0.4?

still same issue

@Kov-Oleg
Copy link

Facing the same issue in version of onesignal_flutter ^5.0.4. I was testing in a real device running Android 13. Since I'm killing(closing completely) the app my application stops receiving any push notifications. Can't find any samples or articles about that in official documentation or in examples.
Can you please provide some workarounds?

@GainUpTrading
Copy link

My problem is I am getting notification in background but on click event of notification I want to navigate to specific screen but everytime I click on the notification it navigate to home screen instead of specific screen.

@jkasten2
Copy link
Member

@caini1213 @yigtkaya The same link Android Notification Service Extension has been update for the 5.x.x SDK.


@Kov-Oleg

Since I'm killing(closing completely) the app my application stops receiving any push notifications.

This should only be a problem if your app is "force stopped". How are you killing your app?

  • Swiping away the app - Most devices count this as a normal kill, not a "force stop".
  • Stopping your app via VS Code or Android Studio - This is know to "force stop" the app instead of a standard kill.

See OneSignal's Android App is Force Stopped documentation for more details on this.


@GainUpTrading
The issue you are seeing isn't related to this topic, could you create a new issue instead?

@nikunjpanchall
Copy link

I have the same error. Why isn't there an update?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants