0

I am trying to get to work with xamarin notifications. For android I have followed these steps: https://developers.google.com/cloud-messaging/android/client for the client side to show the notification. On my server side I am using pushSharp plugin and everything is looks to work fine since the message from the server side is sent. The problem is that I don't get the notification on my phone. My app just crushes when I'm in debug mode or sometimes times it crushes when the app is in background too. As I can see this method :

 public override void OnMessageReceived(string from, Bundle data){//Extract the message received from GCM:
        var message = data.GetString("message");
        Log.Debug("MyGcmListenerService", "From:    " + from);
        Log.Debug("MyGcmListenerService", "Message: " + message);

        //Forward the received message in a local notification:
        SendNotification(message);}

it's not called at all. My manifest is as followed :

<service android:name="com.my_companyname_goes_here.app_name_goes_here.android.MyGcmListenerService" android:exported="false">
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        </intent-filter>
    </service>

I tried searching the web for an answer but none as followed solved my problem : GcmListenerService is not called When Application is in Background Xamarin GCM OnMessageReceived is not called ect etc. Any kind of help would be appreciated.

Edit : This is my json

{
"message": "test",
"vibrate": 1,
"sound": 1
}
Idev Dev
  • 184
  • 17
  • Hi. Could you try using FCM instead of GCM and see if you're still experiencing any issues – AL. Dec 13 '17 at 01:07
  • hi @AL. I don't think my push sharp app will work with FCM so for now I'm stuck with GCM. Do you have any idea why this method it is not even called? Have you worked before with GCM notifications? If yes Can you show me an example? Thanks. – Idev Dev Dec 13 '17 at 08:43
  • Did you try adding register in your manifest? Just below the RECEIVE action tag, add the following : ' ' – ADimaano Dec 13 '17 at 22:41
  • You're payload looks odd. Is that the complete payload? I believe those data should be inside a `notification` parameter. – AL. Dec 14 '17 at 04:56
  • @AL. Yes those data were inside a {data} parameter. I solved it now. So thanks anyway. – Idev Dev Dec 14 '17 at 08:42
  • Of course I did @ADimaano but I had few other things which stopped the app from running ok. One of them was a mistake with the package name in android manifest. Anyway thanks. – Idev Dev Dec 14 '17 at 08:44

1 Answers1

0

You could read the Xamarin official document Xamarin Google Cloud Messaging :

https://developer.xamarin.com/guides/android/application_fundamentals/notifications/remote-notifications-with-gcm/

Here is the sample about the above document :

https://developer.xamarin.com/samples/monodroid/RemoteNotifications/

Also, please make sure Google Play Services is available in your device.

Update :

Please try to add the service attribute like this :

[Service(Exported = false), IntentFilter(new[] { "com.google.android.c2dm.intent.RECEIVE" })]
public class MyGcmListenerService : GcmListenerService
{
    ...
}
Community
  • 1
  • 1
York Shen
  • 9,014
  • 1
  • 16
  • 40
  • @york-shen-msft Hi! Of course I have tried the above examples and of course my google play services is available otherwise my device would not be registered to receive notifications. Even the refresh of the token works because I can see it in the server. I will edit my answer where I will show my code for receiving notifications, please take a look at it. – Idev Dev Dec 13 '17 at 08:40
  • @IdevDev, I have updated my answer, please check it. – York Shen Dec 13 '17 at 09:07
  • @york-shen-msft of course I had that already. I followed google instructions very carefully, I didn't miss any step. It's just that OnMessageReceived method it is not invoked at all. Maybe it has something to do with the payload. Let me show you the json I send from the server. Thanks for trying to help me. – Idev Dev Dec 13 '17 at 09:18
  • @IdevDev, also please note that, the first time when you install your project to your device, your application will receive the message from `GCM`. But if you stop debug, didn't unintall the project from your device and deploy the project to your device again, your application can't receive message, for its reason you could refer to my answer : [Android using Firebase Cloud Messaging not receiving message](https://stackoverflow.com/questions/46800477/xamarin-form-android-using-firebase-cloud-messaging-not-receiving-message/46824685#46824685). – York Shen Dec 13 '17 at 09:27
  • @york-shen-msft isn't the same if I just don't deploy my app directly in my phone using an usb cable?! – Idev Dev Dec 13 '17 at 10:07
  • @IdevDev, do you mean that you install your project by download the apk, and after install the application, you can't receive message from `GCM` ? – York Shen Dec 13 '17 at 10:24
  • @york-shen-msft No I don't download my app via apk... I just deploy it from vs2017. From vs2017 I just select my device as the deployment device and not any other emulator. Simple. Than I use my app. This my I can also debug it. Have you built any app that receives notifications? If yes how? I would appreciate a little example. – Idev Dev Dec 13 '17 at 10:39
  • @IdevDev, deploy the app to my real device, then I could receive message from `GCM`, but if I modify some code in my project and **deploy to my device by VS again**, I can't receive message from `GCM`. **If you want receive message again, you need Uninstall the app first from your device first, after that deploy the project to your device, then your application could receive message from `GCM`**. – York Shen Dec 13 '17 at 12:18
  • @IdevDev, if there's anything that make you confusing, please feel free to ask. – York Shen Dec 13 '17 at 12:23
  • @york-shen-msft hi! After a long night of debugging I found out where my problem was. It was a mistake with the package name and the app I created at google developer console. I tried building another app from scratch following this : https://developer.xamarin.com/guides/android/application_fundamentals/notifications/google-cloud-messaging/ And everything looks fine now. Thanks a lot for helping me. I was able to find a lot more approaches to GCM service. So thanks :) – Idev Dev Dec 14 '17 at 08:39
  • @IdevDev, happy coding. : ) – York Shen Dec 14 '17 at 08:40