3

私のバックエンドアプリケーションからAndroid携帯電話に通知を送信しようとしています。私は、デバイスをインストールしてそれらを削除することができました。メッセージのペイロードに問題があります。私は警報を発する必要があり、メッセージにアプリケーションデータを送る必要があります。これは私が今ペイロードを構築する方法ですが、私はそれは良くないことだと思う。Azure Notification Hubを使用してアプリケーションデータを含むGoogle Cloud Messagingペイロード

string notificationText = NotificationText(story, profile); 

JProperty messageJProperty = new JProperty("message", notificationText); 
JObject messageJObject = new JObject(messageJProperty); 
JProperty objectJProperty = new JProperty("data", messageJObject); 
JObject message = new JObject(objectJProperty); 
var payload = message.ToString(); 

return payload; 

のthnx

アップデート(2017年11月-3): 私は、このペイロード形式は、Azureので受け入れられることがわかりました。

private string Payload(string notificationText, StoryEntity story, ProfileEntity profile, string deviceToken) 
{ 
     var payload = new JObject 
     (
      new JProperty("registration_ids", new JArray(deviceToken)), 
      new JProperty("data", new JObject(
               new JProperty("title", "Mapporia has new stroy>"), 
               new JProperty("message", notificationText) 
              )), 
      new JProperty("notId", $"{new Random().Next(int.MaxValue)}"), 
      new JProperty("content-available", 1), 
      new JProperty("soundname", "default"), 
      new JProperty("image", @"www/assets/img/logo.png"), 
      new JProperty("image-type", "circle"), 
      new JProperty("style", "inbox"), 
      new JProperty("notData", new JObject(
                new JProperty("storyId", story.Id), 
                new JProperty("profileId", profile.Id) 
               )) 
     ).ToString(Newtonsoft.Json.Formatting.None); 

     return payload; 
    } 

これは私のJSONがどのように見えるかです:

enter image description here

しかし、今Azureでは、例外がスローされます。

1 2017-11-01 Create Story : The remote server returned an error: (400) Bad Request. The supplied notification payload is invalid.TrackingId:666febf6-85fe-4ebd-867d-00ce5a668809_G3,TimeStamp:11/1/2017 9:53:07 PM

は、私が何かを見逃していましたか? このpageによれば、私はそれを間違って作りました!

答えて

1

This is how I build the payload now, but I think it's not good

正しく理解してjsonの命令が修正されていれば、それを行うためにオブジェクトをシリアル化することができます。更新

string notificationText = NotificationText(story, profile); 

TestData testData = new TestData { Data = new Data { Message = notificationText }}; 

var payload = JsonConvert.SerializeObject(testData).ToLowerInvariant(); 


public class TestData 
{ 
     public Data Data; 
} 

public class Data 
{ 
     public string Message; 
} 

:次はデモコードです

GCMメッセージは、我々はこのtutorial【選択からGCMメッセージについての詳細情報を得ることができ、クライアントアプリへのペイロードの4キロバイトまで可能制限は4kbであり、これ以上はできません。サウンドを送信する必要がある場合は、バイナリデータを格納するURLを指すメッセージでカスタムjsonを送信することをお勧めします。

Google Cloud Messaging (GCM) is a free service that enables developers to send messages between servers and client apps. This includes downstream messages from servers to client apps, and upstream messages from client apps to servers.

For example, a lightweight downstream message could inform a client app that there is new data to be fetched from the server, as in the case of a "new email" notification. For use cases such as instant messaging, a GCM message can transfer up to 4kb of payload to the client app. The GCM service handles all aspects of queueing of messages and delivery to and from the target client app.

+0

ペイロード形式が問題ではなく、どのように構築しますそれは物理的に。 – Wasyster

+0

私は答えを更新しました。詳細情報は、**更新**セクションを参照してください。 –

+0

Thnx、私はこの部分をどのように得たのですか。通知内でアプリデータを送信する方法、音を有効にする方法を知りたい – Wasyster

1

あなたはこのコール

var payload = new JObject(
         new JProperty("data", new JObject(
         new JProperty("message", notificationText)))) 
         .ToString(Newtonsoft.Json.Formatting.None); 

にあなたのペイロードを簡素化することができますGCMはそれを受け入れるように、出力がJSON形式のペイロードになります。

{"data":{"message":"your notification Text"}} 

このソリューションでは、NewtonsoftのJSONシリアライザを使用してJObjectをシリアライズしました。

1

GCMのための正しいJSON形式は次のとおりです。

{ 
     "to" : "{{devicetoken}} OR {{registrationID form Azure}}", 
     "data": 
      { 
       "title":"{{title goes here}}", 
       "message":"{{message body goes here}}", 
       "priority":"high" 
      }, 
     "notId":"{{unique ID, I used RANDOM to generate it}}", 
     "content-available":1, 
     "soundname":"default", 
     "image":"www/assets/img/logo.png", 
     "image-type":"circle", 
     "style":"inbox", 
     "notData": 
      { 
       "storyId":1, 
       "profileId":6 
      } 
    } 

そして、どのようにNewtonsoft JSON nuget packegeとC#を使用して、このJSONを構築するには:

  var payload = new JObject 
      (
       new JProperty("to", deviceToken), 
       new JProperty("data", new JObject(
                new JProperty("title", "title goes here"), 
                new JProperty("message", "notification text goes here"), 
                new JProperty("priority", "high") 
               )), 
       new JProperty("notId", $"{new Random().Next(int.MaxValue)}"), 
       new JProperty("content-available", 1), 
       new JProperty("soundname", "default"), 
       new JProperty("image", @"www/assets/img/logo.png"), 
       new JProperty("image-type", "circle"), 
       new JProperty("style", "inbox"), 
       new JProperty("notData", new JObject(
                 new JProperty("storyId", story.Id), 
                 new JProperty("profileId", profile.Id) 
                )) 
      ).ToString(Newtonsoft.Json.Formatting.None); 
関連する問題