GCM push notification when iOS app is in background

I am trying to send push notifications to my iOS application using GCM. The application does not receive a notification when it is in the background, but it runs when it is in the foreground. I tested push notifications using a PHP script that sends a message directly to APNS and runs in the background.

JSON sent to GCM: (I am sending it from the client for testing)

{ "to" : "token...", "notification" : { "title": "GCM TITLE", "body" : "FROM GCM", "badge": "1", "sound": "default" } } 

Doesn't work : User information received from GCM in didReceiveRemoteNotification:

 Notification received: [aps: { alert = { body = "FROM GCM"; title = "GCM TILE"; }; badge = 1; sound = default; }, gcm.message_id: 123...] 

Q : User information received when sending with a PHP script (I also added message_id in JSON to find out if this is a problem)

 Notification received: [aps: { alert = { body = "FROM PHP"; title = "PHP TITLE"; }; badge = 2; sound = default; }, gcm.message_id: 123...] 

I tried adding content_available to JSON with various combinations, but didn't help, Content-Type and Authorization request headers are also set:

 Content-Type:application/json Authorization:key=... 
+5
source share
2 answers

If someone has the same problem, the solution was for me to add β€œpriority”: β€œhigh” for JSON. This way I get notifications in the background.

 { "to" : "token...", "priority": "high", "notification" : { "title": "GCM TITLE", "body" : "FROM GCM", "badge": "1", "sound": "default" } } 
+8
source

To be notified when the application is in the background, please note that we need to add:

 "content_available":true // when app in background "priority":"high" //when app is completely closed not even running in background // "content_available":true ..most important field { "to" : "token...", "priority":"high", "content_available":true, "notification" : { "title": "GCM TITLE", "body" : "FROM GCM", "badge": "1", "sound": "default" } } 
0
source

Source: https://habr.com/ru/post/1233942/


All Articles