简体   繁体   中英

XAMARIN - Firebase Cloud Messaging - Android Device not display notification but he receives it

Hi I'm trying to send notifications to an Android device.

I am using this plugin CrossGeeks/PushNotificationPlugin . I created the firebase project and extracted the parameters for use on webapi that I made with .NET Core MVC 3.1 . When I send the notification, the breakpoint on the event is triggered correctly

CrossPushNotification.Current.OnNotificationReceived + = (s, p) =>

However, no notification is displayed on my device. If, on the other hand, I use the firebase console to send a notification, it appears correctly with both the inactive app and the foreground app.

For this reason, since my client application correctly receives notifications from firebase, I think the problem is server side. This is my code:

[HttpPost, Route("Prova")]
public async Task<IActionResult> SendNotication()
{
    try
    {
        var notification = new GoogleNotification();
        notification.Data = new GoogleNotification.DataPayload();
        notification.Data.Title = "Title";
        notification.Data.Body = "bla bla";        


        using (var fcm = new FcmSender("xxxxxx", "yyyyy"))
        {

            await fcm.SendAsync("hhhhhhh", notification);

        }



        return Ok("Done");
    }
    catch (Exception ex)
    {
        return BadRequest(ex.Message);
    }
}

 public class GoogleNotification
    {
        public class DataPayload
        {
            [JsonProperty("title")]
            public string Title { get; set; }
            // Add your custom properties as needed
            [JsonProperty("body")]
            public string Body { get; set; }

            [JsonProperty("tag")]
            public string Tag { get; set; }
        }

        //[JsonProperty("priority")]
        //public string Priority { get; set; } = "high";

        [JsonProperty("data")]
        public DataPayload Data { get; set; }
    }

I have doubts about the GoogleNotification class, do you think it is correct?

I tried through the chrome console to understand the json that generates the firebase console. in fact it is very different to my code

{
  "basics": {},
  "productAction": {
    "notificationAction": {
      "campaignId": "1581564767326567420",
      "messageText": "ciao",
      "campaignStatus": "STATUS_ENQUEUED",
      "displayParameters": {
        "title": "Titolo",
        "priority": "HIGH"
      },
      "registrationIds": [
        "dcdsdsffds"
      ],
      "expiryTime": "2419200s",
      "lastUpdateTime": "2020-01-23T09:15:57.079Z"
    }
  }
}

On Firebase Cloud Messaging there are two types of messages that you can send to clients,refer to Firebase Push Notifications :

Notification messages - Delivered when the application is in background. These messages trigger the onMessageReceived() callback only when your app is in foreground. Firebase will provide the ui for the notification shown on Android device.

{
  "notification" : 
  {
    "body" : "hello",
    "title": "firebase",
    "sound": "default",
    "tag" : "msg"
  },
  "registration_ids" : ["eoPr8fUIPns:APA91bEVYODiWaL9a9JidumLRcFjVrEC4iHY80mHSE1e-udmPB32RjMiYL2P2vWTIUgYCJYVOx9SGSN_R4Ksau3vFX4WvkDj4ZstxqmcBhM3K-NMrX8P6U0sdDEqDpr-lD_N5JWBLCoV"]
}

Data messages - Handled by the client app. These messages trigger the onMessageReceived() callback even if your app is in foreground/background/killed. When using this type of message you are the one providing the UI and handling when push notification is received on an Android device.

{
  "data": {
      "message" : "my_custom_value",
      "other_key" : true,
      "body":"test"
   },
 "priority": "high",
 "condition": "'general' in topics"
}

and Notification Customization you could refer to Android and ios ,so you could check the json from your server

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM