通过邮递员向 xamarin android 发送推送通知

时间:2021-06-21 05:37:34

标签: android firebase xamarin.forms push-notification azure-notificationhub

我正在使用 Azure 推送通知。它成功地将测试消息从 azure 发送到 xamarin 表单应用程序。但是当我使用邮递员发送它时,它的状态显示 200 OK。但是 xamarin android 没有收到推送通知。这是我的 AndroidManifest 文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="addovation.android.mobileaction" android:allowBackup="false" android:installLocation="auto">
    <uses-sdk android:minSdkVersion="24" android:targetSdkVersion="29" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <application android:label="PushDemo.Android">
        <receiver android:name="com.google.firebase.iid.FirebaseInstanceIdInternalReceiver" android:exported="false" />
        <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>
    </application>
</manifest>

我也修改了(添加了新标签)我的有效载荷。但是 API 运行良好。所以我认为我的 xamarin 应用程序有问题。

我已经注册了 firebase 并添加了 GoogleJson 文件。并且我的设备成功注册到 azure。

这是 Azure 测试消息

{
"notification":{
    "title":"Notification Hub Test Notification",
    "body":"This is a sample notification delivered by Azure Notification Hubs."
},
"data":{
    "property1":"value1",
    "property2":42
}

}

这是我修改后的有效载荷。

{ \"notification\": { \"title\" : \"Mobile Action\", \"body\" : \"$(alertMessage)\"}, \"data\" : { \"action\" : \"$(alertMessage)\" , \"NotificationMessage\" : $(NotificationMessage) } }"

这是我的 API POST 请求

[HttpPost]
    [Route("requests")]
    [ProducesResponseType((int)HttpStatusCode.OK)]
    [ProducesResponseType((int)HttpStatusCode.BadRequest)]
    [ProducesResponseType((int)HttpStatusCode.UnprocessableEntity)]
    public async Task<IActionResult> RequestPush(
        [Required] NotificationRequest notificationRequest)
    {
        if (notificationRequest.Silent &&
            string.IsNullOrWhiteSpace(notificationRequest?.NotificationMessage[0].ToString()))
           
            return new BadRequestResult();

        var success = await _notificationService
            .RequestNotificationAsync(notificationRequest, HttpContext.RequestAborted);

        if (!success)
            return new UnprocessableEntityResult();

        return new OkResult();
    }
}

返回值

"{
  "TextMessage": "Confirm this Order",
  "Tags": [
    "MyTab"
  ],
  "AppName": "Test",
  "Notification": [
    {
      "PageName": "OrderPage",
      "PageParameters": [
        {
          "ORDER_NO": "05"
        }
      ]
    }
  ]
}"

这是我的邮递员密码。这也发送成功。

    {
  "NotificationMessage": [
    {
      "TextMessage": "Confirm this Order",
      "Tags": [
        "MyTab"
      ],
      "AppName": "Test",
      "Notification": [
        {
          "PageName": "OrderPage",
          "PageParameters": [
            {
              "ORDER_NO": "05"
            }
          ]
        }
      ]
    }
  ]
}

1 个答案:

答案 0 :(得分:0)

Firebase Cloud Messaging HTTP protocol,设置 POST Method 并将您的通知发送到以下地址

https://fcm.googleapis.com/fcm/send

将您的通知发送到以下地址,您需要在 FCM 的邮递员中添加以下标题

Content-Type : 应用程序/json

Authorization: key= ******来自 firebase 控制台的服务器密钥 ****

enter image description here

使用您的凭据转到 firebase 控制台,然后转到您的应用设置,获取您的服务器密钥

enter image description here

转到 postman Body->Raw->JSON,以 JSON 格式发送您的通知

enter image description here

现在,我可以成功通过邮递员推送通知了。