How to get notified even when app is closed using Xamarin Forms?

I'm a new developer to Xamarin Forms and I want to notify my user even when the app is closed as other apps do.

I have a web app where they can send notifications (like a campaign) to all the users registered to this campaign. My web app connects to Azure Notifications Hub which connects to Firabase Cloud Messaging. Everything works fine most of the time.

If my application is running, the notifications appear:

enter image description here

If I go to the main screen (but app is still running) it works.

enter image description here

enter image description here

When I completely close the app, the notifications stop. I need to notify my users even if they completely close the app.

How can I do this?

Upvotes: 1

Views: 1452

Answers (3)

Takeo Nishioka
Takeo Nishioka

Reputation: 389

For my case, I upgraded nuget packages to be latest solve this issue.

Upgrade nuget packages:

Xamarin.Firebase.Messaging(v123.0.8)

Xamarin.GooglePlayServices.Base(v118.1.0).

Xamarin.Firebase.Iid

This solved the issue the notification does not show while an app is background and closed.

Please do not forget to update AndroidManifest.xml file.

Under manifest section

<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

Under application section

<receiver android:name="com.google.firebase.iid.FirebaseInstanceIdReceiver" android:exported="true" android:permission="com.google.android.c2dm.permission.SEND">
      <intent-filter>
      <action android:name="com.google.android.c2dm.intent.RECEIVE" />
      <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
      <category android:name="${applicationId}" />
      </intent-filter>
</receiver>  

Upvotes: 0

Glorfindel
Glorfindel

Reputation: 22641

I had a similar problem, and the interesting thing was that a native app built with Android Studio didn't have this problem. After several Google searches I stumbled upon this answer by @AdrianJakubcik-3866 on Microsoft Q&A:

Long story short anyone who's experiencing the same problem as the title of this post says the original problem that was discussed here. The solution for me was that after closing the app the debug session in Visual Studio is terminated thus also the google service which is responsible for receiving the notifications. Now to start it up again all I had to do is go to my emulator or physical device and start up the app manually not through Visual Studio, after it's fully loaded you can close it down or remove it from the background processes, and now your notifications should be coming once again.

That did the trick for me.

Upvotes: 2

Use the plugin localnotificationsplugin.

With it, you can schedule a notification.

Like this:

CrossLocalNotifications.Current.Show(“Testing notifications”, “Message text”, 1, DateTime.Now.AddDays(1));

Reference in portuguese-BR (If you are brazilian) here:

https://medium.com/@bertuzzi/meu-plugin-minha-vida-notificações-locais-a4bcd9fa1594

Upvotes: 0

Related Questions