OneSignal / OneSignal-Flutter-SDK

OneSignal is a free push notification service for mobile apps. This plugin makes it easy to integrate your flutter app with OneSignal
https://www.onesignal.com
Other
606 stars 205 forks source link

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

Open davetebonfire0302 opened 1 year ago

davetebonfire0302 commented 1 year ago

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?

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

bugrevealingbme commented 1 year ago

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

Davete0302 commented 1 year ago

Switched to firebase messaging.

tovidd commented 11 months ago

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 commented 6 months ago

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 commented 6 months ago

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 commented 5 months ago

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 commented 2 months ago

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 commented 2 months ago

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

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 commented 3 weeks ago

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