简体   繁体   中英

Sending push notification via postman to xamarin android

I am using azure push notifications. Its successfully send test message from azure to xamarin.forms app. But when i use postman to send this its status showing 200 OK . But xamarin.android is not receiving push notification. this is my AndroidManifest file

<?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>

I have modified(added new tags) my payload also. But API worked well. so i think my xamarin app have the issue.

i have register with firebase and added the GoogleJson file. And my Device registered to azure successfully.

This is Azure Test Message

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

}

This is my Modified Payload.

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

This is my API POST request

[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();
    }
}

Return value

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

This is my Postman Code. This also sent successfully.

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

From Firebase Cloud Messaging HTTP protocol , set POST Method and send your notification to following address

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

Sent your notification to following address you need to add Add below headers in postman for FCM

Content-Type : application/json

Authorization : key= ******your server key from firebase console ****

在此处输入图像描述

Go to firebase console with your credentials then go to your app setting, get your Server key .

在此处输入图像描述

Go to postman Body->Raw->JSON,sent your notification as JSON format

在此处输入图像描述

Now, I can push notification by postman successfully.

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