GCM downstream messaging JSON format for iOS

2020-07-24 04:18发布

问题:

I have an swift/iOS9 application using GCM for it's notifications (WIP).

Application authorisations OK. Certificates are OK. Configuration file OK. Everything is configured on the Apple's developpers portal for development.

This function is called when a notification is received.

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
  GCMService.sharedInstance().appDidReceiveMessage(userInfo);
  print(userInfo.debugDescription)
}

Problem

I can only detect a notification in the previously described function when the server send the following format, and it 'works' because i have a breakpoint to detect it, else nothing happen.

{
  "registration_ids" : ["regId"],
  "data" :
   {
      "to" : "regId",
      "notification" : 
      {
         "sound" : "default",
         "badge" : "2",
         "title" : "anyTitle",
         "body" : "anyMessage" 
      }
   }
}

The person in charge of the webservices made this for me, and it duplicated the existing Android one (Is "registration_ids" any usefull?).

Since the behaviour wasn't the expected one, we tried this from GCM website :

{ "to" : "regId", "content_available" : true, "notification" : { "body" : "great match!", "title" : "Portugal vs. Denmark" } }

The message create an error server side (invalid format for GCM?)

Do we miss something obvious?

Requests are post and headers/url:

https://gcm-http.googleapis.com/gcm/send
Content-Type:application/json
Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA

EDIT

Adding the log of the working and non working one.

{  
   "registration_ids":[  
      "regId"
   ],
   "data":{  
      "to":"regId",
      "notification":{  
         "sound":"default",
         "badge":"2",
         "title":"testNotif",
         "body":"welcome in the Showcase Apple owner"
      }
   }
}

And

Exchange[
Id                  ID-FR-LIL-D00184-54996-1457452459441-0-5
ExchangePattern     InOnly
Headers             {Accept=text/html,application/xhtml+xml,application/xml;q=0.9,​*/*​;q=0.8, Accept-Encoding=gzip, deflate, Accept-Language=fr-fr, Authorization=key=AIzaSyAB_E2Op0GqShCmCmh_6ZxnwrFKoXOaIHU, beaconId=46589-47438, breadcrumbId=ID-talend2-48271-1456928459992-11-173, Cache-Control=max-age=0, CamelHttpMethod=POST, CamelHttpPath=, CamelHttpQuery=custLogin=alex@cgi.com&beaconId=46589-47438&regId=khjGINhshr4:APA91bGXuzrC3tU_jkBMZGCziqIwA9wKv1B-U4acxy68sQxvChJQvKb187o863CzKJyop1AwhP0BNo7I2SJJiWdrtnHFC42LxcBQzOo2Ah868xPde9TBFmj_FLVG8rhyH4Yl48zuQMCJ, CamelJmsDeliveryMode=2, CamelRedelivered=false, CamelRedeliveryCounter=0, CamelServletContextPath=/setBeaconEvent, Connection=keep-alive, Content-Type=application/json, custLogin=, dateEvent=20160308172331, deviceType=ios, DNT=1, Host=192.168.1.239, JMSCorrelationID=null, JMSDeliveryMode=2, JMSDestination=queue://Q.NOTIFIER, JMSExpiration=0, JMSMessageID=ID:FR-LIL-D00184-64570-1457442695117-1:52:1:1:1, JMSPriority=4, JMSRedelivered=false, JMSReplyTo=null, JMSTimestamp=1457454212345, JMSType=null, JMSXGroupID=null, JMSXUserID=null, regId=khjGINhshr4:APA91bGXuzrC3tU_jkBMZGCziqIwA9wKv1B-U4acxy68sQxvChJQvKb187o863CzKJyop1AwhP0BNo7I2SJJiWdrtnHFC42LxcBQzOo2Ah868xPde9TBFmj_FLVG8rhyH4Yl48zuQMCJ, User-Agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/601.4.4 (KHTML, like Gecko) Version/9.0.3 Safari/601.4.4}
BodyType            String
Body                {"registration_ids":["khjGINhshr4:APA91bGXuzrC3tU_jkBMZGCziqIwA9wKv1B-U4acxy68sQxvChJQvKb187o863CzKJyop1AwhP0BNo7I2SJJiWdrtnHFC42LxcBQzOo2Ah868xPde9TBFmj_FLVG8rhyH4Yl48zuQMCJ"],"to":"khjGINhshr4:APA91bGXuzrC3tU_jkBMZGCziqIwA9wKv1B-U4acxy68sQxvChJQvKb187o863CzKJyop1AwhP0BNo7I2SJJiWdrtnHFC42LxcBQzOo2Ah868xPde9TBFmj_FLVG8rhyH4Yl48zuQMCJ", "content_available": true, "notification":{"title": "testNotif","body": "welcome in the Showcase Apple owner" }}
]
Stacktrace
---------------------------------------------------------------------------------------------------------------------------------------
org.apache.camel.component.http.HttpOperationFailedException: HTTP operation failed invoking https://android.googleapis.com/gcm/send?custLogin=alex@cgi.com&beaconId=46589-47438&regId=khjGINhshr4:APA91bGXuzrC3tU_jkBMZGCziqIwA9wKv1B-U4acxy68sQxvChJQvKb187o863CzKJyop1AwhP0BNo7I2SJJiWdrtnHFC42LxcBQzOo2Ah868xPde9TBFmj_FLVG8rhyH4Yl48zuQMCJ with statusCode: 400

EDIT2

Thanks to Arthur advices, the notification leaves the server without error now, and I receive it.

Current format is :

{  
  "to":"regId",
  "data":{  
     "notification":{  
        "sound":"default",
        "badge":"2",
        "title":"testNotif",
        "body":"welcome in the Showcase Apple owner"
     }
  }
}

The remaining problem is that didReceiveRemoteNotification fetchCompletionHandler don't receives it if app is in background. Also didReceiveRemoteNotification receives it when app is foreground, but if I don't display it nothing happen.

I am supposed to call something to have the 'system style' notification? Is it ready out of the box if the format is correct?

I can't find informations about this over the web.

回答1:

So when you use the first format you mentioned, you receive the notification (the app stops at the breakpoint)? My only idea is that maybe your app is in foreground when you receive the notification? It should be in the background or closed otherwise you won't notice anything.



回答2:

Ok so, after all the only problem was the JSON format :

{  
  "to":"regId",
  "notification":{  
     "sound":"default",
     "badge":"2",
     "title":"testNotif",
     "body":"welcome in the shop Apple owner"
  }
}

This way the notification is processed by APNS and arrive refined in the correct format.