OneSignal / OneSignal-Xamarin-SDK

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

How do you process push notifications received while the app is in the background? #351

Closed Osmosis311 closed 1 year ago

Osmosis311 commented 1 year ago

How can we help?

Is there a way to receive an event / handle a push notification that's received when my app is in the background? I'm using the OneSignal Xamarin SDK, and in the documentation it says to add a notification service extension, and inherit from this class OSRemoteNotificationReceivedHandler, but that class isn't found anywhere in the SDK.

Is there a way to do this in the Xamarin SDK?

Code of Conduct

brismithers commented 1 year ago

Hi @Osmosis311 apologies for the delay! The documentation for adding a NotificationServiceExtension is an iOS-specific step. There is no equivalency on Android that can be exploited by the Xamarin SDK.

Note I believe there is a path where you can create an Android native NotificationServiceExtension,it would be written in Java/Kotlin and would extend the OSRemoteNotificationReceivedHandler class you reference above. The underlying technical reason for not having a Xamarin SDK equivalent is, we have not found a way to "get into" the Xamarin environment when your app is driven through the receiving of a notification while the app is in the background/not open.

Staying within the Xamarin SDK, you are limited to the OneSignal.Default. NotificationWillShow event, which unfortunately is limited to notifications received while the app is in the foreground.

Please let me know if you have any follow up questions. Thanks!

brismithers commented 1 year ago

@Osmosis311 would you be able to share your use case for why you need to receive control when receiving a notification and your app isn't in the foreground?

Osmosis311 commented 1 year ago

Yes. Critical notifications. If the user has their phone completely silenced, for specific types of critical alerts, I need to be able to run code when a notification is received and my app isn’t in the foreground that utilizes MediaManager to play a sound to get the users attention (this is of course with their consent. The use case here is volunteer emergency responders. For example, they may be in an office setting and don’t want to be disturbed except for emergencies happening nearby).

I think I figured it out, though. I registered a (generic) notification receiver and that seems to be working exactly the way I want.

On Tue, Jan 17, 2023 at 10:15 AM Brian Smith @.***> wrote:

@Osmosis311 https://github.com/Osmosis311 would you be able to share your use case for why you need to receive control when receiving a notification and your app isn't in the foreground?

— Reply to this email directly, view it on GitHub https://github.com/OneSignal/OneSignal-Xamarin-SDK/issues/351#issuecomment-1385583279, or unsubscribe https://github.com/notifications/unsubscribe-auth/AACX6MEOAWWICQGKPXUP6FLWS2ZS3ANCNFSM6AAAAAATBL2ANI . You are receiving this because you were mentioned.Message ID: @.***>

brismithers commented 1 year ago

thanks for the feedback @Osmosis311, I'll pass that along internally as a scenario to account for. Yes a generic notification receiver would work as well good thought. If you're up for it please post a version of what you did here and I can try to incorporate it as a solution into our docs.

Tock3r commented 1 year ago

I need that badly too. @Osmosis311, your solution would be very helpful. :) In my use case, security guards receive an alarm notification with status messages that they have to acknowledge. If they ignore the notification, the app will periodically remind them with a warning sound.

Osmosis311 commented 1 year ago

Here's the code for the receiver.

Add this to MainActivity.cs in the OnCreate function:

IntentFilter filter = new IntentFilter("com.google.android.c2dm.intent.RECEIVE"); RegisterReceiver(new PushReceiver(), filter);

Tock3r commented 1 year ago

Awesome, it works. Thank you. I had it similar, I only registered the receiver in the launch activity and not in main :D