-
Notifications
You must be signed in to change notification settings - Fork 161
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
Xamarin.Forms Tutorial for the Push Notification Plugin breaks Android Notifications when App is Not Running or Backgrounded (Fix Included in Comments) #39
Comments
Still it won't give me push notifications in the background |
Nevermind, I missed |
|
Hi brminnick, weird - I am not seeing any dependency from the plug-in itself; my guess is, in your implementation of CrossPushNotificationListener, you have created the Dependency on Forms. IMHO: The tutorial (Adding Push Notification Post to your Xamarin Forms Application) is correct. In my code, I don't do anything but to notify the user; my registration with azure happens when the user logs in to the system.. by that time, Forms is already initialized. |
@alexbleks I ran into the same issue but don't really understand how you fixed it... Why should adding UPDATE: fixed the crash (I had some Forms-specific code in my IPushNotificationListener.OnMessage). The
|
@MKahmen,
We cannot call the Xamarin.Forms Dependency Service in the Because Xamarin.Forms is initialized in the CrossPushNotification.cs
|
@brminnick Thank you for your explanation. Do you have a solution / an example for an implementation which will run PushService after restart, killing, ...? |
This is how I got it working:
|
@alexbleks Thank you for your reply. I still don't understand the relationship between your manifest/receiver and the Service/Application-class. Xamarin doc says:
Is there a way to program this receiver around this PushService? I just don't like to change the Manifest if it's not 100% necessary. Regards |
Update: Seems like CrossPushNotification.Initialize can take an instant of listener; I had a problem of playing audio before because of the whole dependency issue; but now, I can pass the audio service from Initialize to use. |
Is there any progress? I tried your suggestions but only fixed the crash. When the app is closed, no push notifications are received. I tried adding into the manifest file but didn't help. Anything else I could do? |
sry for pushing this topic again but it drives me totally crazy.... Any idea how to debug this? Should there be more entries in logcat? Ok, just figured out that the service is still running in the background. but push is not received or at least highlighted... |
ok finally I found the bug in my code and now it's working like a charm. |
Hi Guys, does this work with Firebase? I receive the notification on Android but there is no title content, only shows the app name on top? |
@CR4567 can you provide a sample project that is working? Thanks. |
Install the nuget package in all your projects if using PCL. You need to register the device, either create a popup or button which calls:
Just make sure place this code in your MainApplication as below, to receive notifications in background: `
public override void OnCreate()
}` |
@iManchaDev is completely right. Same did the trick for me. Hope this helps. If not, please give us a little more information about how you implemented the service and what's exactly your problem. |
@iManchaDev @CR4567 thanks for the reply! Forms project - App
` Forms project - CrossPushNotificationListener:
` Android project - AzureClient
` Android project - PushNotificationAppStarter
` And this is the payload I'm sending through Azure's Test Send Or Everything works while my app is running in foreground or in background. Am I missing something? |
If you're debugging app, this happens sometimes when app is terminated. Try release mode and see if it works. |
@iManchaDev I've tried in release mode and even signing and distributing my apk in ad hoc mode. |
for me your code looks correct. Can't see an issue. PS: I don't use the Azure service so I'm not sure about that part but still looks good to me. |
How can I check if my service is still running after closing the app? |
there is an option in the developer settings of android... I guess it was something like service statistics... |
There's nothing related to my app in there, even when the app is running. :( |
? but that's weird... there should be something with your app name... |
Sorry, I was looking at the wrong place. |
It worked!!!! Thanks a lot! |
:-D oh, nice. Tough problem :-) But good you found it. |
@MKahmen I know it's been a while since your last post here, but did you ever find a solution? Even when I add the manifest part in, I still cannot receive notifications when the app is not running / sleeping. |
@lanceking Make sure you implement the code in your MainApplication.cs file rather than MainActivity.cs |
@lanceking I decided to implement my own code based on Xamarin.iOS and Xamarin.Android examples. I had to implement custom notification styles anyways. Sorry, but can't help anymore. |
@lanceking @MKahmen I recommend creating your own implementation too, because the Push Notification Plugin doesn't provide much added-value. The Xamarin Evolve app implements Push for both iOS and Android and it's what I reference when implementing push notifications now: |
Thanks @brminnick @MKahmen @iManchaDev ! I finally got things working. My issue was due to it being a debug build. Pushed an AdHoc Release build and things worked fine. If I run into more issues, I'll def. refer to the Evolve app (great app btw, not sure why I didn't think to reference it first). |
Hi, @iManchaDev and @CR4567 I know this an old issue but I haven't been able to make it work, I've done as you guys say setting a MainApplications.cs with the [Application] tag but I still can't get the message sent through FCM Console not sure why, I'm using the senderID given by the console and it's the same, and I do get the register ID of the device using In advance thanks for help given. |
@William-H-M You may want to see my reply on the following thread: I appreciate what the developers of this plugin have provided free of charge to all users, however with lack of support at times I had to make a decision going forward and haven't looked back. Good Luck |
Hi @iManchaDev thanks for that It'll be great if you give anything about it from where to start or if you got a quick example of it, it will be of a lot of help, while that I'll check it out and see if I can get it work. Thanks! |
Hi I am getting notification and bind it with text on activity but not able to get notification using notification builder in notification panel. |
tl;dr The Xamarin.Forms'
DependancyService
prevents Android apps from receiving push notifications when not running or backgrounded.I recommend updating the tutorial on the Push Notification Plugin's GitHub page to help prevent confusion from other developers in the future: Adding Push Notification Post to your Xamarin Forms Application. I followed this tutorial to implement the Push Notification Plugin, and it lead me to this road block: my Android app could not receive push notifications when it was not running or backgrounded.
After following this step-by-step tutorial to setup the Push Notification Plugin on Xamarin.Forms, the iOS push notifications work flawlessly and the Android devices receive push notifications when the app is running or backgrounded. But on Android, if I force-close the app so that it is no longer running (e.g. press the Recent Apps button, and swipe away my app), and then send a push notification, the device shows this error message: “Unfortunately, [App Name] has stopped” (screen shot below).
The error below was appearing because I did not call the
CrossPushNotification.Initialize<CrossPushNotificationListener>(Keys_Constants.GOOGLE_APIs_ID);
method inside[Application] OnCreate()
as is recommended on the Push Notification Plugin's GitHub page.But, the reason why I didn’t/couldn’t include this method in
[Application] OnCreate()
was becauseCrossPushNotificationListener
requires the Xamarin.Forms’DependencyService
, which is impossible in[Application] OnCreate()
because theDependencyService
hasn’t been initialized yet;global::Xamarin.Forms.Forms.Init(this, bundle);
is called in Android'sMainActivity
which hasn’t been yet instantiated when the code is executing the[Application]
class.The remedy was to break apart my cross-platform implementation of the
CrossPushNotificationListener
into a platform-specificNotificationListener
that now live in the platform-specific PCL (code below).I recommend updating the tutorial on the GitHub page to implement this plugin for Xamarin.Forms. The tutorial has the user create a
CrossPushNotificationListener
class that uses Xamarin.Forms’DependencyService
. This tutorial works flawlessly to receive push notifications on iOS, but on Android, it will only allow the device to receive push notifications while the app is running or backgrounded.Error When Sending a Push Notification to a App That Is Not Running and Not Backgrounded (Before implementing the fix)
Updated Platform-Specific Notification Listener for Android
[Application] File
AndroidPushNotificationListener.cs
AzureNotificationHubService_Android.cs
The text was updated successfully, but these errors were encountered: