OneSignal / OneSignal-Android-SDK

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

[Bug]: onNotificationReceived() not called #2194

Closed himadri1266 closed 1 month ago

himadri1266 commented 2 months ago

What happened?

I am unable to detect notifications in the onNotificationReceived() method of INotificationServiceExtension. This issue occurs whether the app is in the background or foreground. I am using the latest version of the OneSignal SDK, version "5.1.21".

Below is the code I have integrated:

1. build.gradle (app level): implementation("com.onesignal:OneSignal:5.1.21")

2. AndroidManifest.xml:

<meta-data
     android:name="com.onesignal.NotificationServiceExtension"
     android:value="com.myapp.notification.NotificationService" />

3. NotificationService class:

package com.myapp.notification;

import android.util.Log;

import com.onesignal.notifications.IDisplayableMutableNotification;
import com.onesignal.notifications.INotificationReceivedEvent;
import com.onesignal.notifications.INotificationServiceExtension;

public class NotificationService implements INotificationServiceExtension {
    private String TAG = "NotificationService";

    @Override
    public void onNotificationReceived(INotificationReceivedEvent event) {
        IDisplayableMutableNotification notification = event.getNotification();
        Log.d(TAG, "Title " + notification.getTitle());
        Log.d(TAG, "Body " + notification.getBody());

       event.preventDefault();
    }
}

Steps to reproduce?

1.Install the app on a mobile device.
2.Enable the notification service.
3.Send a test notification from the OneSignal dashboard.
4.Check the onNotificationReceived() method.

What did you expect to happen?

Whenever a notification arrives on the mobile device, the onNotificationReceived() method will be executed and will handle the API call from within that method.

OneSignal Android SDK version

5.1.21

Android version

15, 14, 13, 12

Specific Android models

No response

Relevant log output

No response

Code of Conduct

faisal-kabir commented 1 month ago

I am having the same problem, "onNotificationReceived" method is not called in the background.

jinliu9508 commented 1 month ago

Hello @himadri1266 @faisal-kabir, were you able to actually see the notification on your device after you send it from the dashboard? Are you able to check the subscribed status in debug and/or the dashboard?

faisal-kabir commented 1 month ago

Hi @jinliu9508

Hello @himadri1266 @faisal-kabir, were you able to actually see the notification on your device after you send it from the dashboard? Are you able to check the subscribed status in debug and/or the dashboard?

Yes, Everything is showing properly on the OneSignal Dashboard, and the status is subscribed. I am able to receive notifications when the app is in the foreground, but notifications do not come when the app is in the background.

simon1867 commented 1 month ago

I haven't updated to version 5 yet because onNotificationReceived() never gets called. Everything works great using 4.8.10. But when I update to 5.x.x I stop getting notifications.

jinliu9508 commented 1 month ago

@faisal-kabir @simon1867 I am unable to reproduce this issue as the notification works for me regardless whether my app is active or killed. I think it might be a setup issue. A few things that you might want to pay attention to when you setup the service extension following this documentation is that make sure the @keep tag is added and the manifest class value is fully name spaced.

You may share how you setup the service extension and I will be glad to help!

jinliu9508 commented 1 month ago

@himadri1266 I think your NotificationServiceClass is missing a '@keep' annotation. Can you try adding it and let me know how that works out for you?

simon1867 commented 1 month ago

Wow, well it's working for me now. I pulled in the latest version 5.1.23 and I added the @keep annotation to my service extension class.

Thanks @jinliu9508

jinliu9508 commented 1 month ago

Closing this issue as it looks like a setup issue. A few things that anyone might want to pay extra attention to when you setup the service extension following this documentation is that make sure the @keep tag is added and the manifest class value is fully name spaced. If this is still an issue, please open a new report with updated information.